简体   繁体   English

根据已批准状态和特定订单项目更改 WooCommerce 订单状态

[英]Change WooCommerce order status based on approved status and specific order item

I try to change a WooCommerce order status to "Processing" when the current status is "Approved" and the order includes a specific product ( id = 10).当当前状态为“已批准”且订单包含特定产品(id = 10)时,我尝试将 WooCommerce 订单状态更改为“处理中”。

I have attempted the code below but it is not working.我尝试了下面的代码,但它不起作用。 I am quite new to php and would appreciate any guidance!我对 php 很陌生,希望得到任何指导!

add_action('woocommerce_order_status_changed', 'ts_auto_complete_business_cards');

function ts_auto_complete_business_cards($order_id)
{

    if ( ! $order_id ) {
        return;
    }

    global $product;
    $order = wc_get_order( $order_id );

    if ($order->data['status'] == 'approved') {
        $items=$order->get_items();
        foreach ( $items as $item ) {
            $product_id = $item->get_product_id();
            if ($product_id!="10")
            {
                $order->update_status( 'completed' );
            }

        }

    }
}
  • woocommerce_order_status_changed has 4 parameters woocommerce_order_status_changed有 4 个参数
  • This line -> if ($product_id!="10") says NOT equal, you also compare with a string and not with a numerical value这行 -> if ($product_id!="10")说不等于,你也比较字符串而不是数值

Try it this way试试这个方法

function action_woocommerce_order_status_changed( $order_id, $old_status, $new_status, $order ) {

    // Compare
    if( $old_status === 'approved' ) {
        // Get items
        $items = $order->get_items();

        foreach ( $items as $item ) {
            // Get product id
            $product_id = $item->get_product_id();

            if ($product_id == 10 ) {
                $order->update_status( 'processing' );
                break;
            }
        }
    }
}
add_action( 'woocommerce_order_status_changed', 'action_woocommerce_order_status_changed', 10, 4 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM