Skip to content

Instantly share code, notes, and snippets.

@warnakey
Created August 7, 2021 07:38
Show Gist options
  • Save warnakey/acc86ad11912de82cbfd51b30bb0c203 to your computer and use it in GitHub Desktop.
Save warnakey/acc86ad11912de82cbfd51b30bb0c203 to your computer and use it in GitHub Desktop.
WooCommerce - Place Orders On Hold if billing and shipping information are different
<?php
/* PLACE ORDERS ON HOLD WHEN BILLING & SHIPPING INFO DO NOT MATCH */
add_action( 'woocommerce_thankyou', 'woocommerce_billing_shipping_address_match', 10, 1);
function woocommerce_billing_shipping_address_match( $order_id ) {
if ( ! $order_id ) {
return;
}
// Get the order id
$order = wc_get_order( $order_id );
// Customer billing information details
$billing_first_name = $order->get_billing_first_name();
$billing_last_name = $order->get_billing_last_name();
$billing_company = $order->get_billing_company();
$billing_address_1 = $order->get_billing_address_1();
$billing_address_2 = $order->get_billing_address_2();
$billing_city = $order->get_billing_city();
$billing_state = $order->get_billing_state();
$billing_postcode = $order->get_billing_postcode();
$billing_country = $order->get_billing_country();
// Customer shipping information details (from account)
$shipping_first_name = $order->get_shipping_first_name();
$shipping_last_name = $order->get_shipping_last_name();
$shipping_company = $order->get_shipping_company();
$shipping_address_1 = $order->get_shipping_address_1();
$shipping_address_2 = $order->get_shipping_address_2();
$shipping_city = $order->get_shipping_city();
$shipping_state = $order->get_shipping_state();
$shipping_postcode = $order->get_shipping_postcode();
$shipping_country = $order->get_shipping_country();
// check if any the billing info does not match the shipping info and place on hold if not
if($billing_first_name !== $shipping_first_name || $billing_last_name !== $shipping_last_name || $billing_address_1 !== $shipping_address_1 || $billing_address_2 !== $shipping_address_2 || $billing_city !== $shipping_city || $billing_state !== $shipping_state || $billing_postcode !== $shipping_postcode || $billing_country !== $shipping_country) {
// Place the order on hold
$order->update_status( 'on-hold' );
}
}
@warnakey
Copy link
Author

warnakey commented Aug 7, 2021

I am extremely proud of this function

@charliesjc
Copy link

I've been running a function on my wife's site that sends order straight to on-hold so she can customise the meta for the order before moving it onto processing. (She's an artist so it helps to assign the exact artwork to the order that the client will receive). Anyway, I ended up not using the woocommerce_thankyou hook because an order still goes to "processing" and then to "on-hold", so emails are sent out twice (once for processing and once for on-hold). I instead used the 'woocommerce_payment_complete_order_status' hook to intercept the changeover before emails are sent etc. I'm assuming that if you've already used the woocommerce_thankyou hook then a payment has already been made? I also made the on-hold orders show up with a badge so you can still see there was an order, just like when they go to processing. Let me know if you're keen and I'll put in into a gist. I need to get around to it anyway.

@warnakey
Copy link
Author

warnakey commented Aug 7, 2021

I've been running a function on my wife's site that sends order straight to on-hold so she can customise the meta for the order before moving it onto processing. (She's an artist so it
I've been running a function on my wife's site that sends order straight to on-hold so she can customise the meta for the order before moving it onto processing. (She's an artist so it helps to assign the exact artwork to the order that the client will receive). Anyway, I ended up not using the woocommerce_thankyou hook because an order still goes to "processing" and then to "on-hold", so emails are sent out twice (once for processing and once for on-hold). I instead used the 'woocommerce_payment_complete_order_status' hook to intercept the changeover before emails are sent etc. I'm assuming that if you've already used the woocommerce_thankyou hook then a payment has already been made? I also made the on-hold orders show up with a badge so you can still see there was an order, just like when they go to processing. Let me know if you're keen and I'll put in into a gist. I need to get around to it anyway.

Yes please! I only want the email to go out after someone manually changes the order status to processing, so I would love to see your solution. Thank you

@charliesjc
Copy link

charliesjc commented Aug 7, 2021

Here you go: https://gist.github.com/charliesjc/b3bfa1e2c35bd3c21f426c05b1db8234.

So I would add this:
if ($order_status == 'processing') { $order_status = 'on-hold'; }
in your last if statement and then return the $order_status at the end of the function. The only real difference to your original code is the hook.

@charliesjc
Copy link

I haven't got a clue why my link says one thing and then links straight back to your original gist.

@aaron843
Copy link

aaron843 commented Aug 7, 2021

Here you go: https://gist.github.com/charliesjc/b3bfa1e2c35bd3c21f426c05b1db8234.

So I would add this:
if ($order_status == 'processing') { $order_status = 'on-hold'; }
in your last if statement and then return the $order_status at the end of the function. The only real difference to your original code is the hook.

Your link text and link don't match. This will work:

https://gist.github.com/charliesjc/b3bfa1e2c35bd3c21f426c05b1db8234

@charliesjc
Copy link

Here you go: https://gist.github.com/charliesjc/b3bfa1e2c35bd3c21f426c05b1db8234.
So I would add this:
if ($order_status == 'processing') { $order_status = 'on-hold'; }
in your last if statement and then return the $order_status at the end of the function. The only real difference to your original code is the hook.

Your link text and link don't match. This will work:

https://gist.github.com/charliesjc/b3bfa1e2c35bd3c21f426c05b1db8234

I know. I even said so in my previous comment. I haven't got a clue why it's doing that. The link that's in the message is exactly the same as the text, but it's ignoring it completely and linking back to this gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment