php-在WooCommerce中选择变化的销售价格后显示折扣百分比
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php-在WooCommerce中选择变化的销售价格后显示折扣百分比,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2471字,纯文字阅读大概需要4分钟。
内容图文
![php-在WooCommerce中选择变化的销售价格后显示折扣百分比](/upload/InfoBanner/zyjiaocheng/691/4dce74463dd140749b1ef521d15d721b.jpg)
我在下面的这段代码中显示了用户选择了价格变化后的价格变化
add_filter( 'woocommerce_show_variation_price', 'filter_show_variation_price', 10, 3 );
function filter_show_variation_price( $condition, $product, $variation ){
if( $variation->get_price() === "" ) return false;
else return true;
}
我需要计算将显示的促销价格和正常价格之间的折扣百分比.但是仅在选择变体之后,而不是之前(因为我将从通过css之前显示的价格中删除价格).
我认为this answer接近了,但还没有到.
解决方法:
全部以下代码仅适用于单个产品页面上的可变产品.
我以前有女仆提供的链接答案中的第二个代码,效果很好.我做了一些小改动,只针对可变产品.
我添加了一个附加的挂钩函数,该函数将仅删除可变产品标题下的显示价格.
这是所有需要的代码:
// Always Display the selected variation price for variable products (already working)
add_filter( 'woocommerce_show_variation_price', 'filter_show_variation_price', 10, 3 );
function filter_show_variation_price( $condition, $product, $variation ){
if( $variation->get_price() === "" ) return false;
else return true;
}
// Remove the displayed price from variable products in single product pages only
add_action( 'woocommerce_single_product_summary', 'remove_the_displayed_price_from_variable_products', 9 );
function remove_the_displayed_price_from_variable_products() {
global $product;
// Just for variable products
if( ! $product->is_type('variable') ) return;
// Remove the displayed price from variable products
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
}
// Display the selected variation discounted price with the discounted percentage for variable products
add_filter( 'woocommerce_format_sale_price', 'woocommerce_custom_sales_price', 10, 3 );
function woocommerce_custom_sales_price( $price, $regular_price, $sale_price ) {
global $product;
// Just for variable products on single product pages
if( $product->is_type('variable') && is_product() ) {
// 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>';
}
return $price;
}
代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中.
此代码已经过测试,并且仅适用于WooCommerce版本3
内容总结
以上是互联网集市为您收集整理的php-在WooCommerce中选择变化的销售价格后显示折扣百分比全部内容,希望文章能够帮你解决php-在WooCommerce中选择变化的销售价格后显示折扣百分比所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。