WooCommerce 客户完成订单电子邮件通知中基于产品类别的条件文本

问题描述

我正在尝试根据基于订单表中产品类别的一组条件在 WooCommerce 生成的订单完成电子邮件中回显一些文本。

我有 4 个产品类别(猫一、猫二、猫三、猫四)。

一个类别有可变的产品(以防万一,虽然我正在测试没有它的订单)

我想要的条件是:

  1. 仅适用于客户订单完成的电子邮件
  2. 如果订单只有 cat-one 中的产品,则使用“SOME TEXT”
  3. 如果订单中包含第一类产品和任何其他类别的产品,则使用“其他文本”
  4. 如果订单包含除 cat-one 之外的所有类别的产品,则使用“其他文本”

到目前为止我尝试过的:

apiVersion: v1
kind: PersistentVolumeClaim
Metadata:
  name: csi-pvc
  namespace: default
spec:
  [...]
  resources:
    requests:
      # The field below can be increased.
      storage: 10Gi
      [...]

它没有按预期工作。我还尝试在 function add_instructions_to_email( $order,$sent_to_admin,$plain_text,$email ) { $cat_one = false; $cat_two = false; $cat_three = false; $cat_four = false; $items = $order->get_items(); foreach ( $items as $item ) { $product_id = $item->get_product_id(); //CHECK WHICH CATEGORIES ARE IN THE ORDER if ( has_term( 'cat_one','product_cat',$product_id ) ) { $cat_one=true; } if ( has_term( 'cat_two',$product_id ) ) { $cat_two=true; } if ( has_term( 'cat_three',$product_id ) ) { $cat_three=true; } if ( has_term( 'cat_four',$product_id ) ) { $cat_four=true; } if ( $email->id == 'customer_completed_order') { // 1. ONLY CAT-ONE if ( $cat_one) { echo "SOME TEXT"; } // 2. CAT-ONE and any other Category elseif ( $cat_one && $cat_two || $cat_three || $cat_four) { if($order->has_shipping_method('local_pickup')) { echo "SOME TEXT"; } else { echo "SOME OTHER TEXT"; } } // 3. THERE IS NO CAT-ONE IN THE ORDER elseif ( !$cat_one && $cat_two || $cat_three || $cat_four ) { if ($order->has_shipping_method('local_pickup')) { echo "SOME TEXT"; } else { echo "SOME OTHER TEXT"; } } } add_action( 'woocommerce_email_before_order_table','add_instructions_to_email',20,4 ); 中使用 elseif 语句并直接在 foreach 语句中使用条件。没有成功。

感谢任何帮助。

解决方法

您的代码有一些小错误,以下代码/答案包含:

  1. 仅适用于客户订单完成的电子邮件
  2. 如果订单只有 cat-one 中的产品,则使用“SOME TEXT”
  3. 如果订单中包含第一类产品和任何其他类别的产品,则使用“其他文本”
  4. 如果订单包含除 cat-one 之外的所有类别的产品,则使用“其他文本”

所以你得到:

function action_woocommerce_email_before_order_table( $order,$sent_to_admin,$plain_text,$email ) {   
    // Only for order completed email 
    if ( $email->id == 'customer_completed_order' ) {
        // Booleans
        $cat_one = false;
        $cat_two = false;
        $cat_three = false;
        $cat_four = false;
        
        // Output
        $output = '';
        
        // Loop through order items
        foreach ( $order->get_items() as $item ) {
            // Product ID
            $product_id = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();

            // Has term (product category)
            if ( has_term( 'cat-one','product_cat',$product_id ) ) {
                $cat_one = true;
            }
            
            if ( has_term( 'cat-two',$product_id ) ) {
                $cat_two = true;
            }
            
            if ( has_term( 'cat-three',$product_id ) ) {
                $cat_three = true;
            }
            
            if ( has_term( 'cat-four',$product_id ) ) {
                $cat_four = true;
            }
        }
        
        // 1. ONLY CAT-ONE
        if ( $cat_one ) {
            $output = "SOME TEXT";
            
            // 2. CAT-ONE and any other Category
            if ( $cat_two || $cat_three || $cat_four ) {
                $output = "SOME OTHER TEXT";
            }
        } else {
            // 3. ALL CATEGORIES EXCEPT CAT-ONE
            if ( $cat_two && $cat_three && $cat_four ) {
                $output = "ANOTHER TEXT";
            }           
        }
        
        // Output
        echo $output;
    }
}
add_action( 'woocommerce_email_before_order_table','action_woocommerce_email_before_order_table',10,4 );