我開始遇到操作問題,woocommerce_order_status_changed并且從未觸發過我的任何自定義狀態。
我發現只有在不為假時才在class-wc-order.php中使用該鉤子。$status_transition
if ( $status_transition ) {
try {
do_action( 'woocommerce_order_status_' . $status_transition['to'], $this->get_id(), $this );
if ( ! empty( $status_transition['from'] ) ) {
/* translators: 1: old order status 2: new order status */
$transition_note = sprintf( __( 'Order status changed from %1$s to %2$s.', 'woocommerce' ), wc_get_order_status_name( $status_transition['from'] ), wc_get_order_status_name( $status_transition['to'] ) );
// Note the transition occurred.
$this->add_status_transition_note( $transition_note, $status_transition );
do_action( 'woocommerce_order_status_' . $status_transition['from'] . '_to_' . $status_transition['to'], $this->get_id(), $this );
do_action( 'woocommerce_order_status_changed', $this->get_id(), $status_transition['from'], $status_transition['to'], $this );
// Etc...
我制作的所有自定義狀態都有效,并顯示在 WooCommerce 管理頁面上。為了添加我使用的狀態:
function register_statuses()
{
register_post_status('wc-payment-await', array(
'label' => 'Awaiting payment',
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop('Awaiting payment <span >(%s)</span>', 'Awaiting payment <span >(%s)</span>', 'woocommerce')
));
// more statuses here
}
add_action('init', 'register_statuses');
我發現$status_transition每當我以新狀態保存訂單時,這總是錯誤的。在我最近升級 WooCommerce 之前,此功能一直有效。我還嘗試了所有其他用于狀態更改或轉換的鉤子,但都沒有奏效。任何建議表示贊賞。
uj5u.com熱心網友回復:
要添加自定義訂單狀態,我重寫了您的代碼,因為:
init鉤子已被替換woocommerce_register_shop_order_post_statuses為注冊自定義訂單狀態。wc_order_statuses添加以在下拉串列中顯示訂單狀態@單筆訂單bulk_actions-edit-shop_order添加以在下拉串列中顯示訂單狀態@批量操作
// Register order status
function filter_woocommerce_register_shop_order_post_statuses( $order_statuses ) {
// Status must start with "wc-"
$order_statuses['wc-payment-await'] = array(
'label' => _x( 'Awaiting payment', '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( 'Awaiting payment <span >(%s)</span>', 'Awaiting payment <span >(%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 ) {
$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 ) {
// Status must start with "wc-"
$new_order_statuses['wc-payment-await'] = _x( 'Awaiting payment', 'Order status', 'woocommerce' );
}
}
return $new_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_payment-await'] = __( 'Awaiting payment', 'woocommerce' );
return $bulk_actions;
}
add_filter( 'bulk_actions-edit-shop_order', 'filter_bulk_actions_edit_shop_order', 10, 1 );
關于您的問題:“woocommerce_order_status_changed我的任何自定義訂單狀態都不會觸發。”
如果您使用以下代碼,您將看到結果,該$new_status變數包含您的自定義訂單狀態,因此您知道觸發了掛鉤
function action_woocommerce_order_status_changed( $order_id, $old_status, $new_status, $order ) {
var_dump( $old_status );
var_dump( $new_status );
die();
}
add_action( 'woocommerce_order_status_changed', 'action_woocommerce_order_status_changed', 10, 4 );
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/439952.html
