根据 WooCommerce 中的付款方式提高购物车商品价格

Posted

技术标签:

【中文标题】根据 WooCommerce 中的付款方式提高购物车商品价格【英文标题】:Increase cart item prices based on payment method in WooCommerce 【发布时间】:2021-12-21 21:21:01 【问题描述】:

我想根据所选支付网关为购物车商品价格添加百分比值。

我面临的问题是产品价格的变化没有针对产品价格进行更新。最初选择的价格一直显示。

我怎样才能得到相应的更改价格?

到目前为止我的代码:

// Set custom cart item price
function add_custom_price( $cart ) 

    // This is necessary for WC 3.0+
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Avoiding hook repetition (when using price calculations for example | optional)
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) 

        $chosen_payment_method = WC()->session->get('chosen_payment_method');

        if($chosen_payment_method == 'cod') 
            $increaseby =  3;
         elseif($chosen_payment_method == 'paypal') 
            $increaseby =  8;
         else 
            $increaseby =  10;
        

        $price = get_post_meta($cart_item['product_id'] , '_price', true);
        $price = $price + (($price * $increaseby)/100);
        $cart_item['data']->set_price( $price );
    

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 1000, 1);

非常感谢任何帮助。

【问题讨论】:

【参考方案1】:

你的代码有一些错误

在 foreach 循环外使用 WC()->session->get( 'chosen_payment_method' ); 获取价格不需要get_post_meta(),可以使用get_price() 您还需要在更改付款方式时触发的 jQuery。

所以你得到:

function action_woocommerce_before_calculate_totals( $cart ) 
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Get payment method
    $chosen_payment_method = WC()->session->get( 'chosen_payment_method' );

    // Compare
    if ( $chosen_payment_method == 'cod' ) 
        $increaseby = 3;
     elseif ( $chosen_payment_method == 'paypal' ) 
        $increaseby = 8;
     else 
        $increaseby = 10;
    

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item )      
        // Get price
        $price = $cart_item['data']->get_price();

        // Set price
        $cart_item['data']->set_price( $price + ( $price * $increaseby ) / 100 );
    

add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );    

function action_wp_footer() 
    if ( is_checkout() && ! is_wc_endpoint_url() ) :
    ?>
    <script type="text/javascript">
        jQuery(function($)
            $( 'form.checkout' ).on( 'change', 'input[name="payment_method"]', function() 
                $(document.body).trigger( 'update_checkout' );
            );
        );
    </script>
    <?php
    endif;

add_action( 'wp_footer', 'action_wp_footer' );

【讨论】:

如果有人想知道和像我这样的新手,上面的代码应该添加到主题文件的functions.php中。很好地为我工作。为答案竖起大拇指

以上是关于根据 WooCommerce 中的付款方式提高购物车商品价格的主要内容,如果未能解决你的问题,请参考以下文章

WooCommerce:根据选择的支付网关向卡添加费用

根据 Woocommerce 中的运输方式和付款方式添加费用

根据 WooCommerce 购物车中的商品数量和产品类别禁用支付网关

根据 Woocommerce 选择的付款方式更改结帐时的付款按钮

根据 Woocommerce 中的特定城市显示货到付款 (COD)

根据 Woocommerce 中选择的付款方式禁用运输方式