WooCommerce中特定选定付款方式的圆形购物车总数

发布于 2025-02-11 04:54:55 字数 154 浏览 1 评论 0原文

如果有人在交货时选择现金,我需要在结帐时汇总最终价格

。我想实现的目标是:

    if payment_method == 'cod'{
    $cart_subtotal = round($cart_subtotal);
}

I need to round the final price at the checkout if someone chooses Cash on Delivery

The general idea of what I would like to achieve is:

    if payment_method == 'cod'{
    $cart_subtotal = round($cart_subtotal);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神回复 2025-02-18 04:54:55

首先,请确保每次用户更改付款方式时都会重新计算购物车的总计:

add_action('wp_footer', 'trigger_checkout_refresh_on_payment_method_change');
function trigger_checkout_refresh_on_payment_method_change(){
    if ( is_checkout() && ! is_wc_endpoint_url() ) :
    ?>

    <script type="text/javascript">
        (function($){
            $(document.body).on('change', 'input[name="payment_method"]', function(){
                $(document.body).trigger('update_checkout').trigger('wc_fragment_refresh');
            });
        })(jQuery);
    </script>

    <?php
    endif;
}

根据您想要实现的逻辑,有很多方法可以使价格汇总,但这是最简单的方法总计如果用户选择“现金交货”作为付款方式:


add_filter( 'woocommerce_calculated_total', 'round_total_for_specific_payment_methods', 10, 2 );
function round_total_for_specific_payment_methods( $total, $cart ) {
    $chosen_payment_method = WC()->session->get('chosen_payment_method');
    
    if ( $chosen_payment_method && $chosen_payment_method === 'cod' ) {
        $total = round( $total );
    }
    
    return $total;
}

First of all, make sure that the cart totals will be re-calculated every time a user changes the payment method:

add_action('wp_footer', 'trigger_checkout_refresh_on_payment_method_change');
function trigger_checkout_refresh_on_payment_method_change(){
    if ( is_checkout() && ! is_wc_endpoint_url() ) :
    ?>

    <script type="text/javascript">
        (function($){
            $(document.body).on('change', 'input[name="payment_method"]', function(){
                $(document.body).trigger('update_checkout').trigger('wc_fragment_refresh');
            });
        })(jQuery);
    </script>

    <?php
    endif;
}

There are many ways to round a price depending on what logic you want to achieve, but here's the most simple way to round the total if a user has chosen "Cash on delivery" as their payment method:


add_filter( 'woocommerce_calculated_total', 'round_total_for_specific_payment_methods', 10, 2 );
function round_total_for_specific_payment_methods( $total, $cart ) {
    $chosen_payment_method = WC()->session->get('chosen_payment_method');
    
    if ( $chosen_payment_method && $chosen_payment_method === 'cod' ) {
        $total = round( $total );
    }
    
    return $total;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文