商店提供实体产品和研讨会课程。实物产品有许多产品类别。工作坊课程有一个产品类别,称为"门票"。如果订单包括车间课程,我希望电子邮件发票显示"车间说明/政策"(场地位置;取消政策等),如果订单包含实物产品,则显示"退货政策"。
换句话说,如果订单中的任何产品具有与"门票"对应的产品类别ID,我需要出示维修中心说明/政策。如果订单中的任何产品具有与"机票"以外的任何产品类别 ID 对应的产品类别 ID,我需要显示"退货政策"。
我"有点"有这个工作。
问题是我只能通过在电子邮件中的订单项目表上方显示策略来使其工作。客户希望在底部使用策略,这是有道理的。
有效的代码位于电子邮件订单项目.php模板的底部。在该文件的foreach循环中,我有这个:
$nwb_product_cat_ids[] = wc_get_product_cat_ids( $item['product_id'] );
在 foreach 循环关闭后,我进行了一些调整(将多维数组减少为一个简单的数组并删除重复项),然后评估需要显示哪些策略。
我已经在变量 $nwb_ticket_cat_id 中定义了车间("票证")产品类别。以下是两个 if 循环:
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; ?>
正如我所说,这有效,但只能通过在订单详细信息表上方显示内容。
我尝试过(我必须承认,相当盲目)使用动作钩子,但无济于事。
需要帮助。我相信我需要提供更多信息,并且很乐意这样做。
我解决了。这是代码:
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() 函数只是使用开关构造组装并返回每种情况(类或"票证"等)的措辞。