首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在WooCommerce订单汇总表中,将折扣(优惠券)行分成多个扣除成本行

在WooCommerce订单汇总表中,将折扣(优惠券)行分成多个扣除成本行
EN

Stack Overflow用户
提问于 2022-02-05 15:39:50
回答 2查看 336关注 0票数 2

如果使用了2张折扣券,则在Woocommerce订单总计表中显示2张优惠券的总和,而我想分别显示每张优惠券的扣除成本。

例如,插入了2张优惠券,目前它显示:

  • 优惠券:$100

我想把这个改为:

  • Coupon1(优惠券代码):50美元
  • Coupon2(优惠券代码):50美元

来自WooCommerce模板文件的路径:order-details.php

代码语言:javascript
复制
foreach ( $order->get_order_item_totals() as $key => $total ) {
    ?>
        <tr>
            <th scope="row"><?php echo esc_html( $total['label'] ); ?></th>
            <td><?php echo ( 'payment_method' === $key ) ? esc_html( $total['value'] ) : wp_kses_post( $total['value'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></td>
        </tr>
        <?php
}

关于如何改变这个问题有什么建议吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-02-06 05:49:18

没有必要通过woocommerce_get_order_item_totals钩子添加HTML标记,因为它涉及表行。

接下来,答案将删除默认折扣行,并将其分割成多行,并包含所需的详细信息。

本答复中使用的函数:

所以你得到了:

代码语言:javascript
复制
function filter_woocommerce_get_order_item_totals( $total_rows, $order, $tax_display ) {
    // Exit if there is no coupons applied
    if ( sizeof ( $order->get_coupon_codes() ) == 0 ) return $total_rows;
    
    // Initializing
    $new_rows = [];
    
    // Loop through order total lines
    foreach ( $total_rows as $total_key => $total_row ) {
        // Store current rows, except discount
        if ( $total_key != 'discount' ) {
            // Store
            $new_rows[$total_key] = $total_row;
        } else {            
            // For each coupon
            foreach ( $order->get_items('coupon') as $coupon ) {
                // New rows
                $new_rows[$coupon->get_code()] = array(
                    'label' => __( 'Coupon: ', 'woocommerce' ) . $coupon->get_code(),
                    'value' => wc_price( $coupon->get_discount() ),
                );
            }   
        }
    }

    // Overwrite
    $total_rows = $new_rows;

    return $total_rows;
}
add_filter( 'woocommerce_get_order_item_totals', 'filter_woocommerce_get_order_item_totals', 10, 3 );

结果:

相关:向WooCommerce视图、订单详细信息和电子邮件通知中添加优惠券名称和百分比

票数 2
EN

Stack Overflow用户

发布于 2022-02-05 16:07:31

代码语言:javascript
复制
add_filter('woocommerce_get_order_item_totals', 'woocommerce_get_order_item_totals', 10, 3);

function woocommerce_get_order_item_totals($total_rows, $order, $tax_display) {
    // Loop through order items "coupon"
    $original_dicount_value = $total_rows['discount']['value'];
    $breaked_dicount_value = '';
    $wrapp_table_start = '<table style="width:100%">';
    $i = 1;
    foreach ($order->get_items('coupon') as $item_id => $item) {
        // Get the coupon array data
        $data = $item->get_data();
        $coupon_code = $data['code'];
        $coupon_amt = strip_tags(wc_price($data['discount'] + $data['discount_tax']));
        $breaked_dicount_value .= "<tr>
            <td>coupon$i($coupon_code)</td>
            <td>$coupon_amt</td>
          </tr>";
        $i++;
    }
    $wrapp_table_end = "</table>";
    $total_rows['discount']['value'] = ('' !== $breaked_dicount_value ) ? $wrapp_table_start . $breaked_dicount_value . $wrapp_table_end : $original_dicount_value;
    return $total_rows;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70999498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档