本文介绍了在 Woocommerce 3 中以编程方式设置产品销售价格和购物车项目价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!
问题描述
这是继续:在 WooCommerce 中以编程方式设置产品销售价格3
答案有效,但是一旦用户将产品添加到购物车,结帐时仍会显示旧价格.
The answer works, however once a user adds the product to cart, the old price still shows up on checkout.
如何在购物车和结帐页面上获取购物车商品的正确销售价格?
How to get the correct sale price on cart and checkout pages for cart items?
感谢任何帮助.
推荐答案
让它适用于购物车和结帐页面(以及订单和电子邮件通知)的缺失部分是一个非常简单的技巧:
The missing part to get it work for for cart and checkout pages (and also Orders and email notifications too) is a very simple trick:
add_action( 'woocommerce_before_calculate_totals', 'set_cart_item_sale_price', 20, 1 );
function set_cart_item_sale_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Iterate through each cart item
foreach( $cart->get_cart() as $cart_item ) {
$price = $cart_item['data']->get_sale_price(); // get sale price
$cart_item['data']->set_price( $price ); // Set the sale price
}
}
代码位于您的活动子主题(活动主题)的 function.php 文件中.
经过测试并有效.
所以代码只是将产品销售价格设置为购物车项目中的产品价格,并且它可以工作.
So the code just set the product sale price as the product price in cart items and it works.
这篇关于在 Woocommerce 3 中以编程方式设置产品销售价格和购物车项目价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
The End



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)