Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.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 在WC 3.0+;的单个产品页面中显示接近销售价格的折扣百分比;_Php_Wordpress_Woocommerce_Hook Woocommerce_Discount - Fatal编程技术网

Php 在WC 3.0+;的单个产品页面中显示接近销售价格的折扣百分比;

Php 在WC 3.0+;的单个产品页面中显示接近销售价格的折扣百分比;,php,wordpress,woocommerce,hook-woocommerce,discount,Php,Wordpress,Woocommerce,Hook Woocommerce,Discount,我在主题的function.php中有这段代码来显示价格后的百分比,在WooCommerce v2.6.14中运行良好 但这段代码在WooCommerce 3.0+版上不再有效 我怎样才能解决这个问题 这是代码: // Add save percent next to sale item prices. add_filter( 'woocommerce_sale_price_html', 'woocommerce_custom_sales_price', 10, 2 ); function wo

我在主题的
function.php
中有这段代码来显示价格后的百分比,在WooCommerce v2.6.14中运行良好

但这段代码在WooCommerce 3.0+版上不再有效

我怎样才能解决这个问题

这是代码:

// Add save percent next to sale item prices.
add_filter( 'woocommerce_sale_price_html', 'woocommerce_custom_sales_price', 10, 2 );
function woocommerce_custom_sales_price( $price, $product ) {
    $percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
    return $price . sprintf( __(' Save %s', 'woocommerce' ), $percentage . '%' );
}
更新-2019年(避免四舍五入价格问题)-2017年(避免
NAN%
百分比值)

woocommerce\u sale\u price\u html
钩子已被woocommerce 3.0+中的另一个钩子替换,该钩子现在有3个参数(但不再是
$product
参数)


原始答案代码:这是功能类似的代码:

// Only for WooCommerce version 3.0+
add_filter( 'woocommerce_format_sale_price', 'woocommerce_custom_sales_price', 10, 3 );
function woocommerce_custom_sales_price( $price, $regular_price, $sale_price ) {
    $percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
    $percentage_txt = ' ' . __(' Save ', 'woocommerce' ) . $percentage;
    $price = '<del>' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . '</del> <ins>' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) . $percentage_txt : $sale_price . $percentage_txt ) . '</ins>';
    return $price;
}
//仅适用于WooCommerce 3.0版+
添加过滤器(“woocommerce\u格式\u销售价格”,“woocommerce\u自定义\u销售价格”,10,3);
功能商业\自定义\销售\价格($price、$常规\价格、$sale\价格){
$percentage=四舍五入($regular_price-$sale_price)/$regular_price*100)。“%”;
$percentage_txt='.'.\u('Save','woocommerce')。$percentage;
$price=''。(是数字($常规价格)?wc价格($常规价格):$常规价格)。“(是数字($sale价格)?wc价格($sale价格)。$percentage价格:$sale价格.$percentage价格)。”;
返回$price;
}
此代码位于活动子主题(或主题)的function.php文件中,或位于任何插件文件中。
此代码经过测试并正常工作。适用于WooCommerce版本3.0+

更新-2019年(避免四舍五入价格问题)-2017年(避免
NAN%
百分比值)

woocommerce\u sale\u price\u html
钩子已被woocommerce 3.0+中的另一个钩子替换,该钩子现在有3个参数(但不再是
$product
参数)


原始答案代码:这是功能类似的代码:

// Only for WooCommerce version 3.0+
add_filter( 'woocommerce_format_sale_price', 'woocommerce_custom_sales_price', 10, 3 );
function woocommerce_custom_sales_price( $price, $regular_price, $sale_price ) {
    $percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
    $percentage_txt = ' ' . __(' Save ', 'woocommerce' ) . $percentage;
    $price = '<del>' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . '</del> <ins>' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) . $percentage_txt : $sale_price . $percentage_txt ) . '</ins>';
    return $price;
}
//仅适用于WooCommerce 3.0版+
添加过滤器(“woocommerce\u格式\u销售价格”,“woocommerce\u自定义\u销售价格”,10,3);
功能商业\自定义\销售\价格($price、$常规\价格、$sale\价格){
$percentage=四舍五入($regular_price-$sale_price)/$regular_price*100)。“%”;
$percentage_txt='.'.\u('Save','woocommerce')。$percentage;
$price=''。(是数字($常规价格)?wc价格($常规价格):$常规价格)。“(是数字($sale价格)?wc价格($sale价格)。$percentage价格:$sale价格.$percentage价格)。”;
返回$price;
}

此代码位于活动子主题(或主题)的function.php文件中,或位于任何插件文件中。
此代码经过测试并正常工作。对于WooCommerce版本3.0+。

它返回NAN%@LoicTheAztec在我的例子中$regular\u price返回“65.99$”,不仅是常规价格,还有所有这些html,这就是它返回的原因nan@LoicTheAztec意识到是WooCommerce的计量价格计算器改变了价格。必须手动编辑插件的文件并以其他名称保存:(@LoicTheAztec没有钩子,只是插件中的一个函数,可以改变显示的价格。在插件文档中说,任何改变价格的行为都会产生重大冲突,这显然不适用于变化产品。它返回NAN%@LoicTheAztec在我的例子中$REQUALIC\U price RETRURING“65.99$”不仅仅是普通的价格,还有所有这些html,这就是它回归的原因nan@LoicTheAztec意识到是WooCommerce的测量价格计算器改变了价格。必须手动编辑插件的文件并以不同的名称保存:(@LoicTheAztec没有钩子,只是插件中的一个函数,可以改变显示的价格。在插件文档中说,任何改变价格的行为都会产生重大冲突,这显然不适用于变体产品。
// Only for WooCommerce version 3.0+
add_filter( 'woocommerce_format_sale_price', 'woocommerce_custom_sales_price', 10, 3 );
function woocommerce_custom_sales_price( $price, $regular_price, $sale_price ) {
    $percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
    $percentage_txt = ' ' . __(' Save ', 'woocommerce' ) . $percentage;
    $price = '<del>' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . '</del> <ins>' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) . $percentage_txt : $sale_price . $percentage_txt ) . '</ins>';
    return $price;
}