Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
527 views
in Technique[技术] by (71.8m points)

php - Adding the discount percentage to variable products on sale

I’m trying to add a discount percentage aside price in a site that uses WooCommerce.

I’ve applied this script for the standard price and the sale price:

// Add save percentage next to sale item prices.
add_filter( 'woocommerce_get_price_html', 'adventure_tours_sales_price', 10, 2 );
function adventure_tours_sales_price( $price, $product ){
  $percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
  return $price . sprintf( __(' Save %s', 'woocommerce' ), $percentage . '%' );
}

The script above works.

In front-end, I have the price percentage.

Now I want apply the same script to the product variation price.

I’ve checked the product variation option and tried something like this:

// Add save percentage next to sale item prices.
add_filter( 'woocommerce_get_price_html', 'adventure_tours_sales_price', 10, 2 );
function adventure_tours_sales_price( $price, $product ){
  if( $product->is_type( 'variable' ) ) {
    $percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
    return $price . sprintf( __(' Save %s', 'woocommerce' ), $percentage . '%' );
  }else{
    $percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
    return $price . sprintf( __(' Save %s', 'woocommerce' ), $percentage . '%' );
  }
}

But it does not work, the percentage is not applied to the price.

Nor in front-end.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Updated for WooCommerce version 3+ | Deprecated replacements

  • Replaced 'woocommerce_variable_sale_price_html' by 'woocommerce_variable_get_price_html'
  • Replaced 'woocommerce_sale_price_html' by 'woocommerce_get_price_html'
  • Replaced 'woocommerce_price()' by 'wc_price()'
  • Replaced WC_Product price properties by WC_Product price methods

For variable products is more complicated as you have 2 different locations with prices, the first one displays the minimal and maximal price (when you have multiple variations) and the second one displays the price from the selected variations. I have changed a lot your original code.

Here the correct code to display that custom dynamic labels arround discounted percentages:

add_filter('woocommerce_variable_get_price_html','adventure_tours_sales_price', 10, 2 );
add_filter('woocommerce_get_price_html','adventure_tours_sales_price', 10, 2 );
function adventure_tours_sales_price( $price, $product ){

    // Variables initialisation
    $regular_price = $product->get_regular_price();
    $sale_price    = $product->get_sale_price();
    $save_text     = __('Save', 'woocommerce') . ' ';

    if(!empty($sale_price)) {
        // Percentage calculation
        $percentage = '<span class="save-percent"> ' .$save_text . round( ( ( $regular_price -  $sale_price ) / $regular_price ) * 100 ) . '%</span>';

        $price = '<del class="strike">' . wc_price( $regular_price ) . '</del>
        <ins class="highlight">' . wc_price( $sale_price )  . $percentage . '</ins>';
    } else {
        $price = '<ins class="highlight">'.wc_price( $regular_price ).'</ins>';
    }
    return $price;
}

add_filter('woocommerce_variable_get_price_html', 'adventure_tours_sales_min_max_prices', 20, 2);
function adventure_tours_sales_min_max_prices( $price, $product) {

    // Variables initialisation
    $variation_min_reg_price = $product->get_variation_regular_price('min', true);
    $variation_max_reg_price = $product->get_variation_regular_price('max', true);
    $variation_min_sale_price = $product->get_variation_sale_price('min', true);
    $variation_max_sale_price = $product->get_variation_sale_price('max', true);
    $percentage_min = '';
    $percentage_max = '';
    $save_text     = __('Save', 'woocommerce') . ' ';

    // Percentage calculations
    if($variation_min_reg_price != $variation_min_sale_price)
        $percentage_min = '<span class="save-percent-min"> (' .$save_text . round( ( ( $variation_min_reg_price -  $variation_min_sale_price ) / $variation_min_reg_price ) * 100 ) . '%)</span>';
    if($variation_max_reg_price != $variation_max_sale_price)
        $percentage_max = '<span class="save-percent-max"> (' .$save_text . round( ( ( $variation_max_reg_price -  $variation_max_sale_price ) / $variation_max_reg_price ) * 100 ) . '%)</span>';

    if (($variation_min_reg_price != $variation_min_sale_price) || ($variation_max_reg_price != $variation_max_sale_price)) {
        $price = '<del class="strike">' . wc_price($variation_min_reg_price) . '-' . wc_price($variation_max_reg_price) .  '</del>
        <ins class="highlight">' . wc_price($variation_min_sale_price) . $percentage_min . ' - ' . wc_price($variation_max_sale_price) . $percentage_max . '</ins>';
    }
    return $price;
}

Code goes in functions.php file of your active child theme (or theme) or also in any plugin file.

Tested and works on Woocommerce version 3+

enter image description here


Related answers:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...