【问题标题】:Change WooCommerce "on-hold" orders to a custom status for specific products将 WooCommerce“暂停”订单更改为特定产品的自定义状态
【发布时间】:2021-05-23 05:17:14
【问题描述】:

我需要有关此代码的帮助,如果它包含 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;
}

【问题讨论】:

  • 请对以下答案提供一些反馈。

标签: php wordpress woocommerce arguments orders


【解决方案1】:

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

有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 文件中。经过测试并且可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-01
    • 2021-04-16
    • 2018-08-30
    • 1970-01-01
    • 2019-10-09
    • 2021-01-24
    • 1970-01-01
    • 2019-11-12
    相关资源
    最近更新 更多