Magento 2 : Add discount label on product page (already in Category view)
-
27-03-2021 - |
Question
Hi the Magento Community,
I would like to add a discount label on my pictures on my product pages : I already have one for each product on category page (see picture below) but not on product pages.
I took the code from the category, but I don't know where I should paste it and if it will work with fotorama.
My question is : Is it possible and which file I need to edit ?
Here is the code (I use Porto Theme) :
<?php
$product_label = "";
if($_product_label_config['sale_label']) {
$orgprice = $_product->getPrice();
$specialprice = $_product->getSpecialPrice();
$specialfromdate = $_product->getSpecialFromDate();
$specialtodate = $_product->getSpecialToDate();
$today = time();
if(!$specialprice)
$specialprice = $orgprice;
if($specialprice < $orgprice) {
if((is_null($specialfromdate) && is_null($specialtodate)) || ($today >= strtotime($specialfromdate) && is_null($specialtodate)) || ($today <= strtotime($specialtodate) && is_null($specialfromdate)) || ($today >= strtotime($specialfromdate) && $today <= strtotime($specialtodate))){
if($_product_label_config['sale_label_percent']) {
$save_percent = 100-round(($specialprice/$orgprice)*100);
$product_label .= '<div class="product-label sale-label">'.'-'.$save_percent.'%'.'</div>';
} else {
$product_label .= '<div class="product-label sale-label">'.$_product_label_config['sale_label_text'].'</div>';
}
}
}
}
if($_product_label_config['new_label']) {
$now = date("Y-m-d");
$newsFrom= substr($_product->getData('news_from_date'),0,10);
$newsTo= substr($_product->getData('news_to_date'),0,10);
if ($newsTo != '' || $newsFrom != ''){
if (($newsTo != '' && $newsFrom != '' && $now>=$newsFrom && $now<=$newsTo) || ($newsTo == '' && $now >=$newsFrom) || ($newsFrom == '' && $now<=$newsTo)) {
$product_label .= '<div class="product-label new-label">'.$_product_label_config['new_label_text'].'</div>';
}
}
}
if($product_label)
echo '<div class="product-labels">'.$product_label.'</div>';
?>
No correct solution
OTHER TIPS
You can turn on a developer setting which will show you which template is loading each element on a page. Follow the steps on Magento's documentation for it:
https://devdocs.magento.com/guides/v2.3/frontend-dev-guide/themes/debug-theme.html#debug-theme-templ
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange