WooCommerce:根据配送方式自动完成付款订单

4
我有一个产品,人们可以直接打印(配送方式1),或选择通过运输服务获取(配送方式2)。因此,如果他们选择直接打印(配送方式2),订单应自动完成。
从WooCommerce的文档中发现一些代码片段,是否可能将其扩展?
在文档中找到了此链接
/**
 * Auto Complete all WooCommerce orders.
 */
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order');
function custom_woocommerce_auto_complete_order( $order_id ) {
    if ( ! $order_id ) {
        return;
    }

    $order = wc_get_order( $order_id );
    $order->update_status( 'completed' );
}

这里是可行的解决方案。非常感谢 LoicTheAztec 帮助:

add_action( 'woocommerce_thankyou', 
'wc_auto_complete_paid_order_based_on_shipping_method', 20, 1 );
function wc_auto_complete_paid_order_based_on_shipping_method( $order_id ) {
if ( ! $order_id ) return;

// HERE define the allowed shipping methods IDs (can be names or slugs changing the code a bit)
$allowed_shipping_methods = array( '5' );

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

// Get the shipping related data for this order:
$shipping_item = $order->get_items('shipping');
$item = reset($shipping_item);
$item_data = $item->get_data();

// Get the shipping method name, rate ID and type slug
$method_rate_id = $item_data['instance_id'];  // Shipping method ID

// No updated status for orders delivered with Bank wire, Cash on 
delivery and Cheque payment methods.
$avoided_statuses = array( 'bacs', 'cod', 'cheque');
if ( in_array( $order->get_payment_method(), $avoided_statuses ) ){
return;
}
// update status to "completed" for paid Orders and a defined shipping 
method ID
elseif ( in_array( $method_rate_id, $allowed_shipping_methods ) ){
$order->update_status( 'completed' );
}
}
3个回答

0
首先,get_post_meta($order_id, '_payment_method', true ) 或者 $order->get_payment_method() 完全相同,并且执行的是同样的操作
区别在于第一个使用WordPress函数从wp_postmeta表中访问此数据,而第二个是WC_Order方法,也将从wp_postmeta表中访问相同的数据...
没有哪一个比另一个更好。

新增强版代码(请参见this thread以获取说明)

实例ID是什么:
例如,如果运输方式费率ID为flat_rate:14,则实例ID为14(唯一ID)

新版本代码:

add_action( 'woocommerce_payment_complete_order_status', 'auto_complete_paid_order_based_on_shipping_method', 10, 3 );
function auto_complete_paid_order_based_on_shipping_method( $status, $order_id, $order ) {
    // HERE define the allowed shipping methods instance IDs
    $allowed_shipping_methods_instance_ids = array( '14', '19' );
    
    // Loop through order "shipping" items
    foreach ( $order->get_shipping_methods() as $shipping_method ) {
        if( in_array( $shipping_method->get_instance_id(), $allowed_shipping_methods_instance_ids ) ) {
            return 'completed';
        }
    }
    return $status;
}

代码放在活动子主题(或活动主题)的function.php文件中。已测试并可用。


原始答案:

下面的答案来自我之前发布的类似答案:
WooCommerce:自动完成付款订单

add_action( 'woocommerce_thankyou', 'auto_complete_paid_order_based_on_shipping_method', 10, 1 );
function auto_complete_paid_order_based_on_shipping_method( $order_id ) {
    if ( ! $order_id ) return;

    // HERE define the allowed shipping methods IDs (can be names or slugs changing the code a bit)
    $allowed_shipping_methods = array( 'flat_rate:14', 'flat_rate:19' );

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

    // Get the shipping related data for this order:
    $shipping_item = $order->get_items('shipping');
    $item = reset($shipping_item);
    $item_data = $item->get_data();

    // Get the shipping method name, rate ID and type slug
    $shipping_name = $item_data['name']; // Shipping method name
    $method_rate_id = $item_data['method_id'];  // Shipping method ID
    $method_arr = explode( ':', $method_rate_id );
    $method_type = $method_arr[0];  // Shipping method type slug

    // No updated status for orders delivered with Bank wire, Cash on delivery and Cheque payment methods.
    $avoided_statuses = array( 'bacs', 'cod', 'cheque');
    if ( in_array( $order->get_payment_method(), $avoided_statuses ) ){
        return;
    }
    // update status to "completed" for paid Orders and a defined shipping method ID
    elseif ( in_array( $method_rate_id, $allowed_shipping_methods ) ){
        $order->update_status( 'completed' );
    }
}

代码放在活动子主题(或活动主题)的function.php文件中。

已测试并可用。


太棒了! :) 你能告诉我一件事吗?什么是“运输方式类型别名”?你写道:“//在这里定义避免的运输方式”,然后将其命名为$allowed_shipping...这正确吗? - Tob S
1
谢谢 :)。您在代码中的第一个注释是 "// HERE define the avoided shipping...",但我认为您在那里定义了“允许”的运输方法,这会将状态更新为完成? - Tob S
1
祝你晚上愉快 :)! - Tob S
嗨@LoicTheAztec,使用我的特定运输方式进行订单自动完成:“$allowed_shipping_methods = array('flat_rate:5');”,如果有人通过PayPal购买,则不再起作用。通过PayPal支付并使用我上面定义的特定方法后,状态为“处理中”。我确定以前它是有效的。WC是否更改了任何内容?谢谢:)! - Tob S
抱歉,我不知道stackoverflow如何处理事情。我再次接受了你的请求。 - Tob S
显示剩余5条评论

0

这是我的工作解决方案(感谢LoicTheAztec)

add_action( 'woocommerce_thankyou', 
    'wc_auto_complete_paid_order_based_on_shipping_method', 20, 1 );
function wc_auto_complete_paid_order_based_on_shipping_method( $order_id ) {
    if ( ! $order_id ) return;

    // HERE define the allowed shipping methods IDs (can be names or slugs changing the code a bit)
    $allowed_shipping_methods = array( '5' );

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

    // Get the shipping related data for this order:
    $shipping_item = $order->get_items('shipping');
    $item = reset($shipping_item);
    $item_data = $item->get_data();

    // Get the shipping method name, rate ID and type slug
    $method_rate_id = $item_data['instance_id'];  // Shipping method ID

    // No updated status for orders delivered with Bank wire, Cash on delivery and Cheque payment methods.
    $avoided_statuses = array( 'bacs', 'cod', 'cheque');
    if ( in_array( $order->get_payment_method(), $avoided_statuses ) ){
        return;
    }
    // update status to "completed" for paid Orders and a defined shipping method ID
    elseif ( in_array( $method_rate_id, $allowed_shipping_methods ) ){
        $order->update_status( 'completed' );
    }
}

就像我之前说的一样。抱歉,我以为这是其他用户使用的方式。 - Tob S

0

不完全是。更像是:如果($order->has_shipping_method('bacs')|| $order->has_shipping_method('cod')|| $order->has_shipping_method('cheque')){ - Peter HvD
BACS、COD和支票是付款方式。我只添加了运输前缀。付款方式应该按照这种方式工作... - Tob S
当然...噢!在这种情况下,您应该使用'bacs' == $order->get_payment_method()而不是'bacs' == get_post_meta($order_id, '_payment_method'等。 - Peter HvD

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接