WooCommerce电子邮件发票:有条件的"政策"基于产品类别

时间:2016-03-18 23:50:45

标签: php wordpress woocommerce

商店提供实体产品和车间类。实物产品有很多产品类别。研讨会课程有一个产品类别,名为" ticket"。我希望电子邮件发票显示"工作坊说明/政策" (如果订单包括研讨班和"退货政策"如果它包括实体产品。

换句话说,如果订单中的任何产品的产品类别ID对应于" ticket",我需要显示Workshop说明/政策。如果订单中的任何产品的产品类别ID对应于任何其他"机票",我需要显示"退货政策"。

我"有点"有这个工作。

问题是我只能通过在电子邮件中显示订单项目表中的政策来实现此目的。客户想要BOTTOM的政策,这是有道理的。

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

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

在关闭foreach循环之后,我会进行一些调整(将多维数组减少为一个简单的数组并删除重复数据),然后评估需要显示哪些策略。

我已在变量$ nwb_ticket_cat_id中定义了研讨会(" ticket")产品类别。以下是两个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; ?>

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

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

需要帮助。我确定我需要提供更多信息,并且很乐意这样做。

1 个答案:

答案 0 :(得分:0)

我解决了。这是代码:

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结构简单地汇编并返回每种情况(类,“票证”和其他)的措辞。

相关问题