1

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?

0

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;

}
0

Try action hook woocommerce_checkout_create_order, it is called just before save to DB.

woocommerce_checkout_create_order

0

you got the right filter that arguments are wrong way:

woocommerce 3.0.6 doc

// define the woocommerce_create_order callback 
function filter_woocommerce_create_order( $null, $instance ) { 
    // make filter magic happen here... 
    return $null; 
}; 

// add the filter 
add_filter( 'woocommerce_create_order', 'filter_woocommerce_create_order', 10, 2 ); 

So your $order would always be null, then put the null replace of your $order, and let $order move to second params. Then return value must be $order_id or the try-catch function would stuck your snippets. There is full doc of this function.

fixed

function alter_shipping (null,$order) {
  if ($something == $condition) {
    $order->shipping_address = "..."; //(simplified)
  }
  $order_id = $order->save();

  return $order_id;
}
add_filter( 'woocommerce_create_order', 'alter_shipping', 10, 2);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.