問題描述
我使用的是 WooCommerce 3.0+ 并且我已經在某個頁面上設置了產品價格.
I am using WooCommerce 3.0+ and I have set the product price on a certain page.
$regular_price = get_post_meta( $_product->id, '_regular_price', true);
$buyback_percentage = get_post_meta( $_product->id, '_goldpricelive_buy_back', true);
$fixed_amount = get_post_meta( $_product->id, '_goldpricelive_fixed_amount', true);
$markedup_price = get_post_meta( $_product->id, '_goldpricelive_markup', true);
$buyback_price = ($regular_price - $fixed_amount)/(1 + $markedup_price/100) * (1-$buyback_percentage/100);
$_product->set_price($buyback_price);
我的購物車上的價格正在更新,但是當我點擊提交訂單時,訂單對象似乎沒有得到我設置的價格.它需要原產地產品價格.
The price is updating on my cart but when I click on to submit my order, Order object doesn't seem to get the price I set. It takes the origin product price.
知道如何實現這一點嗎?
Any idea on how I can accomplish this?
謝謝
推薦答案
更新為 get_price()
方法……
Updated with get_price()
method …
您應該在此自定義掛鉤函數、您的產品 ID 或產品 ID 數組中使用 woocommerce_before_calculate_totals
操作掛鉤設置.
然后,您可以為每個人進行自定義計算以設置將在購物車、結帳和提交訂單后設置的自定義價格.
You should use woocommerce_before_calculate_totals
action hook setting inside this custom hooked function, your products IDs or an array of product IDs.
Then for each of them you can make a custom calculation to set a custom price that will be set on Cart, checkout and after submitting in the order.
這是在 WooCommerce 3.0+ 版上測試的功能代碼:
Here is that functional code tested on WooCommerce version 3.0+:
add_action( 'woocommerce_before_calculate_totals', 'adding_custom_price', 10, 1);
function adding_custom_price( $cart_obj ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Set below your targeted individual products IDs or arrays of product IDs
$target_product_id = 53;
$target_product_ids_arr = array(22, 56, 81);
foreach ( $cart_obj->get_cart() as $cart_item ) {
// The corresponding product ID
$product_id = $cart_item['product_id'];
// For a single product ID
if($product_id == $target_product_id){
// Custom calculation
$price = $cart_item['data']->get_price() + 50;
$cart_item['data']->set_price( floatval($price) );
}
// For an array of product IDs
elseif( in_array( $product_id, $target_product_ids_arr ) ){
// Custom calculation
$price = $cart_item['data']->get_price() + 30;
$cart_item['data']->set_price( floatval($price) );
}
}
}
代碼位于活動子主題(或主題)的 function.php 文件或任何插件文件中.
然后,您可以使用 get_post_meta() 函數輕松地將我的虛假計算中的固定值替換為您的產品動態值,就像在您的代碼中一樣,因為您擁有 $product_id
強>對于每個購物車項目......
Then you can easily replace the fixed values in my fake calculations by your product dynamic values with that with get_post_meta() function just like in your code as you have the
$product_id
for each cart item…
這篇關于動態購物車項目定價不適用于 WooCommerce 3.0+ 中的訂單的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!