将 WooCommerce 的“暂停”订单更改为特定产品的自定义状态

时间:2021-02-20 10:55:29

标签: php wordpress woocommerce arguments orders

我需要有关此代码的帮助,如果它包含 ID 为 19345 的特定产品,我需要它将订单状态从“暂停”状态更改为“customstatus”。

我已经创建了这个“customstatus”并且我正在使用一个 ACH 支付网关来暂停订单。

我的代码在我尝试付款时出现错误

add_action( 'woocommerce_order_status_on-hold', 'wc_put_order_onhold', 10, 3 );
function wc_put_order_onhold( $status, $order_id, $order ) {
    $product_ids   = array('19345');
    $product_found = true;
    
    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        if( ! array_intersect( $product_ids, array($item->get_product_id(), $item->get_variation_id()) ) ) {
            $product_found = false;
            break;
        }
    }

    return $product_found ? 'customstatus' : $status;
}

1 个答案:

答案 0 :(得分:0)

您的代码中存在错误,因为挂钩函数只有 2 个参数

有两种情况:

对于独家商品来自特定产品的订单

add_action( 'woocommerce_order_status_on-hold', 'change_order_status_conditionally', 10, 2 );
function change_order_status_conditionally( $order_id, $order ) {
    $product_ids  = array('19345'); // <== Here define your targeted products
    
    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        if( ! array_intersect( $product_ids, array($item->get_product_id(), $item->get_variation_id()) ) ) {
            return $status;
        }
    }
    return 'customstatus';
}

对于至少包含来自特定产品的商品的订单非独家

add_action( 'woocommerce_order_status_on-hold', 'change_order_status_conditionally', 10, 2 );
function change_order_status_conditionally( $order_id, $order ) {
    $product_ids = array('19345'); // <== Here define your targeted products
    
    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        if( array_intersect( $product_ids, array($item->get_product_id(), $item->get_variation_id()) ) ) {
            return 'customstatus';
        }
    }
    return $status;
}

代码位于活动子主题(或活动主题)的functions.php 文件中。经测试有效。