問題描述
使用 woocommerce,在我的網(wǎng)站中,我想在購物車頁面中添加一個(gè)選擇輸入,用戶可以在其中選擇兩個(gè)選項(xiàng)之間的值,并根據(jù)該值更改價(jià)格.
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.
到目前為止,我可以獲得總數(shù)并使用此更改它:
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);};
問題是,當(dāng)我去結(jié)帳頁面時(shí),它沒有計(jì)算functions.php
The issue is that when I go to checkout page it doesn't take the total calculated in functions.php
謝謝你幫助我.
推薦答案
您也可以同時(shí)使用 woocommerce_review_order_before_order_total
鉤子,在結(jié)帳時(shí)顯示您的自定義價(jià)格,這樣:
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);
}
代碼位于活動(dòng)子主題(或主題)的 function.php 文件或任何插件文件中.
此代碼已經(jīng)過測(cè)試且有效.
This code is tested and works.
這篇關(guān)于woocommerce 在結(jié)帳和購物車頁面中更改價(jià)格的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!