問題描述
這是繼續(xù):在 WooCommerce 中以編程方式設(shè)置產(chǎn)品銷售價格3
答案有效,但是一旦用戶將產(chǎn)品添加到購物車,結(jié)帳時仍會顯示舊價格.
The answer works, however once a user adds the product to cart, the old price still shows up on checkout.
如何在購物車和結(jié)帳頁面上獲取購物車商品的正確銷售價格?
How to get the correct sale price on cart and checkout pages for cart items?
感謝任何幫助.
推薦答案
讓它適用于購物車和結(jié)帳頁面(以及訂單和電子郵件通知)的缺失部分是一個非常簡單的技巧:
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 文件中.
經(jīng)過測試并有效.
所以代碼只是將產(chǎn)品銷售價格設(shè)置為購物車項目中的產(chǎn)品價格,并且它可以工作.
So the code just set the product sale price as the product price in cart items and it works.
這篇關(guān)于在 Woocommerce 3 中以編程方式設(shè)置產(chǎn)品銷售價格和購物車項目價格的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!