WooCommerce:根据运输方式自动完成已付款订单
WooCommerce: autocomplete paid orders based on shipping method
我有一种产品,人们可以直接打印(运输方式 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' );
}
}
我想你要找的是 $order->has_shipping_method('name_of_method')
发件人:https://docs.woocommerce.com/wc-apidocs/class-WC_Abstract_Order.html#_has_shipping_method)
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.
新增强的重访代码(参见了解解释) 。
实例 ID 是什么:
例如,如果 Shipping method rate Id 是 flat_rate:14
,实例 Id 是 14
(unique 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 文件。已测试并有效。
原回答:
下面的答案来自我之前做过的类似回答:
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 文件。
经过测试并有效。
这是我的工作解决方案(感谢 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' );
}
}
我有一种产品,人们可以直接打印(运输方式 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' );
}
}
我想你要找的是 $order->has_shipping_method('name_of_method')
发件人:https://docs.woocommerce.com/wc-apidocs/class-WC_Abstract_Order.html#_has_shipping_method)
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 fromwp_postmeta
table and the 2nd one is aWC_Order
method that will also access the same data fromwp_postmeta
table…
No one is better than the other.
新增强的重访代码(参见
实例 ID 是什么:
例如,如果 Shipping method rate Id 是 flat_rate:14
,实例 Id 是 14
(unique 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 文件。已测试并有效。
原回答:
下面的答案来自我之前做过的类似回答:
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 文件。
经过测试并有效。
这是我的工作解决方案(感谢 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' );
}
}