【问题标题】:Add total discount coupons amount in Woocommerce checkout在 Woocommerce 结帐中添加总折扣券金额
【发布时间】:2023-03-30 08:36:01
【问题描述】:

我正在尝试将所有添加的优惠券相加,以便在结帐时获得折扣总额。我尝试在结帐模板文件的顶部添加一个变量并为每个条目执行 ++,但我抛出了错误。

任何想法如何将值添加到变量以获得总数?

如果您更改一个值,结帐总额会重新生成,因此我发现每次循环运行时都会输出我的答案。

我的代码:

<?php foreach ( WC()->cart->get_coupons() as $code => $coupon ) : ?>
        <tr class="cart-discount coupon-<?php echo esc_attr( sanitize_title( $code ) ); ?>">
            <th><?php wc_cart_totals_coupon_label( $coupon ); ?></th>
            <td><?php $helloworld = wc_cart_totals_coupon_html( $coupon )++; ?></td>
        </tr>
    <?php endforeach; ?>

【问题讨论】:

    标签: php wordpress woocommerce checkout discount


    【解决方案1】:

    这可以使用一些现有的 WC_Cart 方法轻松完成。

    所以在模板checkout/oreder-review.php 中,紧随其后:

            <?php foreach ( WC()->cart->get_coupons() as $code => $coupon ) : ?>
                <tr class="cart-discount coupon-<?php echo esc_attr( sanitize_title( $code ) ); ?>">
                    <th><?php wc_cart_totals_coupon_label( $coupon ); ?></th>
                    <td><?php wc_cart_totals_coupon_html( $coupon ); ?></td>
                </tr>
            <?php endforeach; ?>
    

    您将插入以下代码(在第 69 行之后)

            <?php
                $discount_excl_tax_total = WC()->cart->get_cart_discount_total();
                $discount_tax_total = WC()->cart->get_cart_discount_tax_total();
                $discount_total = $discount_excl_tax_total + $discount_tax_total;
            if( ! empty($discount_total) ): ?>
                <tr class="cart-discount coupon-<?php echo esc_attr( sanitize_title( $code ) ); ?>">
                    <th><?php _e('Discount total','woocommerce'); ?></th>
                    <td><?php echo wc_price(-$discount_total) ?></td>
                </tr>
            <?php endif; ?>
    

    经过测试并且有效。

    【讨论】:

      猜你喜欢
      • 2020-11-04
      • 2015-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-23
      • 2019-12-30
      相关资源
      最近更新 更多