WooCommerce email based on shipping zone

丶灬走出姿态 提交于 2019-12-19 10:26:18

问题


I need send email instructions when customer select shipping zone id = 0 (The rest of the world).

I found code below, but its based on payment method:

add_action( 'woocommerce_email_before_order_table', 'add_order_email_instructions', 10, 2 );

function add_order_email_instructions( $order, $sent_to_admin ) {

  if ( ! $sent_to_admin ) {

    if ( 'cod' == $order->payment_method ) {
      // cash on delivery method
      echo '<p><strong>Instructions:</strong> Full payment is due immediately upon delivery: <em>cash only, no exceptions</em>.</p>';
    } else {
      // other methods (ie credit card)
      echo '<p><strong>Instructions:</strong> Please look for "Madrigal Electromotive GmbH" on your next credit card statement.</p>';
    }
  }
}

How can I change it to specific shipping zone please?
How can I set specific email instructions based on shipping zone in WooCommerce?

Any help on this will be appreciated.


回答1:


Get the shipping zone for an order is not so easy. It can be done with the following code example:

add_action( 'woocommerce_email_before_order_table', 'custom_text_in_email_shipping_zone_based', 10, 4 );
function custom_text_in_email_shipping_zone_based( $order, $sent_to_admin, $plain_text, $email ) {
    if ( ! $sent_to_admin ) {

        // Get the shipping method related data (we need the Instance ID)
        $shipping_item        = $order->get_items('shipping');
        $item = reset($shipping_item);
        $shipping_method_id   = $item->get_method_id();
        $method_arr           = explode( ':', $shipping_method_id );

        // Get the Zone ID and related data
        $shipping_zone_object = WC_Shipping_Zones::get_zone_by( 'instance_id', $method_arr[1] );
        $zone_id              = $shipping_zone_object->get_id();        // Zone ID
        $zone_name            = $shipping_zone_object->get_zone_name(); // Zone name
        // Get the zone locations codes and types (if needed)
        foreach( $shipping_zone_object->get_zone_locations() as $zone_location ){
            $zone_location_code = $zone_location->code;
            $zone_location_type = $zone_location->type;
        }

        if ( '0' == $zone_id ) {
            // Rest of the world
            echo '<p><strong>Instructions:</strong> for Rest of the world.</p>';
        }
        elseif ( 'Mexico' == $zone_name ) {
            // Mexico zone name
            echo '<p><strong>Instructions:</strong> for Mexico.</p>';
        }
        else {
            // All other zones
            echo '<p><strong>Instructions:</strong> Other zones.</p>';
        }
    }
}

Code goes in function.php file of the active child theme (or active theme).

Tested and works.




回答2:


According to the WC_Abstract_Order codex page https://docs.woocommerce.com/wc-apidocs/class-WC_Abstract_Order.html#_has_shipping_method you can check the shipping method using has_shipping_method().

So, your if statement should be something like

if ( $order->has_shipping_method('name_of_my_shipping_method') ) {

I couldn't find an equivalent for shipping zones though.

Hope that helps



来源:https://stackoverflow.com/questions/48303802/woocommerce-email-based-on-shipping-zone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!