Wordpress 在销售按钮中显示折扣百分比
我正在寻找一种方法来显示在商业销售泡沫中折扣的百分比。下面是按钮现在的外观:Wordpress 在销售按钮中显示折扣百分比,wordpress,woocommerce,Wordpress,Woocommerce,我正在寻找一种方法来显示在商业销售泡沫中折扣的百分比。下面是按钮现在的外观: 因此,基本上,按钮将显示:-20%您应该能够连接到woocommerce\u sale\u flash过滤器,抓取产品对象,计算出百分比并将其添加到HTML中 大概是这样的: add_filter( 'woocommerce_sale_flash', 'add_percentage_to_sale_bubble' ); function add_percentage_to_sale_bubble( $html ) {
因此,基本上,按钮将显示:-20%您应该能够连接到
woocommerce\u sale\u flash
过滤器,抓取产品对象,计算出百分比并将其添加到HTML中
大概是这样的:
add_filter( 'woocommerce_sale_flash', 'add_percentage_to_sale_bubble' );
function add_percentage_to_sale_bubble( $html ) {
global $product;
$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
$output =' <span class="onsale">VERKOOP -'.$percentage.'%</span>';
return $output;
}
add_filter( 'woocommerce_sale_flash', 'add_percentage_to_sale_bubble', 20 );
function add_percentage_to_sale_bubble( $html ) {
global $product;
if ($product->is_type('simple')) { //if simple product
$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 ).'%';
} else { //if variable product
$percentage = get_variable_sale_percentage( $product );
}
$output =' <span class="onsale">-'.$percentage.'</span>';
return $output;
}
function get_variable_sale_percentage( $product ) {
//get variables
$variation_min_regular_price = $product->get_variation_regular_price('min', true);
$variation_max_regular_price = $product->get_variation_regular_price('max', true);
$variation_min_sale_price = $product->get_variation_sale_price('min', true);
$variation_max_sale_price = $product->get_variation_sale_price('max', true);
//get highest and lowest percentages
$lower_percentage = round( ( ( $variation_min_regular_price - $variation_min_sale_price ) / $variation_min_regular_price ) * 100 );
$higher_percentage = round( ( ( $variation_max_regular_price - $variation_max_sale_price ) / $variation_max_regular_price ) * 100 );
//sort array
$percentages = array($lower_percentage, $higher_percentage);
sort($percentages);
if ($percentages[0] != $percentages[1] && $percentages[0]) {
return $percentages[0].'% - '.$percentages[1].'%';
} else {
return $percentages[1].'%';
}
}
嘿,thnx。在哪里添加此代码?在functions.php?Hi@ArjanWassink中-是的,把它放在你的functions.php中-我刚刚更新了代码,以确保百分比在气泡中,所以确保从这里重新复制:)你有任何错误吗?我在自己的一个网站上测试过,效果非常好。你在哪里加的这个?我一点也没出错。该按钮持续显示“VERKOOP”,但不显示下降百分比。我在主题的functions.php中粘贴了这段代码。你收到我的消息了吗?