该商店提供实物产品和车间课程。有许多实物产品的产品类别。车间课程有一个产品类别,叫做“票证”。我希望电子邮件发票显示“工作坊指示/政策”(地点、取消政策等)。如果订单包括车间类和“退货策略”(如果包括实物产品)。
换句话说,如果订单中的任何产品都有与“票证”相对应的产品类别标识,我需要显示车间说明/政策。如果订单中的任何产品都有一个与“票证”以外的产品类别id相对应的产品,我需要显示“返回策略”。
我“有点”让这件事奏效了。
问题是,我只能通过在电子邮件中显示Order表上面的策略来使其工作。客户想要在最底层的政策,这是有意义的。
工作的代码位于email-order-items.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; ?>正如我所说的,这是可行的,但只能通过显示order表上的内容来实现。
我试图(相当盲目地,我必须承认)利用动作钩子,但没有用。
需要帮助。我相信我需要提供更多的信息,我会很乐意这样做的。
发布于 2016-03-21 18:28:14
我解决了。以下是代码:
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()函数使用开关结构简单地组装并返回每种情况(类或“票证”等)的语句。
https://stackoverflow.com/questions/36096119
复制相似问题