当订单中有延期交货项目时更改订单状态

Tus*_*nna 1 php wordpress status orders woocommerce

在 WooCommerce 中,on-hold如果此订单中有延期交货的商品,如何将订单状态更改为其他状态?

我曾尝试使用挂钩在woocommerce_order_status_on-hold动作挂钩中的自定义函数,但没有成功。

任何人都可以帮助我解决这个问题吗?

谢谢。

Loi*_*tec 5

这是一个钩在woocommerce_thankyou动作钩子中的自定义函数,如果该订单具有“暂停”状态并且其中有任何缺货产品,它将更改订单状态。

你将不得不以一套在功能所需的新状态蛞蝓变化。

这是自定义函数(代码注释良好)

add_action( 'woocommerce_thankyou', 'change_paid_backorders_status', 10, 1 );
function change_paid_backorders_status( $order_id ) {

    if ( ! $order_id )
        return;

    // HERE below set your new status SLUG for paid back orders  
    $new_status = 'completed';

    // Get a an instance of order object
    $order = wc_get_order( $order_id );

    // ONLY for "on-hold" ORDERS Status
    if ( ! $order->has_status('on-hold') )
        return;

    // Iterating through each item in the order
    foreach ( $order->get_items() as $item ) {

        // Get a an instance of product object related to the order item
        $product = $item->get_product();

        // Check if the product is on backorder
        if( $product->is_on_backorder() ){
            // Change this order status
            $order->update_status($new_status);
            break; // Stop the loop
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。

该代码经过测试并有效。