Created
June 27, 2015 07:52
-
-
Save psaikali/1cb5adf6adc6cb6557ac to your computer and use it in GitHub Desktop.
WordPress WooCommerce : afficher le (plus grand) pourcentage de réduction dans le macaron de promo d'un produit (woocommerce/loop/sale-flash.php)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Single Product Sale Flash | |
* | |
* @author WooThemes | |
* @package WooCommerce/Templates | |
* @version 1.6.4 | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; // Exit if accessed directly | |
} | |
global $post, $product; | |
if ($product->is_on_sale()) : | |
// Dans le cas où le produit est "variable" (plusieurs variations de produits existantes) | |
if ($product->product_type == 'variable') : | |
$available_variations = $product->get_available_variations(); | |
$max_percentage = 0; | |
// On passe au crible toutes les variations et calcule le pourcentage de réduction de chacune. | |
// Si ce pourcentage de réduction est le maximum, on le garde en mémoire dans $max_percentage | |
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->regular_price; | |
$sales_price = $variable_product1->sale_price; | |
$percentage = round(((($regular_price - $sales_price) / $regular_price) * 100), 1); | |
if ($percentage > $max_percentage) { | |
$max_percentage = $percentage; | |
} | |
} | |
echo apply_filters( 'woocommerce_sale_flash', '<span class="promo-star">-' . round($max_percentage) . '%</span>', $post, $product ); | |
// Si le produit n'est pas "variable" mais simple, nous n'avons qu'un prix normal et un prix promo... fastoche ! | |
elseif ($product->product_type == 'simple') : | |
$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 ); | |
echo apply_filters( 'woocommerce_sale_flash', '<span class="promo-star">-' . round($percentage) . '%</span>', $post, $product ); | |
endif; | |
endif; ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment