[英]Add a free product programmatically on WooCommerce specific order status change
Im trying to add a line item when I update an order in the backend and it matches the status update of "enquiry" but no item is added.当我在后端更新订单时,我尝试添加一个行项目,它与“查询”的状态更新匹配,但没有添加项目。
I tried both get_status
and has_status
but I cant get it to work.我尝试了get_status
和has_status
但我无法让它工作。 Any ideas on why this isn't working?关于为什么这不起作用的任何想法?
Code:代码:
add_action( 'woocommerce_order_status_changed', 'add_free_products', 20, 4 );
function add_free_products( $order_id, $order ){
if ( ! $order_id )
return;
// Getting an instance of the order object
$order = wc_get_order( $order_id );
if( $order->get_status() == 'enquiry' ) {
$product_id = '155185';
$product = wc_get_product( $product_id );
$order->add_product( $product);
$order->save();
}
}
There are some mistakes and missing things, Use the following instead:有一些错误和遗漏的东西,请改用以下内容:
add_action( 'woocommerce_order_status_changed', 'add_free_product_on_order_enquiry_status', 20, 4 );
function add_free_product_on_order_enquiry_status( $order_id, $old_status, $new_status, $order ){
if ( "enquiry" === $new_status ) {
$product_id = '155185';
$product = wc_get_product( $product_id );
$order->add_product( $product );
$order->calculate_totals(); // calculate taxes, totals and save (method includes save() method)
// $order->save();
}
}
Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的functions.php文件。 Tested and works.测试和工作。
Addition 1加法1
You could also flag this action with custom meta data, to avoid adding multiple products if you change order status multiple times with the following:您还可以使用自定义元数据标记此操作,以避免在多次更改订单状态时添加多个产品:
add_action( 'woocommerce_order_status_changed', 'add_free_product_on_order_enquiry_status', 20, 4 );
function add_free_product_on_order_enquiry_status( $order_id, $old_status, $new_status, $order ){
if ( "enquiry" === $new_status && ! $order->get_meta('_free_product_added') ) {
$product_id = '155185';
$product = wc_get_product( $product_id );
$order->add_product( $product );
$order->update_meta_data('_free_product_added', 'yes'); // Flag the order
$order->calculate_totals(); // calculate taxes, totals and save (method includes save() method)
}
}
Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的functions.php文件。 Tested and works.测试和工作。
Addition 2 - Check if the product has been already added to the order (avoiding adding the product multiple times) : Addition 2 - 检查产品是否已经添加到订单中(避免多次添加产品) :
function order_has_specific_product( $product_id, $order ) {
// Loop through order items to check if a product is on the current order
foreach ( $order->get_items() as $item ) {
if ( in_array( $product_id, array($item->get_product_id(), $item->get_variation_id()) ) ) {
return true;
}
}
return false;
}
add_action( 'woocommerce_order_status_changed', 'add_free_product_on_order_enquiry_status', 20, 4 );
function add_free_product_on_order_enquiry_status( $order_id, $old_status, $new_status, $order ){
$product_id = '155185'; // Free product to be added only once
if ( "enquiry" === $new_status && ! order_has_specific_product( $product_id, $order ) ) {
$product = wc_get_product( $product_id );
$order->add_product( $product );
$order->calculate_totals(); // calculate taxes, totals and save (method includes save() method)
}
}
Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的functions.php文件。 Tested and works.测试和工作。
For a custom order status you could use the woocommerce_order_status_'. $status_transition['to']
对于自定义订单状态,您可以使用woocommerce_order_status_'. $status_transition['to']
woocommerce_order_status_'. $status_transition['to']
action hook, where you will replace $status_transition[to]
by enquiry
woocommerce_order_status_'. $status_transition['to']
动作挂钩,您将在其中通过enquiry
替换$status_transition[to]
So you get:所以你得到:
function action_woocommerce_order_status_enquiry( $order_id, $order ) {
// Product ID
$product_id = 155185;
// Get product
$product = wc_get_product( $product_id );
// Add an order line item + quantity
$order->add_product( $product, 1 );
// Calculate totals and SAVE order data
$order->calculate_totals();
}
add_action( 'woocommerce_order_status_enquiry', 'action_woocommerce_order_status_enquiry', 10, 2 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.