Woocommerce: Which hook to replace deprecated woocommerce_add_order_item_meta
If you look at wc-deprecated-functions.php
you will see
/**
* @deprecated
*/
function woocommerce_add_order_item_meta( $item_id, $meta_key, $meta_value, $unique = false ) {
return wc_add_order_item_meta( $item_id, $meta_key, $meta_value, $unique );
}
Basically, the function was renamed to wc_add_order_item_meta()
, so if you need the function then use that. The action hook was not renamed and remains in class-wc-checkout.php
as:
// Allow plugins to add order item meta
do_action( 'woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key );
Replace woocommerce_add_order_item_meta hook in Woocommerce 3.4
Updated
Since Woocommerce version 3, woocommerce_checkout_create_order_line_item
action hook now replace outdated woocommerce_add_order_item_meta
hook in a much better way using the new introduced CRUD getters and setters methods:
// Save custom data to order item meta data
add_action( 'woocommerce_checkout_create_order_line_item', 'tshirt_order_meta_handler', 20, 4 );
function tshirt_order_meta_handler( $item, $cart_item_key, $values, $order ) {
$custom_designer = WC()->session->get( $cart_item_key.'_designer' );
if( ! empty($custom_designer) ) {
$item->update_meta_data( 'custom_designer', $custom_designer );
}
}
Code goes in function.php file of your active child theme (or active theme). It should works.
See this related thread:
Woocommerce: Which hook to replace deprecated "woocommerce_add_order_item_meta"
Replace woocommerce_add_order_item_meta hook in Woocommerce 3+
You need to change more than just the hook… The hooked function arguments are wrong in your code and the way to updated data has changed too. Try the following:
add_action( 'woocommerce_checkout_create_order_line_item', 'add_business_days', 10, 4 );
function add_business_days( $order_item, $cart_item_key, $cart_item, $order ){
$now = current_time( 'mysql' );
$add = $cart_item['data']->get_meta( 'dp_kargolanma_suresi' );
$d = new DateTime( $now );
$t = $d->getTimestamp();
$oneDay = 86400;
$nextDay = date('w', ($t + $oneDay));
// loop for X days
for( $i = 0; $i < $add; $i++ ) {
// if it's Saturday or Sunday get $i-1
if($nextDay == 0 || $nextDay == 6) {
$i--;
}
// modify timestamp, add 1 day
$t = $t + $oneDay;
}
$d->setTimestamp($t);
$teslim_tarihi = $d->format( 'd-m-Y' ). "\n";
$desiveyakg = ( $cart_item['quantity'] * $cart_item['data']->get_weight() );
$kargosinifi = $cart_item['data']->get_shipping_class();
$order_item->update_meta_data( 'En Gec Kargolanma Tarihi', $teslim_tarihi );
$order_item->update_meta_data( '_desi_kg', $desiveyakg );
$order_item->update_meta_data( '_alici_onay_durumu', 'Onay Bekliyor' );
if($cart_item['quantity'] >= 100) {
$order_item->update_meta_data( '_kargo_sinifi', 'ucretsiz-kargo' );
} else {
$order_item->update_meta_data( '_kargo_sinifi', $kargosinifi );
}
}
It should work…
Problem with woocommerce_add_order_item_meta
Hook woocommerce_add_order_item_meta
is replaced by woocommerce_checkout_create_order_line_item
, so with your code (assuming that the cart object contains your custom cart item data):
add_action('woocommerce_checkout_create_order_line_item', 'save_custom_order_item_meta_data', 10, 4 );
function save_custom_order_item_meta_data( $item, $cart_item_key, $values, $order ) {
if( isset( $values['custom_data']['label'] ) && isset( $values['custom_data']['value'] ) ) {
$item->update_meta_data( $values['custom_data']['label'], $values['custom_data']['value'] );
}
}
Code goes in function.php file of your active child theme (or active theme). It should works.
Related:
- Replace woocommerce_add_order_item_meta hook in Woocommerce 3.4
- Woocommerce: Which hook to replace deprecated "woocommerce_add_order_item_meta"
- Threads with:
woocommerce_checkout_create_order_line_item
action hook
How to add new meta item for each order item in WooCommerce?
Try this instead,
add_action( 'woocommerce_checkout_create_order_line_item', 'add_my_meta_to_line_item', 10, 4 );
function add_my_meta_to_line_item( $item, $cart_item_key, $values, $order) {
if ($_POST['design']) $item->add_meta_data( 'design', esc_attr($_POST['design'], true ));
}
Trying to save order item meta data with woocommerce_new_order_item hook
Since Woocommerce 3 and the new CRUD setters and getters methods, use this instead:
add_action( 'woocommerce_checkout_create_order_line_item', 'add_booking_order_line_item', 20, 4 );
function add_booking_order_line_item( $item, $cart_item_key, $values, $order ) {
// Get cart item custom data and update order item meta
if( isset( $values['adults_qty'] ) ){
if( ! empty( $values['adults_qty'] ) )
$item->update_meta_data( 'Adults', $values['adults_qty'] );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.
See this related explanations: Woocommerce: which hook to use instead of deprecated "woocommerce_add_order_item_meta"
Woocommerce which hook to use for order status changes
"I need to catch every time an order status changes from
wc_on_hold
towc_completed
"
You would need to change your hook to woocommerce_order_status_changed
.
"I couldn't get on Chrome's Console that echo giving me the order price"
You can't use javascript
and console.log
in this hook. You would need to either use the die
function or the error_log
function to log it to your debug.log
file.
Using die
function
add_action('woocommerce_order_status_changed', 'so_status_completed', 10, 3);
function so_status_completed($order_id, $old_status, $new_status)
{
$order = wc_get_order($order_id);
//$order_total = $order->get_formatted_order_total();
$order_total = $order->get_total();
die($order_total);
}
Using error_log
function
dd_action('woocommerce_order_status_changed', 'so_status_completed', 10, 3);
function so_status_completed($order_id, $old_status, $new_status)
{
$order = wc_get_order($order_id);
//$order_total = $order->get_formatted_order_total();
$order_total = $order->get_total();
error_log(print_r('order total: ' . $order_total, true));
}
Second question
"since I am just here, I want to ask you if
$order-get_total()
is the right one to retrieve order amount without fee applied?"
You might want to take a look at the $order->get_subtotal()
for the totals before shipping, coupons and taxes:
Please see these related answers:
- https://stackoverflow.com/a/47782647/15040627
- https://stackoverflow.com/a/61102779/15040627
- https://stackoverflow.com/a/32634480/15040627
- https://stackoverflow.com/a/44708344/15040627
woocommerce_add_order_item_meta hook sending only one argument; three expected
Shame on me!I had not found necessary to add the last two arguments of the add_action() function. So the default number of argument being one, only one was passed to my callback.
so the right code for the add_action() function is:
add_action('woocommerce_add_order_item_meta', array($this, 'charlie_ajoute_un_order_item_meta'), 10, 3);
Hope it may help someone.
Charles
Which Woocommerce hook when order is purchased for an external delivery service
Instead, you could try to use the following hooked function, that will work for defined order statuses only once, after the delivery data is processed.
add_action( 'woocommerce_order_status_changed', 'delivery_information_process', 20, 4 );
function delivery_information_process( $order_id, $old_status, $new_status, $order ){
// Define the order statuses to check
$statuses_to_check = array( 'on-hold', 'processing' );
// Only "On hold" order status and "Free Shipping" Shipping Method
if ( $order->get_meta( '_delivery_check', true ) && in_array( $new_status, $statuses_to_check ) )
{
// Getting all WC_emails objects
foreach($order->get_items() as $item_id => $item ){
$product = $item->get_product();
$sku = $product->get_sku();
}
## ==> Process delivery data step
// Once delivery information is send or processed ==> update '_delivery_check' to avoid repetitions
$order->update_meta_data( '_delivery_check', true );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Related Topics
How to Run a PHP File in a Scheduled Task (Windows Task Scheduler)
Finding the Subsets of an Array in PHP
How to Create a Copy of an Object in PHP
How to Loop Through a MySQL Result Set
Truncate Text Containing Html, Ignoring Tags
Checking If Customer Has Already Bought Something in Woocommerce
Get Data from Json File With PHP
How to Prevent Multiple Form Submission on Multiple Clicks in PHP
When to Use Static VS Instantiated Classes
Convert String to Binary Then Back Again Using PHP
Remove Script Tag from HTML Content
How to Only Allow Certain Filetypes on Upload in PHP
How to Resolve Curl Error (7): Couldn't Connect to Host
Why Does PHP 5.2+ Disallow Abstract Static Class Methods
Header Only Retrieval in PHP Via Curl
How to Check If an Email Address Is Real or Valid Using PHP
Amazon S3 - How to Fix 'The Request Signature We Calculated Does Not Match the Signature' Error