wordpress - 如何使用“WooCommerce 的折扣规则”插件 woocommerce 以销售百分比和主页显示销售额
问题描述
使用时,在文件Discount Rules for WooCommerce
中使用此钩子代码时,我在每个产品上都获得 100% 的折扣。function.php
但是它通过销售完美地展示所有东西是完美的,但是当我使用钩子时,它没有以百分比格式显示产品的完美销售,只显示每种产品的 100% 折扣。现在我想以正确的值获得百分比格式的折扣。当我到处搜索然后发现我的钩子 woocommerce_sale_flash
但它只适用于 woocommerce 插件我什么Discount Rules for WooCommerce
插件。但是在Discount Rules for wooCommerce
插件中我没有找到任何钩子来查看产品左上角的百分比格式的折扣。
add_filter( 'woocommerce_sale_flash', 'ask_percentage_sale', 11, 3 );
function ask_percentage_sale( $text, $post, $product ) {
$discount = 0;
if ( $product->get_type() == 'variable' ) {
$available_variations = $product->get_available_variations();
$maximumper = 0;
for ($i = 0; $i < count($available_variations); ++$i) {
$variation_id=$available_variations[$i]['variation_id'];
$variable_product1= new WC_Product_Variation( $variation_id );
$regular_price = $variable_product1->get_regular_price();
$sales_price = $variable_product1->get_sale_price();
if( $sales_price ) {
$percentage= round( ( ( $regular_price - $sales_price ) / $regular_price ) * 100 ) ;
if ($percentage > $maximumper) {
$maximumper = $percentage;
}
}
}
$text = '<span class="onsale">' . $maximumper . '%</span>';
} elseif ( $product->get_type() == 'simple' ) {
$percentage = round( ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100 );
$text = '<span class="onsale">' . $percentage . '% off</span>';
}
return $text;
}
我尝试使用此代码,但它以 woocommerce 销售产品插件的正确百分比显示,但对于“Woo 折扣规则”未显示正确百分比,它始终显示 100% 百分比。请告诉我任何获得百分比折扣的方向。
解决方案
推荐阅读
- android - NetworkCallback 在启动时不起作用(API 版本 22 到 API 版本 26)
- java - 解压分块的 GZIP HTTP 响应
- java - 创建在外部 jar spring boot 中配置的 bean
- python - Pycharm的Python枚举值类型提示失败?
- python - 递归地展平复杂的python dicts列表
- git - 如何使用适用于 Mac 的 SourceTree v4.1.1 更新 git 凭据
- r - 在 R 中使用 Matchit 以 1:5 的比例仅获取完全匹配的案例
- javascript - 在 React.js 中仅检测浏览器/选项卡关闭或重新加载事件
- android - 如何获取 android:forceDarkAllowed 生成的 styles.xml?
- node.js - 使用自制软件安装特定 nodejs 版本时出错