68228

Save WooCommerce order item custom fields sum as a new meta data

Question:

When a particular product is ordered in this WooCommerce store, two meta values are added to the order.

The two fields that store the meta values are located in wp_woocommerce_order_itemmeta

The meta keys are :

quantity assemblycost

I want to create a new custom field programmatically when a new order is placed and set the value of this new field equal to quanity * assemblycost if the meta key assemblycost exists for the product that has been ordered.

After some research I discovered that woocommerce_checkout_update_order_meta is a hook that is executed after an order is saved to the database and the meta data has been updated. So this appears to be the hook I should use.

Ref: <a href="https://stackoverflow.com/questions/25626058/add-extra-meta-for-orders-in-woocommerce" rel="nofollow">Add extra meta for orders in Woocommerce</a> :

function add_item_meta( $order_id ) { //global $woocommerce; update_post_meta( $order_id, '_has_event', 'yes' ); }

I tried adding the following code, in functions.php:

add_action('woocommerce_checkout_update_order_meta',function( $order_id, $posted ) { $assemblycost = wc_get_order_item_meta($order_id, 'assemblycost'); $quantity = wc_get_order_item_meta($order_id, 'quantity'); $calculatedValue = $quantity * $assemblycost; wc_update_order_item_meta( $order_id, 'calculated_field', $calculatedValue ); } , 10, 2);

This does create the new meta field, however it sets the value to 0.

<a href="https://i.stack.imgur.com/l7XAe.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/l7XAe.png" data-original="https://i.stack.imgur.com/l7XAe.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

How can I change the code above so that the value of calculated_field is the multiplication of quantity * assemblycost ?

Answer1:

This is related to <strong>order items</strong> and should be saved as order item meta data but not as order meta data.

Now this should be saved <strong>at the same time</strong> than your 2 custom fields quantity and assemblycost. So you should provide in your question the related code that save quantity and assemblycost as order item meta data.

You can try the following (but I am not sure that I will work):

add_action( 'woocommerce_checkout_create_order_line_item', 'action_checkout_create_order_line_item_callback', 1000, 4 ); function action_checkout_create_order_line_item_callback( $item, $cart_item_key, $cart_item, $order ) { $quantity = $item->get_meta('quantity'); $assemblycost = $item->get_meta('assemblycost'); if( isset($quantity) && isset($assemblycost) ) { $item->update_meta_data( 'calculated_field', $quantity * $assemblycost ); } }

Code goes in function.php file of your active child theme (or active theme).

Recommend

  • Jitpack: Failed to install the following Android SDK packages as some licences have not been accepte
  • AWS CLI S3API find newest folder in path
  • Debugging a SOAP service using xDebug
  • Use reselect selector with parameters
  • Iterate over all items in json object
  • Google Apps Script Async function execution on Server side
  • Installing apk on android device via ADB with Java program on Linux
  • Laravel - Collection with relations take a lot of time
  • How to get value from Fields in redux-form?
  • Spring, Interceptor's excludePathPatterns function is not working properly
  • MVC5 Render View (with parameters) as String
  • Pimcore where does code go
  • How to change node value using node's current value in SQL with XQuery (SQL Server)
  • Converting numerics with COMMAw,d
  • Passing Control Id from Linkbutton in a gridview
  • Upload video and extract thumbnail using Amazon s3
  • List (and connect to) Bluetooth devices from within iOS App
  • How to position a Widget at the bottom of a SingleChildScrollView?
  • Train two models concurrently
  • MySQL ordering a date range to the beginning, then ordering everything else by other criteria
  • Identifying dates in strings using NLTK
  • Arc gradients in Flutter?
  • jQuery YQL SELECT FROM rss variable
  • read.table returning character matrix, would like numeric
  • Why am I getting an Argument exception when creating event handler dynamically?
  • Simultaneous animation when entering editing mode of UITableViewCell
  • jQuery Ajax call to WCF service returning “Method not allowed (405)”
  • .Net core Hosted Services guaranteed to complete
  • opencv deskewing a contour
  • How to define a custom accuracy in Keras to ignore samples with a particular gold label?
  • What does “T extends Junk” mean in a generic class in Java?
  • Request Access Token in Postman for Azure Function App protected by Azure AD B2C
  • How to clear a browser cache in Protractor
  • matrix multiplication apache pig
  • php “page caching” solution suggestions for CMS Applications
  • multiple button click in asp.net MVC 3
  • Jersey serializes character value to ASCII equivalent numeric string
  • CAS 4 - Not able to retrieve the LDAP groups after successful authentication
  • Running R's aov() mixed effects model from Python using rpy2
  • What does the “id” field in an Android “Google Play Music” broadcast intent correspond to?