Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 显示Woocommerce产品的默认折扣价格和百分比_Php_Wordpress_Woocommerce_Percentage_Price - Fatal编程技术网

Php 显示Woocommerce产品的默认折扣价格和百分比

Php 显示Woocommerce产品的默认折扣价格和百分比,php,wordpress,woocommerce,percentage,price,Php,Wordpress,Woocommerce,Percentage,Price,我试图在Woocommerce上显示产品的折扣百分比。最初提供的解决方案(如下链接)有效,但是如果存在默认的产品变量集,则不会显示折扣百分比。只有当选择更改为其他变体时,才会显示百分比折扣。如何修改代码以立即显示百分比折扣,而不必选择其他变体 源代码: (备选方案2) 2) 储蓄率: add_filter( 'woocommerce_get_price_html', 'change_displayed_sale_price_html', 10, 2 ); function change_

我试图在Woocommerce上显示产品的折扣百分比。最初提供的解决方案(如下链接)有效,但是如果存在默认的产品变量集,则不会显示折扣百分比。只有当选择更改为其他变体时,才会显示百分比折扣。如何修改代码以立即显示百分比折扣,而不必选择其他变体

源代码: (备选方案2)

2) 储蓄率:

add_filter( 'woocommerce_get_price_html',     'change_displayed_sale_price_html', 10, 2 );
function change_displayed_sale_price_html( $price, $product ) {
     // Only on sale products on frontend and excluding min/max price on variable products
    if( $product->is_on_sale() && ! is_admin() && ! $product->is_type('variable')){
         // Get product prices
         $regular_price = (float) $product->get_regular_price(); // Regular price
         $sale_price = (float) $product->get_price(); // Active price (the "Sale price" when on-sale)

         // "Saving Percentage" calculation and formatting
         $precision = 1; // Max number of decimals
         $saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), 1 ) . '%';

         // Append to the formated html price
         $price .= sprintf( __('<p class="saved-sale">Save: %s</p>', 'woocommerce' ), $saving_percentage );
    }
    return $price;
}
// For product variations
add_filter( 'woocommerce_available_variation', 'custom_variation_price_saving_percentage', 10, 3 );
function custom_variation_price_saving_percentage( $data, $product, $variation ) {
    $active_price  = $data['display_price'];
    $regular_price = $data['display_regular_price'];

    if( $active_price !== $regular_price ) {
        $saving_percentage = round( 100 - ( $active_price / $regular_price * 100 ), 1 ) . '%';
        $data['price_html'] .= sprintf( __('<p class="saved-sale">Save: %s</p>', 'woocommerce' ), $saving_percentage );
    }
    return $data;
}
add_filter('woocommerce_get_price_html','change_display_sale_price_html',10,2);
功能更改\u显示\u销售\u价格\u html($price,$product){
//仅在前端销售产品,不包括可变产品的最低/最高价格
如果($product->is_on_sale()&&!is_admin()&&!$product->is_type('variable')){
//获取产品价格
$regular_price=(float)$product->get_regular_price();//常规价格
$sale_price=(float)$product->get_price();//有效价格(销售时的“销售价格”)
//“保存百分比”计算和格式设置
$precision=1;//最大小数位数
$saving_percentage=round(100-($sale_price/$regular_price*100),1)。“%”;
//附加到格式化的html价格
$price.=sprintf(“

保存:%s

,“woocommerce”),$saving\u百分比); } 返回$price; }
当变量产品存在默认的选定变体(在售)并正确显示折扣百分比时,也会显示

现在,对于可变产品一般显示的价格范围,您不能显示折扣百分比,,因为所有变体都需要出售,并且每个变体的折扣百分比可能不同

对于所选的销售中的产品变体价格,您还可以使用以下方法获得节省百分比:

add_filter( 'woocommerce_get_price_html',     'change_displayed_sale_price_html', 10, 2 );
function change_displayed_sale_price_html( $price, $product ) {
     // Only on sale products on frontend and excluding min/max price on variable products
    if( $product->is_on_sale() && ! is_admin() && ! $product->is_type('variable')){
         // Get product prices
         $regular_price = (float) $product->get_regular_price(); // Regular price
         $sale_price = (float) $product->get_price(); // Active price (the "Sale price" when on-sale)

         // "Saving Percentage" calculation and formatting
         $precision = 1; // Max number of decimals
         $saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), 1 ) . '%';

         // Append to the formated html price
         $price .= sprintf( __('<p class="saved-sale">Save: %s</p>', 'woocommerce' ), $saving_percentage );
    }
    return $price;
}
// For product variations
add_filter( 'woocommerce_available_variation', 'custom_variation_price_saving_percentage', 10, 3 );
function custom_variation_price_saving_percentage( $data, $product, $variation ) {
    $active_price  = $data['display_price'];
    $regular_price = $data['display_regular_price'];

    if( $active_price !== $regular_price ) {
        $saving_percentage = round( 100 - ( $active_price / $regular_price * 100 ), 1 ) . '%';
        $data['price_html'] .= sprintf( __('<p class="saved-sale">Save: %s</p>', 'woocommerce' ), $saving_percentage );
    }
    return $data;
}
//用于产品变体
添加过滤器('woocommerce\u available\u variation'、'custom\u variation\u price\u Save\u percentage',10,3);
函数自定义\变化\价格\保存\百分比($data、$product、$VARIANCE){
$active_price=$data['display_price'];
$regular_price=$data['display_regular_price'];
如果($active\u price!==$regular\u price){
$saving_percentage=round(100-($active_price/$regular_price*100),1)。“%”;
$data['price\u html'].=sprintf('p class=“saved sale”>保存:%s

,'woocommerce'),$saving\u百分比); } 返回$data; }
代码进入活动子主题(或活动主题)的functions.php文件

然后,对于简单的产品,您将使用:

// For simple products
add_filter( 'woocommerce_get_price_html', 'change_displayed_sale_price_html', 10, 2 );
function change_displayed_sale_price_html( $price, $product ) {
     // Only on sale products on frontend and excluding min/max price on variable products
    if( $product->is_on_sale() && ! is_admin() && $product->is_type('simple') ){
         // Get product prices
         $regular_price = (float) $product->get_regular_price(); // Regular price
         $sale_price = (float) $product->get_price(); // Active price (the "Sale price" when on-sale)

         // "Saving Percentage" calculation and formatting
         $precision = 1; // Max number of decimals
         $saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), $precision ) . '%';

         // Append to the formated html price
         $price .= sprintf( __('<p class="saved-sale">Save: %s</p>', 'woocommerce' ), $saving_percentage );
    }
    return $price;
}
//对于简单产品
添加过滤器('woocommerce\u get\u price\u html','change\u displayed\u sale\u price\u html',10,2);
功能更改\u显示\u销售\u价格\u html($price,$product){
//仅在前端销售产品,不包括可变产品的最低/最高价格
如果($product->is_on_sale()&&&!is_admin()&&&$product->is_type('simple')){
//获取产品价格
$regular_price=(float)$product->get_regular_price();//常规价格
$sale_price=(float)$product->get_price();//有效价格(销售时的“销售价格”)
//“保存百分比”计算和格式设置
$precision=1;//最大小数位数
$saving_percentage=四舍五入(100-($sale_price/$regular_price*100),$precision)。“%”;
//附加到格式化的html价格
$price.=sprintf(“

保存:%s

,“woocommerce”),$saving\u百分比); } 返回$price; }

代码进入活动子主题(或活动主题)的functions.php文件。

答案不起作用,可能检查如果get\u price==0,然后查看get\u variation\u price('min'),看看是否可以从中获得价格
get\u regular\u price()
可能也会返回0抱歉,但当存在默认选择的变体(在售)并显示折扣百分比时,该代码也会起作用…