在 WooCommerce 中使用短代码将销售百分比添加到销售产品中 [英] Add sale percentage to products on sale with a shortcode in WooCommerce

查看:66
本文介绍了在 WooCommerce 中使用短代码将销售百分比添加到销售产品中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面的代码创建了一个短代码,它会打印变量简单产品的销售百分比(如果有的话).

My code below creates a shortcode that will print the sale percentage of variable and simple products if its on sale.

我对这个代码的问题是仅限简单产品,当产品不打折时,销售百分比显示为 100% 折扣.即没有输入销售价格

My problem with this code is that with simple products only, the sale percentage is showing as 100% OFF when the product is not on sale. I.e. no sale price entered

如果我设置了促销价,因此将产品设置为促销,则显示的百分比是正确的.

If I set a sale price, so setting the product to be on sale, the percentage shown is correct.

问题仅限于非销售的简单产品.根本不应显示销售百分比.

The problem is isolated to simple products that are not on sale. The sale percentage should not be being displayed at all.

add_shortcode( 'sale-percentage', 'add_percentage_to_sale_badge' );
function add_percentage_to_sale_badge() {
   global $product;
   if ( $product->is_type( 'variable' ) ) {
      $percentages = array(); // Get all variation prices
      $prices = $product->get_variation_prices(); // Loop through variation prices
      foreach ( $prices['price'] as $key => $price ) { // Only on sale variations
         if ( $prices['regular_price'][ $key ] !== $price ) {
            // Calculate and set in the array the percentage for each variation on sale
            $percentages[] = round( 100 - ( $prices['sale_price'][ $key ] / $prices['regular_price'][ $key ] * 100 ) );
         }
      }
      $percentage = $percentages ? max( $percentages ) . '% Off' : '';
   } else {
      $regular_price = (float) $product->get_regular_price();
      $sale_price = (float) $product->get_sale_price();
      $percentage = round( 100 - ( $sale_price / $regular_price * 100 ) ) . '% Off SIMPLE';
   }

   $percentage = $percentage ? esc_html__( '', 'woocommerce' ) . ' ' . $percentage : '';

   return $percentage;
}

推荐答案

实际上,您自己的问题已经有了答案:

You actually already have the answer yourself in your question:

  • "当产品不打折时.IE.没有输入销售价格.."

如果不填写,则无法用该数字进行计算,因此您必须在计算百分比之前添加一个if条件.

If this is not filled in, no calculations can be made with that number, Therefore you must add an if condition before you calculate the percentage.

所以改变

$regular_price = (float) $product->get_regular_price();
$sale_price = (float) $product->get_sale_price();
$percentage = round( 100 - ( $sale_price / $regular_price * 100 ) ) . '% Off SIMPLE';

// Get regular price
$regular_price = (float) $product->get_regular_price();

// Get sale price
$sale_price = (float) $product->get_sale_price();

// Condition, isset 
if ( isset ( $regular_price ) && isset ( $sale_price ) ) {
    $percentage = round( 100 - ( $sale_price / $regular_price * 100 ) ) . '% Off SIMPLE';   
}

  • isset — 确定变量是否已声明且不同比NULL
    • isset — Determine if a variable is declared and is different than NULL
    • 也可以使用,或者两者结合使用

      Could also be used, or a combination of both

      • empty — 判断变量是否为空
      • empty — Determine whether a variable is empty

      喜欢

      // NOT empty
      if ( ! empty ( $sale_price ) ) {
          // $percentage =...
      }
      

      这篇关于在 WooCommerce 中使用短代码将销售百分比添加到销售产品中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆