Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if "Ship to different address" has been checked in Woocommerce

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?

like image 845
Neophyte Avatar asked Dec 11 '22 09:12

Neophyte


2 Answers

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

like image 86
LoicTheAztec Avatar answered Jan 05 '23 07:01

LoicTheAztec


ahhh.. we can just check if $_POST['ship_to_different_address'] is set..

like image 33
Neophyte Avatar answered Jan 05 '23 07:01

Neophyte