如果管理员更新 WooCommerce 中的 shipping/billing 地址,需要调用哪个挂钩?

Which hook needs to call if admin update the shipping/billing address in WooCommerce?

我不确定哪个 hook/action 需要设置才能知道管理员何时在创建订单后更新 shipping/billing 地址。

所以我想在这里实现的是:

  1. 在 WooCommerce 订单部分,当管理员更新 shipping/billing 地址时,它会触发一个操作。
  2. 此操作基本上是对我的自定义脚本进行一次 curl 调用,让我知道订单地址已被管理员更改。
  3. 我会在脚本中施展魔法。

我在下面找到了,但我不认为它更多来自管理方面。

// define the woocommerce_admin_order_data_after_shipping_address callback 
function action_woocommerce_admin_order_data_after_shipping_address( 
 $delta_wccs_custom_checkout_details_pro_shipping, $int, $int ) { 
// make action magic happen here... 
}; 
     
// add the action 
add_action( 'woocommerce_admin_order_data_after_shipping_address',  'action_woocommerce_admin_order_data_after_shipping_address', 10, 3 ); 

如果有人知道订单 shipping/billing 地址更改时触发的正确操作,请告诉我。

// Define the woocommerce_admin_order_data_after_shipping_address callback .
function action_woocommerce_admin_order_data_after_shipping_address( $order ) { 
   // This hook will only fire in backend when viewing the order edit screen. Not for orders placed from checkout
}; 
     
// add the action 
add_action( 'woocommerce_admin_order_data_after_shipping_address',  'action_woocommerce_admin_order_data_after_shipping_address', 10, 1 );

woocommerce_admin_order_data_after_shipping_address钩子是为了在订单编辑页面(后端)显示额外的内容


要在保存到数据库之前或之后触发 $order_item 操作,请使用:

/**
 * Trigger action before saving to the DB. Allows you to adjust object props before save.
 *
 * @param WC_Data          $this The object being saved.
 * @param WC_Data_Store_WP $data_store THe data store persisting the data.
 */
function action_woocommerce_before_order_item_object_save( $order_item, $data_store ) {
    // Get type
    $data_type = $order_item->get_type();

    // Before billing changes
    if ( $data_type == 'billing' ) {
        // Do..
    }

    // Before shipping changes
    if ( $data_type == 'shipping' ) {
        // Do..
    }
}
add_action( 'woocommerce_before_order_item_object_save', 'action_woocommerce_before_order_item_object_save', 10, 2 ); 

/**
 * Trigger action after saving to the DB.
 *
 * @param WC_Data          $this The object being saved.
 * @param WC_Data_Store_WP $data_store THe data store persisting the data.
 */
function action_woocommerce_after_order_item_object_save( $order_item, $data_store ) {    
    // Get type
    $data_type = $order_item->get_type();

    // After billing changes
    if ( $data_type == 'billing' ) {
        // Do..
    }

    // After shipping changes
    if ( $data_type == 'shipping' ) {
        // Do..
    }
}
add_action( 'woocommerce_after_order_item_object_save', 'action_woocommerce_after_order_item_object_save', 10, 2 ); 

使用几乎相同的 woocommerce_before_order_object_save 挂钩可能更合适,因为通过 $order->get_changes() 您可以 trigger/log/compare 更改了哪些 $order 数据

function action_woocommerce_before_order_object_save( $order, $data_store ) {
    // Get changes
    $changes = $order->get_changes();

    // Billing OR shipping
    if ( isset( $changes['billing'] ) || isset( $changes['shipping'] ) ) {
        // Do..
    }

    // OR even more specific (e.g.: shipping first name field was changed)
    if ( isset( $changes['shipping_first_name'] ) ) {
        // Do..
    }
}
add_action( 'woocommerce_before_order_object_save', 'action_woocommerce_before_order_object_save', 10, 2 );

编辑: 一个已知问题是,这些挂钩在不打算被调用时被多次调用

See: https://github.com/woocommerce/woocommerce/issues/25771

作为解决方法,添加:

if ( did_action( 'replace_by_the_desired_hook_name' ) >= 2 ) return;

作为回调函数的第一行