Conditional display based on product category in processing order email template

R. *_*rma 3 php wordpress categories orders woocommerce

我正在研究的代码customer-processing-order.php WooCommerce email Template.

仅当订单商品具有已定义的订购商品产品类别时,我才想显示下面的代码。

<p><?php _e( "Your order has been received and is now being processed. Your order details are shown below for your reference:", 'woocommerce' ); ?></p>
Run Code Online (Sandbox Code Playgroud)

就像是:

if($categ="demo1"){
<p><?php _e( "Your order has been received and is now being processed. Some text here:", 'woocommerce' ); ?></p>
} 
Run Code Online (Sandbox Code Playgroud)

以下是模板代码的摘录:

<?php
/**
 * Customer processing order email
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/emails/customer-processing-order.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see         https://docs.woocommerce.com/document/template-structure/
 * @author      WooThemes
 * @package     WooCommerce/Templates/Emails
 * @version     2.5.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * @hooked WC_Emails::email_header() Output the email header
 */
do_action( 'woocommerce_email_header', $email_heading, $email ); ?>

<p><?php _e( "Your order has been received and is now being processed. Your order details are shown below for your reference:", 'woocommerce' ); ?></p>

<?php

/**
 * @hooked WC_Emails::order_details() Shows the order details table.
 * @hooked WC_Emails::order_schema_markup() Adds Schema.org markup.
 * @since 2.5.0
 */
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

/**
 * @hooked WC_Emails::order_meta() Shows order meta data.
 */
do_action( 'woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text, $email );

/**
 * @hooked WC_Emails::customer_details() Shows customer details
 * @hooked WC_Emails::email_address() Shows email address
 */
do_action( 'woocommerce_email_customer_details', $order, $sent_to_admin, $plain_text, $email );

/**
 * @hooked WC_Emails::email_footer() Output the email footer
 */
do_action( 'woocommerce_email_footer', $email );
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

谢谢

Loi*_*tec 5

这就是您所期待的。我使用 WordPress 原生条件函数has_term()。此函数接受单个术语的字符串或多个术语的数组中的术语 ID、术语名称或术语别名。

\n\n

您必须定义您的一个或多个类别(请参阅两个代码中的注释)。

\n\n

这是定制的模板代码(针对某一类别)

\n\n
<?php\n/**\n * Customer processing order email\n *\n * This template can be overridden by copying it to yourtheme/woocommerce/emails/customer-processing-order.php.\n *\n * HOWEVER, on occasion WooCommerce will need to update template files and you\n * (the theme developer) will need to copy the new files to your theme to\n * maintain compatibility. We try to do this as little as possible, but it does\n * happen. When this occurs the version of the template file will be bumped and\n * the readme will list any important changes.\n *\n * @see         https://docs.woocommerce.com/document/template-structure/\n * @author      WooThemes\n * @package     WooCommerce/Templates/Emails\n * @version     2.5.0\n */\n\nif ( ! defined( \'ABSPATH\' ) ) {\n    exit;\n}\n\n//Define below your category (ID, slug or name)\n$category = \'my_category\';\n\n$has_category = false;\nforeach( $order->get_items() as $order_item ) {\n    if( has_term( $category, \'product_cat\', $order_item["product_id"] ) ) {\n        $has_category = true;\n        break;\n    }\n}\n\n/**\n * @hooked WC_Emails::email_header() Output the email header\n */\n\ndo_action( \'woocommerce_email_header\', $email_heading, $email ); ?>\n\n<?php\n// Here is your conditional statement based on a defined product category\nif( $has_category ){\n    echo \'<p>\'. __( "Your order has been received and is now being processed. Some text here:", \'woocommerce\' ) .\'</p>\';\n} else {\n    echo \'<p>\'. __( "Your order has been received and is now being processed. Your order details are shown below for your reference:", \'woocommerce\' ) .\'</p>\';\n}\n?>\n\n<?php\n\n/**\n * @hooked WC_Emails::order_details() Shows the order details table.\n * @hooked WC_Emails::order_schema_markup() Adds Schema.org markup.\n * @since 2.5.0\n */\ndo_action( \'woocommerce_email_order_details\', $order, $sent_to_admin, $plain_text, $email );\n\n# ... etc ...\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果你有多个类别,代码将非常相似:

\n\n
//Define below your categories in the array (IDs, slugs or names)\n$categories = array(\'my_category1\', \'my_category2\', \'my_category3\');\n\n$has_category = false;\nforeach( $order->get_items() as $order_item ) {\n\n    if( has_term( $categories, \'product_cat\', $order_item["product_id"] ) ) {\n        $has_category = true;\n        break;\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

所有代码都经过测试并且可以工作。

\n\n
\n\n

参考:WordPress 函数参考 \xe2\x80\x94 has_term()

\n