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

Time:04-30

I'm not sure which hook/action needs to setup to know when the admin is updating shipping/billing addresses once the order has been created.

So what I'm trying to achieve here is:

  1. In WooCommerce order section when the admin updates the shipping/billing address then it triggers an action.
  2. this action basically makes a single curl call to my custom script and lets me know that the address of the order has been changed by the admin.
  3. I'll do some magic in my script.

I found below but I don't think its more from admin side.

// 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 ); 

Please let me know if anyone knows the right action to trigger when order shipping/billing address change.

CodePudding user response:

// 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 );

CodePudding user response:

The woocommerce_admin_order_data_after_shipping_address hook is to display extra content on the order edit page (backend)


To trigger $order_item actions before or after saving to the DB, use:

/**
 * 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 ); 

OR

Use the almost identical woocommerce_before_order_object_save hook that may be even more suitable, because via $order->get_changes() you can trigger/log/compare which $order data has been changed

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 );
  • Related