[英]Display selected variation price suffixed in Woocommerce variable products
I am using this function so I get the default variation price but I want to show my price suffix after the price:我正在使用这个函数,所以我得到了默认的变化价格,但我想在价格后显示我的价格后缀:
add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
function custom_variation_price( $price, $product ) {
foreach($product->get_available_variations() as $pav){
$def=true;
foreach($product->get_variation_default_attributes() as $defkey=>$defval){
if($pav['attributes']['attribute_'.$defkey]!=$defval){
$def=false;
}
}
if($def){
$price = $pav['display_price'];
}
}
return woocommerce_price($price);
}
What I am getting out now is for example '€15,00' but what I want it to show is '€15,00 per kilo' and 'per kilo' is the price suffix例如,我现在得到的是“15,00 欧元”,但我希望它显示的是“每公斤 15,00 欧元”,“每公斤”是价格后缀
Your code is outdated since Woocommerce 3 ( get_variation_default_attributes()
and woocommerce_price()
are deprecated) .您的代码已过时,因为 Woocommerce 3 (
get_variation_default_attributes()
和woocommerce_price()
已弃用) 。
The will display the selected variation price suffixed for Woocommerce 3:将显示 Woocommerce 3 后缀的所选变体价格:
add_filter('woocommerce_available_variation', 'display_variation_price_suffixed', 10, 3 );
function display_variation_price_suffixed( $variation_data, $product, $variation ) {
$variation_data['price_html'] .= ' <span class="price-suffix">' . __("per kilo", "woocommerce") . '</span>';
return $variation_data;
}
Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.
测试和工作。
If needed you can use
$variation_data['attributes']
array to target specific product attributes term values in a foreach loop.如果需要,您可以使用
$variation_data['attributes']
数组在 foreach 循环中定位特定的产品属性术语值。
For information, the filter hook woocommerce_available_variation
is located inside WC_Product_Variable
get_available_variation()
method.有关信息,过滤器钩子
woocommerce_available_variation
位于WC_Product_Variable
get_available_variation()
方法内。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.