最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Filter hook before create order WooCommerce

programmeradmin5浏览0评论

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?

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?

Share Improve this question asked Jul 19, 2017 at 17:11 Lucas JohnstonLucas Johnston 731 gold badge1 silver badge7 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 6

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'      => '[email protected]',
      '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;

}

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

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

woocommerce_checkout_create_order

发布评论

评论列表(0)

  1. 暂无评论