【问题标题】:Hide discount infos without canceling the coupon in woocommerce隐藏折扣信息而不取消woocommerce中的优惠券
【发布时间】:2014-11-01 02:20:04
【问题描述】:

我在 Woocommerce 购物车中使用优惠券进行计算。它会自动在总额中添加折扣,以便将正确的金额发送到支付网关。

我想对访问者隐藏有关此优惠券/折扣的所有信息。

问题:我发现的唯一方法(见下文)隐藏了优惠券字段、行(来自总计)和消息,但也禁用了优惠券...

add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field' );
function hide_coupon_field( $enabled ) {
    if ( is_cart() || is_checkout() ) {
        $enabled = false;
    }
    return $enabled;
}

是否有一个钩子可以隐藏与折扣相关的所有内容而不取消优惠券?


编辑: 看起来不可能简单地删除 order-details 中的折扣行。因此,受 helgatheviking 建议启发的一个简单解决方案可能是删除这部分生成的所有总数

<?php if ( $totals = $order->get_order_item_totals() ) foreach ( $totals as $total ) : ?>
            <tr>
                <th scope="row"><?php echo $total['label']; ?></th>
                <td><?php echo $total['value']; ?></td>
            </tr>
    <?php endforeach; ?>

然后以我需要的方式一一回应它们。我已经可以用这个显示订单总额了

<td><?php echo number_format($order->get_total(),2,'.','')."&#8364;"; ?></td>

但现在我正在尝试检索 订单小计,以及此代码

<td><?php echo number_format($order->get_item_subtotal(),2,'.','')."&#8364;"; ?></td>

给我一​​个警告:WC_Order::get_item_subtotal() 缺少参数 1。

我不确定get_item_subtotal() 是否是获取订单小计的正确方法。如果是这样,缺少什么论据?还是我应该搜索get_line_subtotalget_subtotal_to_display

【问题讨论】:

  • 如果您查看WC_Order 类中的get_item_subtotal() 方法,您会发现需要$item 参数。

标签: wordpress woocommerce


【解决方案1】:

不,似乎没有,因为购物车类的 get_coupons() 方法中没有过滤器。如果您访问 WooCommerce git repo 并在此处发送带有过滤器的拉取请求并解释为什么它应该在那里,他们可能会考虑将其合并。我已经这样做了几次。

您也可以将checkout/review-order.phpcart/cart-totals.php 模板复制到您的主题中并删除以下两个代码块:

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

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

请记住,这会阻止所有优惠券折扣的显示,最终看起来像以下屏幕截图:

我不喜欢覆盖更复杂的 WC 模板……尤其是那些与结帐流程相关的模板。随着 WooCommerce 的发展,当主题模板覆盖变得过时时,我不得不修复许多停止工作的网站。

编辑

我在order/order-details.php 模板中找到了折扣行。它来自函数$order-&gt;get_order_item_totals()...,它返回一个行数组并且可以被过滤。因此,这会从收到的订单页面中删除该行:

function so_25714509_get_order_item_totals( $total_rows ){
    unset( $total_rows['order_discount'] );
    return $total_rows;
}
add_filter( 'woocommerce_get_order_item_totals', 'so_25714509_get_order_item_totals' );

【讨论】:

  • 谢谢,我之前也这样做过,但无法摆脱订单详情(结帐后的页面)中的折扣行。我认为有一个更简单的解决方案,但我认为你的方法是最简单的方法。我删除了负责显示所有总数的部分&lt;?php if ( $totals = $order-&gt;get_order_item_totals() ) foreach ( $totals as $total ) : ?&gt; &lt;tr&gt; &lt;th scope="row"&gt;&lt;?php echo $total['label']; ?&gt;&lt;/th&gt; &lt;td&gt;&lt;?php echo $total['value']; ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?php endforeach; ?&gt;
  • 现在我正在尝试在需要时显示调用它的每一行。我只是坚持在模板上呼应小计......你能帮我解决这个问题吗?
  • 我试图用&lt;?php echo number_format($order-&gt;get_item_subtotal(),2,'.','')."&amp;#8364;"; ?&gt; 显示它,但我有一个错误...我应该用什么替换这个get_item_subtotal()
  • 我并没有真正关注你。为什么需要更换小计?它应该是什么?您可以编辑您的问题以反映这一点吗?
  • 我不想替换get_item_subtotal(),我只想显示订单小计,现在我收到警告。我刚刚编辑了这个问题,希望它更清楚。
猜你喜欢
  • 2015-09-08
  • 1970-01-01
  • 2021-03-18
  • 2012-12-27
  • 1970-01-01
  • 1970-01-01
  • 2020-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多