【问题标题】:WooCommerce email based on shipping zone基于运输区域的 WooCommerce 电子邮件
【发布时间】:2018-06-26 11:49:58
【问题描述】:

当客户选择送货zone id = 0(世界其他地区)时,我需要发送电子邮件说明。

我在下面找到了代码,但它基于付款方式:

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>';
    }
  }
}

请问如何将其更改为特定的运输区域?
如何在 WooCommerce 中根据送货区域设置特定的电子邮件说明?

我们将不胜感激。

【问题讨论】:

    标签: php wordpress woocommerce shipping email-notifications


    【解决方案1】:

    获取订单的送货区域并非易事。可以通过以下代码示例完成:

    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>';
            }
        }
    }
    

    代码进入活动子主题(或活动主题)的 function.php 文件中。

    经过测试并且有效。

    【讨论】:

      【解决方案2】:

      根据WC_Abstract_Order codex page https://docs.woocommerce.com/wc-apidocs/class-WC_Abstract_Order.html#_has_shipping_method,您可以使用has_shipping_method()查看运输方式。

      所以,你的 if 语句应该是这样的

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

      不过,我找不到可配送区域的等价物。

      希望有帮助

      【讨论】:

        【解决方案3】:

        add_action('woocommerce_email_before_order_table', 'custom_text_in_email_shipping_zone_based', 10, 4); 功能 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>';
            }
        }
        

        }

        上面显示的代码虽然作者已经说过尝试和测试,但仍然无法正常工作,任何人都可以更新代码。

        无论您选择哪个区域,您总是以所有其他区域结束。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-01-06
          • 2019-04-23
          • 1970-01-01
          • 2014-06-18
          • 2011-06-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多