skip to Main Content

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.

2

Answers


  1. // 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 );
    
    Login or Signup to reply.
  2. 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 );
    

    EDIT: it is a known issue that these hooks are called multiple times when they are not intended to be

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

    As a workaround, add:

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

    As first line in your callback function

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search