简体   繁体   English

在 WooCommerce 特定订单状态更改上以编程方式添加免费产品

[英]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_statushas_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.

相关问题 更改 WooCommerce 中没有特定产品的“暂停”订单的订单状态 - Change order status for “on-hold” orders that doesn't have a specific product in WooCommerce 根据已批准状态和特定订单项目更改 WooCommerce 订单状态 - Change WooCommerce order status based on approved status and specific order item 在 WooCommerce 中更改虚拟、可下载、免费或延期交货产品的订单状态 - Change order status for virtual, downloadable, free or backorder products in WooCommerce 批量更改订单信息作为特定Woocommerce订单ID的订单状态 - Batch change order information as order status for specific Woocommerce order IDs 将定制价格的产品添加到WooCommerce中以编程方式创建的订单 - Add product with custom price to an Order programmatically created in WooCommerce 将免费的产品添加到购物车 - Add Woocommerce free product to cart Woocommerce:以编程方式更新订单状态 - Woocommerce: Programmatically Updating Order Status 在 Woocommerce 中添加自定义订单状态并发送有关状态更改的电子邮件 - Add custom order status and send email on status change in Woocommerce 通过 WooCommerce 中的特定数据以编程方式添加产品变体 - Add product variations programmatically via specific data in WooCommerce 在 WooCommerce 订单状态更改上添加用户元数据作为订单元 - Add user meta data as order meta on WooCommerce order status change
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM