自动完成Woocommerce上特定产品ID的已付款订单

时间:2019-08-06 14:57:03

标签: woocommerce

有什么方法可以仅针对Woocommerce上的特定产品ID自动完成订单?

我使用代码on this thread自动完成订单。

我也阅读了this thread,但它从自动完成功能中排除了产品ID。而且我无法使它反过来工作。

由于我的商店中有20多种产品,并且只想在其中2种产品上使用自动完成功能,所以如果我可以指定要自动完成的订单ID,那就太好了。

1 个答案:

答案 0 :(得分:1)

这是一种自动完成特定产品IDS的已付款订单的方法:

add_action( 'woocommerce_payment_complete_order_status', 'wc_auto_complete_paid_order', 10, 3 );
function wc_auto_complete_paid_order( $status, $order_id, $order ) {
    // Below the targeted product Ids
    $targeted_ids = array(37, 53);

    // Loop through order line items
    foreach( $order->get_items() as $item ) { 
        if ( in_array( $item->get_product_id(), $targeted_ids ) || in_array( $item->get_variation_id(), $targeted_ids ) ) {
            return 'completed';
        }
    }

    return $status;
}

代码进入活动子主题(或活动主题)的functions.php文件中。