【发布时间】:2020-11-02 04:31:54
【问题描述】:
根据这篇文章:
- Displaying the lowest variation price of variable products 还有这个话题:
- Display the discounted percentage near sale price in Single product pages for WC 3.0+
我一直在尝试在我的网站上随处显示可变和非可变产品的最低变化价格以及相关的折扣百分比。
这是我正在使用的当前代码:
add_filter( 'woocommerce_variable_price_html', 'bbloomer_variation_price_format', 10, 2 );
function bbloomer_variation_price_format( $price, $product ) {
// Main Price
$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
$price = $prices[0] !== $prices[1] ? sprintf( __( '%1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
// Sale Price
$prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
sort( $prices );
$saleprice = $prices[0] !== $prices[1] ? sprintf( __( '%1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
if ( $price !== $saleprice ) {
$price = '<del>' . $saleprice . '</del> <ins>' . $price . '</ins>';
}
return $price;
}
add_filter( 'woocommerce_format_sale_price', 'woocommerce_custom_sales_price', 10, 3 );
function woocommerce_custom_sales_price( $price, $regular_price, $sale_price ) {
// Getting the clean numeric prices (without html and currency)
$regular_price = floatval( strip_tags($regular_price) );
$sale_price = floatval( strip_tags($sale_price) );
// Percentage calculation and text
$percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
$percentage_txt = __(' Save ', 'woocommerce' ).$percentage;
return '<del>' . wc_price( $regular_price ) . '</del> <ins>' . wc_price( $sale_price ) . $percentage_txt . '</ins>';
}
add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'mmx_remove_select_text');
如果我只保留第二个功能,它适用于所有产品,但不适用于可变产品,后者在商店页面上显示价格范围。如果我添加第一个函数,它会显示最低产品变化的价格而不是价格范围,但旁边没有折扣百分比。
我可以将第二个函数的 % 代码添加到第一个函数中,但这很麻烦,有没有办法合并这些函数,以便我可以在任何地方显示折扣 %,并且在最低变化旁边显示可变产品?
【问题讨论】:
标签: php wordpress woocommerce product price