WooCommerce 电子邮件发票:有条件的“政策"基于产品类别 [英] WooCommerce Email Invoice: Conditional "Policies" based on Product Category

查看:108
本文介绍了WooCommerce 电子邮件发票:有条件的“政策"基于产品类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该商店提供实体产品和研讨会课程.实体产品有很多产品类别.研讨会课程有一个产品类别,称为门票".如果订单包含研讨会课程,我希望电子邮件发票显示研讨会说明/政策"(场地位置;取消政策等);如果订单包含实体产品,则显示退货政策".

The store offers physical products and workshop classes. There are many product categories for physical products. There is ONE product category for workshop classes, called "ticket". I want the email invoice to display "Workshop Instructions/Policies" (location of venue; cancellation policy; etc.) if the order includes a workshop class and "Return Policies" if it includes physical products.

换句话说,如果订单中的任何产品具有与ticket"对应的产品类别 ID,我需要显示 Workshop 说明/政策.如果订单中的任何产品的产品类别 ID 对应于票"以外的任何东西,我需要显示退货政策".

In other words, if any product in the order has a product category id corresponding to "ticket", I need to show Workshop Instructions/Policies. And if any product in the order has a product category id corresponding to ANYTHING OTHER THAN "ticket", I need to display "Return Policies".

我有点"有这个工作.

问题是,我只能通过在电子邮件中的订单项目"表上方显示政策来使其工作.客户想要底部的策略,这是有道理的.

The problem is that I can only get this to work by displaying the policies ABOVE the Order Items table in the email. The customer wants the policies at the BOTTOM, which makes sense.

有效的代码位于 email-order-items.php 模板的底部.在那个文件的 foreach 循环中,我有这个:

The code that works is at the bottom of the email-order-items.php template. Inside the foreach loop in that file, I have this:

$nwb_product_cat_ids[] = wc_get_product_cat_ids( $item['product_id'] );

在 foreach 循环结束后,我会进行一些调整(将多维数组减少为简单数组并删除重复项),然后评估需要显示哪些策略.

After the close of the foreach loop is where I do some tweaking (reducing the multidimensional array to a simple array and removing duplicates) and then evaluating which policies need to be displayed.

我已在变量 $nwb_ticket_cat_id 中定义了研讨会(票")产品类别.下面是两个 if 循环:

I have defined the workshop ("ticket") product category in the variable $nwb_ticket_cat_id. Here are the two if loops:

if ( in_array( $nwb_ticket_cat_id, $nwb_product_cat_ids_reduced ) ) {
    $nwb_show_policy_class = true;
}

if ( count($nwb_product_cat_ids_reduced) > 1 
|| 
!in_array( $nwb_ticket_cat_id, $nwb_product_cat_ids_reduced ) ) {
    $nwb_show_policy_return = true;
}

然后我有这个:

<?php if ( $nwb_show_policy_return ) : ?>
    <p>Here is our return policy:</p>
<?php endif; ?>

<?php if ( $nwb_show_policy_class ) : ?>
    <p>Here is our class policy:</p>
<?php endif; ?>

正如我所说,这有效,但只能通过显示订单详细信息表上方的内容.

As I said, this works, but only by displaying the content ABOVE the order details table.

我已经尝试(相当盲目,我必须承认)使用动作钩子,但无济于事.

I've tried (rather blindly, I must admit) to utilize action hooks, to no avail.

需要帮助.我确定我需要提供更多信息并且很乐意提供.

Help wanted. I'm sure I need to provide more information and will do so gladly.

推荐答案

我解决了.代码如下:

function nwb_show_policies_under_items_table($order, $sent_to_admin) {
    if ( $sent_to_admin ) {
        return; // Not showing on the admin notice.
    }
    $nwb_ticket_cat_id = NWB_TICKET_CAT_ID; // The product_cat ID corresponding to "Ticikets"
    $nwb_product_cat_ids = array(); // init Array of product IDs for this order
    $nwb_show_policy_class = false; // init
    $nwb_show_policy_return = false; // init
    $items = $order->get_items(); // Get the items for this order
    // Populate the array of product category IDs for this order:
    foreach ( $items as $key => $item ) {
        $nwb_product_cat_ids[] = wc_get_product_cat_ids( $item['product_id'] );
    }
    // Reduce the multidimensional array to a flat one:
    $nwb_product_cat_ids_reduced = call_user_func_array('array_merge', $nwb_product_cat_ids);
    // Get rid of ducplicate product_cat IDS:
    $nwb_product_cat_ids_reduced = array_unique($nwb_product_cat_ids_reduced);

    // If our ticket product_cat_id is in there, then we need to show the Class Instructions/Policies
    if ( in_array( $nwb_ticket_cat_id, $nwb_product_cat_ids_reduced ) ) {
        $nwb_show_policy_class = true;
    }
    // And here's how we determine whether the order includes a product OTHER THAN "ticket"
    if ( count($nwb_product_cat_ids_reduced) > 1 || !in_array( $nwb_ticket_cat_id, $nwb_product_cat_ids_reduced ) ) {
        $nwb_show_policy_return = true;
    }

    // And now we show the policies if applicable:
    if ( $nwb_show_policy_class ) {
        echo nwb_woo_policy('class');
    }
    if ( $nwb_show_policy_return ) {
        echo nwb_woo_policy('other');
    }
}
add_action( 'woocommerce_email_after_order_table', 'nwb_show_policies_under_items_table', 10, 2 );

nwb_woo_policy() 函数使用 switch 结构简单地组合并返回每种情况(类或票"等)的措辞.

The nwb_woo_policy() function simply assembles and returns the verbiage for each case (class, or "ticket", and other) using a switch construct.

这篇关于WooCommerce 电子邮件发票:有条件的“政策"基于产品类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆