woocommerce在结帐和购物车页面中更改价格 [英] woocommerce change price in checkout and cart page

查看:264
本文介绍了woocommerce在结帐和购物车页面中更改价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用woocommerce,我想在我的网站上的购物车页面中添加一个选择输入,用户可以在其中选择两个选项之间的一个值,然后根据该值,我将更改价格.

With woocommerce, in my website I'd like to add in the cart page a select input where the user can select a value between two options, and depending on this value I will change the price.

到目前为止,我可以获取总数并使用此方法进行更改:

so far, I could get the total and change it using this :

function action_woocommerce_before_cart_totals(  ) { 
 global $woocommerce;

 $woocommerce->cart->total  = $woocommerce->cart->total*0.25;
   var_dump( $woocommerce->cart->total);}; 

问题在于,当我转到结帐页面时,它并没有采用在functions.php中计算的总数

The issue is that when I go to checkout page it doesn't take the total calculated in functions.php

感谢您的帮助.

推荐答案

您也可以同时使用 woocommerce_review_order_before_order_total 挂钩,以这种方式在结帐时显示自定义价格:

You can use woocommerce_review_order_before_order_total hook too at the same time, to display your custom price in checkout, this way:

add_action( 'woocommerce_review_order_before_order_total', 'custom_cart_total' );
add_action( 'woocommerce_before_cart_totals', 'custom_cart_total' );
function custom_cart_total() {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;

    WC()->cart->total *= 0.25;
    //var_dump( WC()->cart->total);
}

该代码会出现在您活动的子主题(或主题)的function.php文件中,也可能会出现在任何插件文件中.

此代码已经过测试并且可以正常工作.

This code is tested and works.

这篇关于woocommerce在结帐和购物车页面中更改价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆