自定义产品价格使购物车商品价格显示为 0
Customizing product prices make cart items price showing 0
我正在尝试根据位置更改产品价格。
为此,我正在使用 wc fields factory 为位置创建字段数并更新价格,并根据 IP 查找城市(位置)并获取自定义字段值。
使用
function return_custom_price($price, $product) {
global $post, $blog_id;
$price = get_post_meta($post->ID, '_regular_price');
$post_id = $post->ID;
$price = ($price[0]*2.5);
return $price;
}
add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);
它工作正常,但是当我去那里查看购物车时它显示产品价格为 0
像这样:
请帮我解决这个问题。
谢谢。
Update:
产品元数据'_regular_price'
不是自定义字段而是产品正常价格,您可以在[=41]上直接using WC_Product methods and magic properties directly获取=]$product
对象.
If you look to your function you have 2 arguments: $price
(the product price) and the $product
(the product object)… So you don't need to use any global variables as you got already $product object, that you can use.
这是更新后的代码:
add_filter('woocommerce_get_price', 'product_custom_price', 10, 2);
function product_custom_price($price, $product) {
$custom_price = $product->get_regular_price();
return $custom_price * 2.5;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
请查看购物车截图:
1) 未使用此代码的购物车(之前):
2) 使用此代码的购物车(之后):
As you can see, this code works perfectly and display the regular prices in cart items.
OP 将此代码与自定义字段一起使用:
add_filter('woocommerce_get_price', 'product_custom_price', 10, 2);
function product_custom_price($price, $product) {
$custom_price = get_post_meta($product->id, 'custom_key', true);
return $custom_price;
}
我正在尝试根据位置更改产品价格。
为此,我正在使用 wc fields factory 为位置创建字段数并更新价格,并根据 IP 查找城市(位置)并获取自定义字段值。
使用
function return_custom_price($price, $product) {
global $post, $blog_id;
$price = get_post_meta($post->ID, '_regular_price');
$post_id = $post->ID;
$price = ($price[0]*2.5);
return $price;
}
add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);
它工作正常,但是当我去那里查看购物车时它显示产品价格为 0 像这样:
请帮我解决这个问题。
谢谢。
Update:
产品元数据'_regular_price'
不是自定义字段而是产品正常价格,您可以在[=41]上直接using WC_Product methods and magic properties directly获取=]$product
对象.
If you look to your function you have 2 arguments:
$price
(the product price) and the$product
(the product object)… So you don't need to use any global variables as you got already $product object, that you can use.
这是更新后的代码:
add_filter('woocommerce_get_price', 'product_custom_price', 10, 2);
function product_custom_price($price, $product) {
$custom_price = $product->get_regular_price();
return $custom_price * 2.5;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
请查看购物车截图:
1) 未使用此代码的购物车(之前):
2) 使用此代码的购物车(之后):
As you can see, this code works perfectly and display the regular prices in cart items.
OP 将此代码与自定义字段一起使用:
add_filter('woocommerce_get_price', 'product_custom_price', 10, 2);
function product_custom_price($price, $product) {
$custom_price = get_post_meta($product->id, 'custom_key', true);
return $custom_price;
}