【问题标题】:Woocommerce new order notification to different email based on shipping countryWoocommerce 新订单通知根据发货国家发送到不同的电子邮件
【发布时间】:2014-05-08 23:44:09
【问题描述】:

这几天我一直在为此烦恼。场景是 1 家商店有 2 个地点,因此根据结帐时选择的送货国家/地区,新订单通知需要发送到相关的电子邮件地址。

我得到了这个工作....部分。

在测试阶段,为了完成订单,我使用了“直接银行转账”付款方式。下面的代码非常适合这种付款方式,但我后来发现,当通过贝宝下订单时,它不再有效。

此外,如果您将产品更改为虚拟产品,那么它可以与 paypal 一起使用,但只要您添加

add_filter( 'woocommerce_cart_needs_shipping', '__return_true' );

恢复运输选项,然后它再次停止工作。

这是我目前的代码

    global $woocommerce;
if (strtolower($woocommerce->customer->get_shipping_country()) === 'us') {
$this->send( 'shopone@gmail.com', $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
} else {
$this->send( 'shoptwo@gmail.com', $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
};

希望有人能发现我遗漏的明显东西,提前谢谢!

更新: Zapier 工作但有一些限制,我仍然想找到一个不需要第三方应用程序的解决方案。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    找到了解决办法。

    对于以后遇到同样问题的人,在 class-wc-email-new-order.php 中替换

    $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
    

    if ($this->object->shipping_country === 'US') {
    $this->send( 'shopeone@gmail.com', $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
    } else { 
    $this->send( 'shoptwo@email.com', $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
    }
    

    问题是使用 $woocommerce 全局变量从客户那里获取价值,而您应该从订单本身获取价值。

    如果您想根据不同的结帐字段创建条件,您可以在 class-wc-api-orders.php 中找到列表

    只需替换

     $order->shipping_country
    

    $this->object->shipping_country
    

    【讨论】:

      【解决方案2】:

      还有另一种方法可以使用名为Zapier 的网站来完成此操作。它允许您链接多个 API,其中之一是 WooCommerce。您需要做的就是创建一个触发器,如果​​发货国家/地区是美国,它将向 shopone@gmail.com 发送电子邮件,如果不是,则无论付款方式如何,都会向 shoptwo@gmail.com 发送电子邮件。

      【讨论】:

      • 感谢 zapier 完成了我需要做的事情!
      • 感谢您的建议,我认为它解决了我的问题,但 zapier 对我的需求很重要的实际电子邮件内容不够灵活。
      【解决方案3】:

      这比我见过的任何其他选项都好用,谢谢! 我将shipping_country 更改为billing_state

      是否可以添加两个以上的变体?让我们这样说:

          if ($this->object->billing_state === 'SC') {
          $this->send( 
              'sc@gmail.com', 
              $this->get_subject(),     
              $this->get_content(), 
              $this->get_headers(), 
              $this->get_attachments()
          ); 
      }
      
          if ($this->object->billing_state === 'RS') {
              $this->send( 
                  'rs@gmail.com', 
                  $this->get_subject(),
                  $this->get_content(), 
                  $this->get_headers(), 
                  $this->get_attachments() 
              );
          } 
      
      if ($this->object->billing_state === 'PR') {
              $this->send( 
                  'pr@gmail.com', 
                  $this->get_subject(),
                  $this->get_content(), 
                  $this->get_headers(), 
                  $this->get_attachments() 
              );
          } 
      
      
          else { 
              $this->send( 
                  'else@gmail.com',
                  $this->get_subject(), 
                  $this->get_content(), 
                  $this->get_headers(), 
                  $this->get_attachments() 
              );
          }
      

      【讨论】:

      • 我认为缺少一个关闭的刹车!
      • 我没有找到丢失的支架(我是新手)。 “其他”电子邮件也不起作用 我的代码:if ($this->object->billing_state === 'SC') { $this->send( 'scc@gmail.com', $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); } if ($this->object->billing_state === 'RS') { $this->send( 'rs@gmail.com', $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); } else { $this->send( 'else@hotmail.com', $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); }
      • 我重做了代码的缩进。如您所见,最后一个“}”不见了。但实际上,我认为是在第二个 if(){ ... } 之前我会发布并回答。
      • 我最后只修改了一点,结帐没有完成,因为我认为在第一个“if”之后缺少一个“}”。添加后,一切正常,现在可以正常工作了!非常感谢!唯一的问题是,每当我向第二个“如果”发送邮件时,“其他”电子邮件也会收到通知,但这不是什么大不了的事,我可以忍受。
      • 很高兴能帮上忙。这就是为什么确保代码的正确缩进非常重要的原因。这解决了 80% 的问题。
      【解决方案4】:

      我尝试应用代码以允许基于自定义结帐字段的条件电子邮件 - 选择两个值。如前所述,没有电子邮件发送出去。

      if ($this->object->billing_divisions === 'sprinkler') {
        $this->send( 'shopeone@gmail.com', $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
      } else { 
        $this->send( 'shoptwo@mail.com', $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
      }
      

      我再次编辑了 php 文件以添加回替换的代码。否则不会为管理员触发新的订单电子邮件。

      if ( $this->is_enabled() && $this->get_recipient() ) {
      

      我一直在绞尽脑汁,试图让有条件的电子邮件根据自定义结帐字段选择工作。

      【讨论】:

      • 这仍然不起作用。我将 if ( $this->is_enabled() && $this->get_recipient() ) { 替换为
      • 我将 if ( $this->is_enabled() && $this->get_recipient() ) { 替换为 if ($this->object->billing_divisions === 'sprinkler') { $this ->send('shopeone@gmail.com', $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments()); } else { $this->send('shoptwo@mail.com', $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments()); } 如图所示,在 class-wc-email-new-order.php 文件中。我做错了什么?
      • add_filter('woocommerce_email_recipient_new_order', 'new_order_conditional_email_recipient', 10, 2);函数 new_order_conditional_email_recipient($recipient, $order) { $order_id = method_exists($order, 'get_id') ? $order->get_id() : $order->id; $custom_field = get_post_meta($order_id, 'custom_field', true); if ($custom_field == "value1") $recipient .= ', xxxxxervices@gmail.com'; elseif ($custom_field == "value2") $recipient .= ', xxxxxshoponline@gmail.com';返回 $recipient;测试和工作 Woocommerce 版本 3.3.3
      猜你喜欢
      • 2017-02-08
      • 2018-10-24
      • 2020-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-22
      • 2017-04-20
      相关资源
      最近更新 更多