【问题标题】:Disable WooCommerce email notification for specific product禁用特定产品的 WooCommerce 电子邮件通知
【发布时间】:2017-04-08 08:52:36
【问题描述】:

我可以参考这个功能来禁用电子邮件通知: https://docs.woocommerce.com/document/unhookremove-woocommerce-emails/

但我只想为特定产品禁用它,或者如果可以更简单的话,为特定产品类别禁用它。

感谢您的帮助

【问题讨论】:

    标签: notifications woocommerce hook-woocommerce


    【解决方案1】:

    感谢@vidish-purohit 的帮助!

    如果您需要为特定产品禁用 admin 电子邮件通知,请使用以下代码:

    function change_email_recipient_depending_of_product_id( $recipient, $order ) {
        global $woocommerce;
        $items = $order->get_items();
        foreach ( $items as $item ) {
            $product_id = $item['product_id'];
            if ( $product_id == xxx ) {
                $recipient = '';
            }
            return $recipient;
        }
    }
    add_filter( 'woocommerce_email_recipient_new_order', 'change_email_recipient_depending_of_product_id', 10, 2 );
    

    如果您需要为特定产品禁用客户电子邮件通知:

    function change_email_recipient_depending_of_product_id( $recipient, $order ) {
        global $woocommerce;
        $items = $order->get_items();
        foreach ( $items as $item ) {
            $product_id = $item['product_id'];
            if ( $product_id == xxx ) {
                $recipient = '';
            }
            return $recipient;
        }
    }
    add_filter( 'woocommerce_email_recipient_customer_processing_order', 'change_email_recipient_depending_of_product_id', 10, 2 );
    

    【讨论】:

      【解决方案2】:

      我认为当您尝试从模板中挂钩电子邮件通知时,您可以在其中找到订单,那时电子邮件已经发送。

      您可以尝试一件事 - 使用收件人的挂钩可以删除收件人电子邮件并返回空字符串。或者如果空字符串触发错误,那么你可以给一些虚拟电子邮件。

      使用此代码:

      // Change new order email recipient for registered customers
      function wc_change_admin_new_order_email_recipient( $recipient, $order ) {
          global $woocommerce;
      
          // check if product in order
          if ( true ) ) {
              $recipient = "";
          } else {
              $recipient = "newbusiness@yourdomain.com";
          }
          return $recipient;
      }
      add_filter('woocommerce_email_recipient_new_order', 'wc_change_admin_new_order_email_recipient', 1, 2);
      

      // Change new order email recipient for registered customers
      function wc_change_admin_new_order_email_recipient( $recipient, $order ) {
      
          $flagHasProduct = false;
      
          // Get items in order
          $items = $order->get_items(); 
      
          // Loop for all items
          foreach ( $items as $item ) {
             $product_id = $item['product_id'];
      
              // check if specific product is in order
              if ( $product_id == 102 ) {
                  $flagHasProduct = true;
              }
          }
      
          // if product is found then remove recipient
          if ($flagHasProduct) {
              $recipient = "";
          } else {
              $recipient = "newbusiness@yourdomain.com";
          }
          return $recipient;
      }
      add_filter('woocommerce_email_recipient_new_order', 'wc_change_admin_new_order_email_recipient', 1, 2);
      

      【讨论】:

      • 感谢您的帮助!是的,它可以是删除收件人电子邮件的解决方案,有趣...现在困难的部分是编写条件(如果产品 ID 是...)。
      • 如果您看到函数参数,您将获得订单对象作为第二个参数。您可以从中按顺序获取所有项目,并检查特定产品是否在订单中。如果您仍需要帮助,请告诉我。
      • 谢谢 Vidish,非常感谢您的帮助,如果我们假设产品 ID 为 102,代码会是什么样子?
      • 我已经编辑了我的答案并添加了完整的解决方案。您可以使用此代码。
      • 如果您觉得我的回答有用,那么您可以接受并点赞!
      【解决方案3】:

      以上代码将禁用 Woocommerce 电子邮件设置选项页面中的电子邮件选项。

      /**
       * Disable Admin email Notification for Specific Product
       */
      
      function cstm_change_email_recipient_for_giftcard_product($recipient, $order)
      {
          // Bail on WC settings pages since the order object isn't yet set yet
          // Not sure why this is even a thing, but shikata ga nai
          $page = $_GET['page'] = isset($_GET['page']) ? $_GET['page'] : '';
          if ('wc-settings' === $page) {
              return $recipient;
          }
      
          // just in case
          if (!$order instanceof WC_Order) {
              return $recipient;
          }
      
          $items = $order->get_items();
          foreach ($items as $item) {
              $product_id = $item['product_id'];
              if ($product_id == xxxx) {
                  $recipient = '';
              }
              return $recipient;
          }
      }
      
      add_filter('woocommerce_email_recipient_new_order', 'cstm_change_email_recipient_for_giftcard_product', 10, 2);
      

      此代码在最新版本的 Woocommerce 中运行良好。

      【讨论】:

        猜你喜欢
        • 2019-10-07
        • 2018-07-04
        • 2018-03-10
        • 1970-01-01
        • 2019-04-08
        • 2021-01-04
        • 2020-10-03
        • 1970-01-01
        相关资源
        最近更新 更多