Woocommerce更新价格不会保存在购物车会话中
I'm having issues in Wordpress Woocommerce whereby I have programmatically updated prices of products based on whatever conditions they need. Below is a simple example. I have it displaying and then adding to the cart just fine. My problem is, when the user logs out and logs back in, the cart ends up returning the full prices of the product. Either I'm updating the price incorrectly, or there is a better way to ensure the cart has the correct discounted price.
Here is what I have in functions.php
add_action('woocommerce_get_price_html','pricechanger');
function pricechanger($price){
$theid = get_the_ID();
$product = wc_get_product($theid);
$price = $product->price;
$price = //do something to the price here
//save the productid/price in session for cart
$_SESSION['pd']['$theid'] = $price;
add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10, 2);
add_action( 'init', 'woocommerce_add_to_cart_action', 10);
add_action( 'init', 'woocommerce_checkout_action', 10 );
return $price;
}
Because the price wouldn't transition to the add to cart button, I've had to save them in a session. I haven't found anywhere that sends that price to the cart.
add_action( 'woocommerce_before_calculate_totals', 'woo_add_discount');
function woo_add_discount() {
if(isset($_SESSION['pd'])) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
foreach($_SESSION['pd'] as $key => $val) {
if($cart_item['data']->id == $key){
$cart_item['data']->set_price($val);
}
}
}
}
}
Help is greatly appreciated! Thanks!
我在Wordpress Woocommerce中遇到问题,我根据他们需要的条件以编程方式更新产品价格。 下面是一个简单的例子。 我有它显示,然后添加到购物车就好了。 我的问题是,当用户退出并重新登录时,购物车最终会返回产品的全价。 要么我不正确地更新价格,要么有更好的方法来确保购物车有正确的折扣价格。 p>
以下是我在functions.php中的内容 p>
add_action('woocommerce_get_price_html','pricechanger');
function pricechanger($ price){
$ theid = get_the_ID();
$ product = wc_get_product($ theid);
$ price = $ product-> price;
$ price = //做点什么 到这里的价格
//将产品/价格保存在购物车的会话中
$ _SESSION ['pd'] ['$ theid'] = $ price;
add_action('woocommerce_after_shop_loop_item','woocommerce_template_loop_add_to_cart ',10,2);
add_action('init','woocommerce_add_to_cart_action',10);
add_action('init','woocommerce_checkout_action',10);
返回$ price;
}
code> pre>
由于价格不会转换为添加到购物车按钮,我必须将它们保存在会话中。 我还没有找到任何可以将价格发送到购物车的地方。 p>
add_action('woocommerce_before_calculate_totals','woo_add_discount');
函数woo_add_discount(){
if (isset($ _ SESSION ['pd'])){
foreach(WC() - > cart-> get_cart()as $ cart_item_key => $ cart_item){
foreach($ _ SESSION ['pd' ]如$ key => $ val){
if($ cart_item ['data'] - > id == $ key){
$ cart_item ['data'] - > set_price($ val);
}
}
}
}
}
code> pre>
非常感谢帮助!
谢谢! p>
DIV>
I forgot to post my concluded answer here.
You need to run these hooks against your price change code:
add_action( 'woocommerce_before_calculate_totals', 'your_function_here');
add_action( 'woocommerce_before_mini_cart', 'your_function_here');
function your_function_here(){
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
//do something to your price here
$price = 'some calculation here';
//set your price
$cart_item['data']->price = floatval($price);
}
}
Hope that helps someone!