首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么使用修改后的$price调用woocommerce_variation_prices_price过滤器,将sale_price设置为regular_price?

为什么使用修改后的$price调用woocommerce_variation_prices_price过滤器,将sale_price设置为regular_price?
EN

Stack Overflow用户
提问于 2021-10-28 12:13:48
回答 1查看 60关注 0票数 1

我有这个$product->get_variation_prices()的结果,参见“价格”和"sale_price":

代码语言:javascript
复制
array(3) { 
["price"]=> array(3) { 
[18042]=> string(6) "910.57" 
[18044]=> string(6) "910.57" 
[18043]=> string(7) "1050.00" } 
["regular_price"]=> array(3) { 
[18042]=> string(7) "1138.21" 
[18043]=> string(7) "1138.21" 
[18044]=> string(7) "1138.21" } 
["sale_price"]=> array(3) { 
[18042]=> string(6) "910.57"
[18044]=> string(6) "910.57" 
[18043]=> string(7) "1050.00" } 
}

我从variation_prices中减去100

代码语言:javascript
复制
add_filter('woocommerce_variation_prices_price', 'theme_get_variation_price', 99, 3 );
function theme_get_variation_price($price, $variation, $product){

wc_delete_product_transients($variation->get_id());

if ( current_user_can('test') ){
return $price-100; //test
}
}

这是$product->get_variation_prices()输出:

代码语言:javascript
复制
array(3) { 
["price"]=> array(3) { 
[18042]=> string(6) "810.57" 
[18044]=> string(6) "810.57" 
[18043]=> string(6) "950.00" } 
["regular_price"]=> array(3) { 
[18042]=> string(7) "1138.21" 
[18043]=> string(7) "1138.21" 
[18044]=> string(7) "1138.21" } 
["sale_price"]=> array(3) { 
[18042]=> string(7) "1138.21" 
[18043]=> string(7) "1138.21" 
[18044]=> string(7) "1138.21" } 
}

为什么每次我更改$price时,"sale_price“值自动等于"regular_price"?如何保留"sale_price“值?我是不是遗漏了什么?编辑:为了清楚起见,我不希望sale_price也被减去。我想保留原始值。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-28 16:41:35

在WooCommerce代码参考中我发现了以下内容

代码语言:javascript
复制
                // If sale price does not equal price, the product is not yet on sale.
                if ( $sale_price === $regular_price || $sale_price !== $price ) {
                    $sale_price = $regular_price;
                }

这里有个链接:https://woocommerce.github.io/code-reference/files/woocommerce-includes-data-stores-class-wc-product-variable-data-store-cpt.html#source-view.308

嗯,我不认为它的产品/变化价格应该与常规/销售价值不同。同样,由于某些函数不再起作用,比如$product->is_on_sale();

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69753907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档