I would like to write a little script that does a check whenever an order is created in WooCommerce (right after checkout) and then conditionally changes the Shipping Address of the customer.

I think I need a filter hook that ‘fires’ as soon as an order is created. I tried several things in the WooCommerce Hook reference guide, like: ‘woocommerce_create_order’, but without success. I also contacted WooCommerce support, but no solution. I also checked out: Woocommerce hook after order (on Stackexchange), however I would need a filter hook that fires before order create (not after).

What I would like to accomplish is something like:

function alter_shipping ($order) {
  if ($something == $condition) {
    $order->shipping_address = "..."; //(simplified)
  }
  return $order;
}
add_filter( 'woocommerce_create_order', 'alter_shipping', 10, 1 );

This filter ‘woocommerce_create_order’ in above example doesn’t pass any variables to be manipulated.

I need to conditionally manipulate the shipping address in a WooCommerce order as it gets created. Does anyone know a suitable filter hook for this? Or another way?

3 s
3

Stumbled on this looking for the same thing which I’ve now figured out (Woocommerce 3.x)…

add_filter( 'woocommerce_checkout_create_order', 'mbm_alter_shipping', 10, 1 );
function mbm_alter_shipping ($order) {

  if ($something == $condition) {
    $address = array(
      'first_name' => 'Martin',
      'last_name'  => 'Stevens',
      'company'    => 'MBM Studio',
      'email'      => 'test@test.com',
      'phone'      => '777-777-777-777',
      'address_1'  => '99 Arcadia Avenue',
      'address_2'  => '', 
      'city'       => 'London',
      'state'      => '',
      'postcode'   => '12345',
      'country'    => 'UK'
    );

    $order->set_address( $address, 'shipping' );

  }

  return $order;

}

Leave a Reply

Your email address will not be published. Required fields are marked *