save_post_shop_order
アクション フックに追加のメタデータを保存する WooCommerce プラグインを開発しています。ここで、いくつかの条件に基づいて注文ステータスを「保留中」に変更するロジックを追加したいと考えています。save_post_shop_order
アクションフックでは、どのステータスでも注文ステータスが変わらないことがわかりました。
function save_order_data(int $post_id)
{
$nonce_name = isset($_POST['save_invoice_nonce']) ? $_POST['save_invoice_nonce'] : '';
$nonce_action = 'save_invoice';
if (!wp_verify_nonce($nonce_name, $nonce_action)) {
return;
}
if (!current_user_can('edit_shop_orders', $post_id)) {
return;
}
if (wp_is_post_autosave($post_id)) {
return;
}
if (wp_is_post_revision($post_id)) {
return;
}
$order = wc_get_order($post_id);
$order->update_status('pending'); // This command works but it seems order status is being overwritten maybe by WooCommerce to previous status
}
add_action('save_post_shop_order', 'save_order_data', PHP_INT_MAX);