Woocommerce Hook woocommerce_get_price 如何在产品列表中使用? [英] Woocommerce Hook woocommerce_get_price how to used in product list?
问题描述
我使用 Woocommerce 购物车.我想更改列表页面中的产品价格.我用这个钩子改变产品价格.
i used the Woocommerce for shopping cart. i want to change price of product in listing page. i used this hook for change product price.
woocommerce_get_price
这是我的职责.
add_action('woocommerce_get_price','change_price_regular_member', 10, 2);
function change_price_regular_member($price, $productd){
return $price*2/100;
}
我想更改列表页面中所有产品的价格.
i want to change price of all product in listing page.
但是这个功能不工作是在每个产品价格中返回第一个产品价格.
But this function is not working is return first product price in every product price.
我怎样才能用这个钩子把每一个产品的价格都改得很完美?
how can i used this hook to change every product price perfect?
谢谢.
推荐答案
当我测试您的代码时,我的列表中的所有价格都会发生变化.也许你的销售价格有问题.WC_PRODUCT
类具有不同的功能(包括过滤器以根据其销售状态获取价格).get_price_html
使用 get_regular_price
或 get_price_suffix
函数等
When i test your code, all prices in my listing change.
Maybe you got a problem with sales prices. The WC_PRODUCT
class has different functions (including filter to get a price depending on it's sales status). The get_price_html
uses the get_regular_price
or get_price_suffix
function, etc.
试试:
add_filter('woocommerce_get_price','change_price_regular_member', 10, 2);
add_filter('woocommerce_get_regular_price','change_price_regular_member', 10, 2);
add_filter('woocommerce_get_sale_price','change_price_regular_member', 10, 2);
You could also try an extension like http://club.orbisius.com/products/wordpress-plugins/woocommerce-extensions/orbisius-flex-price-woocommerce/.
更新
当我通过 paypal 下订单时,您的代码无法在任何地方使用
Your Code is not working every where when i place order through paypal
paypal 网关似乎使用订单类而不是产品类来计算价格.在 ./includes/class-wc-order.php
中,您会找到诸如 get_item_subtotal()
之类的函数,它有一个过滤器 apply_filters( 'woocommerce_order_amount_item_subtotal', $price, $this );
.尝试使用这些,例如:
It seems the paypal gateway use the order class instead of the product class to calculate prices. In ./includes/class-wc-order.php
you will find functions such as get_item_subtotal()
which has a filter apply_filters( 'woocommerce_order_amount_item_subtotal', $price, $this );
. Try to use these, for instance:
add_filter('woocommerce_order_amount_item_subtotal','change_price_regular_member', 10, 2);
这篇关于Woocommerce Hook woocommerce_get_price 如何在产品列表中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!