gpt4 book ai didi

php - WooCommerce:根据运输方式自动完成已付款订单

转载 作者:搜寻专家 更新时间:2023-10-31 21:48:56 26 4
gpt4 key购买 nike

我有一种产品,人们可以直接打印(运输方式 1)或选择通过运输服务(运输方式 2)获取它。因此,如果他们选择直接打印订单(送货方式 2),订单应该会自动完成。

是否可以从 WooCommerce 扩展该代码片段?

从我找到的文档 this

/**
* 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' );
}
}

最佳答案

First, get_post_meta($order_id, '_payment_method', true ) or $order->get_payment_method() are completely similar and do the same thing.
The difference is that the first one use a Wordpress function to access this data from wp_postmeta table and the 2nd one is a WC_Order method that will also access the same data from wp_postmeta table…
No one is better than the other.

新的增强型重访代码(参见 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 文件。经过测试并有效。


原答案:

下面的答案来 self 之前做过的类似回答:
WooCommerce: Auto complete paid orders

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 文件。

经过测试并有效。

关于php - WooCommerce:根据运输方式自动完成已付款订单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48303688/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com