Skip to main content
WB2
AFFICHAGE DU POURCE...
 
Notifications
Retirer tout

AFFICHAGE DU POURCENTAGE DE RÉDUCTION DES PROMOTIONS

1 Publications
1 Utilisateurs
0 Reactions
4 Vu
(@kinepanoramix)
Publications: 1
New Member
Début du sujet
 
[#6980]

Bonjour,

J'ai un petit souci lorsque je mets ce code :

/* Afficher le pourcentage de réduction des promotions WooCommerce */

add_action( 'woocommerce_before_shop_loop_item_title', 'wpm_show_sale_percentage_loop', 25 );

function wpm_show_sale_percentage_loop() {
global $product;

// Si le produit est en promotion
if ( $product->is_on_sale() ) {

// Si le produit n'est pas un produit variable
if ( ! $product->is_type( 'variable' ) ) {
$max_percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
} else {

// Sinon si c'est un produit variable, on récupère les différents prix
foreach ( $product->get_children() as $child_id ) :
$variation = $product->get_child( $child_id );
$price = $variation->get_regular_price();
$sale = $variation->get_sale_price();

// On calcule le pourcentage
$percentage = $price != 0 && ! empty( $sale ) ? ( ( $price - $sale ) / $price * 100 ) : $max_percentage;
if ( $percentage >= $highest_percentage ) :
    $max_percentage = $percentage;
    $regular_price = $product->get_variation_regular_price( 'min' );
    $sale_price = $product->get_variation_sale_price( 'min' );
endif;
endforeach;
}

// On affiche le pourcentage en l'arrondissant
echo "
-" . round($max_percentage) . "%
"; }  }

dans le fichier « functions.php » de mon thème enfant. 

Lorsque j'utilise des produits variables, ce code ne fonctionne pas et m'affiche (sous l'image du produit) cette erreur :

Warning: Undefined variable $highest_percentage in /htdocs/wp-content/themes/estore enfant/functions.php on line 59<br style="box-sizing: border-box; font-family: 'Times New Roman'; font-size: 16px; text-align: center; white-space: normal;" /><br style="box-sizing: border-box; font-family: 'Times New Roman'; font-size: 16px; text-align: center; white-space: normal;" />Warning: Undefined variable $highest_percentage in /htdocs/wp-content/themes/estore enfant/functions.php on line 59<br style="box-sizing: border-box; font-family: 'Times New Roman'; font-size: 16px; text-align: center; white-space: normal;" /><br style="box-sizing: border-box; font-family: 'Times New Roman'; font-size: 16px; text-align: center; white-space: normal;" />Warning: Undefined variable $highest_percentage in /htdocs/wp-content/themes/estore enfant/functions.php on line 59

-33%
Auriez-vous une solution, svp?
Merci d'avance

 
Posté : 20/12/2023 2:11 pm
Share: