【问题标题】:WooCommerce Email Invoice: Conditional "Policies" based on Product CategoryWooCommerce 电子邮件发票:基于产品类别的条件“政策”
【发布时间】:2016-07-05 21:36:55
【问题描述】:

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

换句话说,如果订单中的任何产品具有与“ticket”对应的产品类别 id,我需要显示 Workshop Instructions/Policies。如果订单中的任何产品的产品类别 ID 对应于“ticket”以外的任何内容,我需要显示“Return Policies”。

我“有点”有这个工作。

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

有效的代码位于 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; ?>

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

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

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

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    我解决了。代码如下:

    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 构造简单地组合并返回每个案例(类或“票证”等)的措辞。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 2019-05-30
      • 2021-04-02
      • 2020-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多