Reputation: 143
In WwooCommerce, I am trying to add the ship to different address information in my admin email.
How can I check if the checkbox to ship to different address from checkout page is checked?
I tried to use:
$ship_to_different_address = get_option( 'woocommerce_ship_to_destination' ) === 'shipping' ? 1 : 0;
if($ship_to_different_address == 1):
//the additional email text here
endif;
But this seems not working. Any ideas?
Upvotes: 5
Views: 7450
Reputation: 5281
I needed the same kind of address checking and programmed myself a really good working solution which respects custom billing/shipping fields:
/**
* Verify if the shipping address is different
*
* @param WC_Order $order
*
* @return bool
*/
function is_different_shipping_address( WC_Order $order ): bool {
$billing_address = $order->get_address();
$shipping_address = $order->get_address( 'shipping' );
if ( ! empty( $billing_address ) && ! empty( $shipping_address ) ) {
foreach ( $billing_address as $billing_address_key => $billing_address_value ) {
if ( isset( $shipping_address[ $billing_address_key ] ) ) {
$shipping_address_value = $shipping_address[ $billing_address_key ];
if ( ! empty( $billing_address_value ) && ! empty( $shipping_address_value ) && strcmp( $billing_address_value, $shipping_address_value ) !== 0 ) {
return true;
}
}
}
}
return false;
}
First I'm requesting both addresses from the order object. After that I'm looping over the billing address by default. Now I'm getting the same value from the shipping address (if set) and compare both values. If they are different, I'm returning true, else false.
Hoping it helps someone too.
Upvotes: 1
Reputation: 254363
May be the best way is to emulate it comparing for the order the billing and the shipping addresses. In most of all available related email notification hooks, the $order
object is included as a parameter.
Here is an example with this function hooked in woocommerce_email_order_details
action hook, that will display something different depending on that:
add_action( 'woocommerce_email_order_details', 'custom_content_email_order_details', 10, 4 );
function custom_content_email_order_details( $order, $sent_to_admin, $plain_text, $email ){
// Only for "New Order" and admin email notification
if ( 'new_order' != $email->id && ! $sent_to_admin ) return;
// Displaying something related
if( $order->get_billing_address_1() != $order->get_shipping_address_1() ) {
echo '<p style="color:red;">Different billing and shipping addresses<p>';
} else {
echo '<p style="color:green;">Same billing and shipping addresses<p>';
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested in WooCommerce 3.1+ and works
You can also use (with different priorities) any of the following hooks in this code:
-woocommerce_email_before_order_table
-woocommerce_email_after_order_table
-woocommerce_email_order_meta
-woocommerce_email_customer_details
Upvotes: 9
Reputation: 143
ahhh.. we can just check if $_POST['ship_to_different_address']
is set..
Upvotes: 7