【发布时间】:2021-04-30 02:05:03
【问题描述】:
通过以下代码,我在 WooCommerce 中添加了自定义订单状态(Abonnement)
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' );
然后我使用Change WooCommerce order status based on approved status and specific order item 回答代码进一步帮助我。
我修改了该答案以进行下一步:
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');
但这似乎不起作用。所以我不确定我错过了什么?
【问题讨论】:
标签: php wordpress woocommerce status orders