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