如果 WooCommerce 订单包含来自某个类别的项目,则设置自定义状态 [英] Set custom status if WooCommerce order has items from a certain category
问题描述
通过以下代码,我在 WooCommerce 中添加了自定义订单状态(Abonnement)
Via the following code I added a custom order status (Abonnement) in WooCommerce
function register_abonnement_order_status() {
register_post_status( 'wc-abonnement', array(
'label' => 'Abonnement',
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'abonnement (%s)', 'abonnement (%s)' )
) );
}
add_action( 'init', 'register_abonnement_order_status' );
// Add to list of WC Order statuses
function add_abonnement_to_order_statuses( $order_statuses ) {
$new_order_statuses = array();
// add new order status after processing
foreach ( $order_statuses as $key => $status ) {
$new_order_statuses[ $key ] = $status;
if ( 'wc-processing' === $key ) {
$new_order_statuses['wc-abonnement'] = 'Abonnement';
}
}
return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'add_abonnement_to_order_statuses' );
然后我使用根据批准状态和特定订单项目更改 WooCommerce 订单状态 答案代码来进一步帮助我.
I then used Change WooCommerce order status based on approved status and specific order item answer code to help me further.
我修改了该答案以进入下一步:
I modified that answer to go to the next step:
function action_woocommerce_order_status_changed( $order_id, $old_status, $new_status, $order ) {
// Compare
if( $old_status === 'processing' ) {
// Get items
$items = $order->get_items();
foreach ( $items as $item ) {
// Get products categories
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat_id = $term->term_id;
break;
}
$product_cat = $item->get_product_cat();
if ($product_cat == 249 ) {
$order->update_status( 'abonnement' );
break;
}
}
}
}
add_action( 'woocommerce_order_status_changed', 'action_woocommerce_order_status_changed');
但这似乎不起作用.所以我不确定我错过了什么?
But that doesn't seems to work. So i'm not sure what I'm missing?
推荐答案
- 通过代码中添加的注释标签进行说明
要在每个订单后自动更改订单状态,当订单包含属于某些类别的商品时,请使用:
To automatically change the order status after each order, when the order contains items that belong to certain categories, use:
/**
* Change Order Status
*/
function action_woocommerce_thankyou( $order_id ) {
if ( ! $order_id ) return;
// Get order object
$order = wc_get_order( $order_id );
// Specific categories: the term name/term_id/slug. Several could be added, separated by a comma
$categories = array( 'categorie-1', 'categorie-2', 15, 16 );
// Flag
$found = false;
// Loop through order items
foreach ( $order->get_items() as $item ) {
// Product ID
$product_id = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();
// Has term (product category)
if ( has_term( $categories, 'product_cat', $product_id ) ) {
$found = true;
break;
}
}
// True
if ( $found ) {
// Status without the "wc-" prefix || Some options: pending, processing, on-hold, completed, cancelled, refunded, failed, etc...
$order->update_status( 'abonnement' );
}
}
add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 );
OR 使用 woocommerce_order_status_changed
钩子,您可以在其中定位您的订单状态转换 from
和 to
,以更改任何其他订单状态.
OR using woocommerce_order_status_changed
hook where you can target your orders statuses transition from
and to
, to change the order status to any other.
function action_woocommerce_order_status_changed( $order_id, $old_status, $new_status, $order ) {
// Compare
if ( $old_status === 'processing' ) {
// Specific categories: the term name/term_id/slug. Several could be added, separated by a comma
$categories = array( 'categorie-1', 'categorie-2', 15, 16 );
// Flag
$found = false;
// Loop through order items
foreach ( $order->get_items() as $item ) {
// Product ID
$product_id = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();
// Has term (product category)
if ( has_term( $categories, 'product_cat', $product_id ) ) {
$found = true;
break;
}
}
// True
if ( $found ) {
// Status without the "wc-" prefix || Some options: pending, processing, on-hold, completed, cancelled, refunded, failed, etc...
$order->update_status( 'abonnement' );
}
}
}
add_action( 'woocommerce_order_status_changed', 'action_woocommerce_order_status_changed', 10, 4 );
可选:要注册新的订单状态,您可以使用此更新后的代码替换当前代码
Optional: to register a new order status, you can replace your current code with this updated code
/**
* Register Order Status
*/
function filter_woocommerce_register_shop_order_post_statuses( $order_statuses ) {
// Status must start with "wc-"
$order_statuses['wc-abonnement'] = array(
'label' => _x( 'Abonnement', 'Order status', 'woocommerce' ),
'public' => false,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
/* translators: %s: number of orders */
'label_count' => _n_noop( 'Abonnement <span class="count">(%s)</span>', 'Abonnement <span class="count">(%s)</span>', 'woocommerce' ),
);
return $order_statuses;
}
add_filter( 'woocommerce_register_shop_order_post_statuses', 'filter_woocommerce_register_shop_order_post_statuses', 10, 1 );
/**
* Show Order Status in the Dropdown @ Single Order
*/
function filter_wc_order_statuses( $order_statuses ) {
// Status must start with "wc-"
$order_statuses['wc-abonnement'] = _x( 'Abonnement', 'Order status', 'woocommerce' );
return $order_statuses;
}
add_filter( 'wc_order_statuses', 'filter_wc_order_statuses', 10, 1 );
/**
* Show Order Status in the Dropdown @ Bulk Actions
*/
function filter_bulk_actions_edit_shop_order( $bulk_actions ) {
// Note: "mark_" must be there instead of "wc"
$bulk_actions['mark_abonnement'] = __( 'Change status to abonnement', 'woocommerce' );
return $bulk_actions;
}
add_filter( 'bulk_actions-edit-shop_order', 'filter_bulk_actions_edit_shop_order', 10, 1 );
这篇关于如果 WooCommerce 订单包含来自某个类别的项目,则设置自定义状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!