Question

The following code has been added to the list.phtml but is working only for simple products. When they have an special price with a date range, a sale label is attached to the product image on the category pages. This is also displaying a new label on products that are set as NEW with a date range (the new label is working for all products).

I am trying to find a way to combine a special price check that works for simple products and for configurable products in the case that all child products of a configurable product have an special price.

<?php
$specialprice = $_product->getSpecialPrice();
$specialPriceFromDate = $_product->getSpecialFromDate();
$specialPriceToDate = $_product->getSpecialToDate();
$now = date("Y-m-d");
$newsFrom = substr($_product->getNewsFromDate(), 0, 10);
$newsTo = substr($_product->getNewsToDate(), 0, 10);
$today = time();
$class_has_new = "";
if ($newsTo != '' || $newsFrom != '') {
    if (($newsTo != '' && $newsFrom != '' && $now >= $newsFrom && $now <= $newsTo) || ($newsTo == '' && $now >= $newsFrom) || ($newsFrom == '' && $now <= $newsTo)) {
        $class_has_new = " has-new";
    }
}
if ($specialprice) {
    if ($today >= strtotime($specialPriceFromDate) && $today <= strtotime($specialPriceToDate) || $today >= strtotime($specialPriceFromDate) && is_null($specialPriceToDate)) { ?>
        <div class="label-product label-sale<?php echo $class_has_new; ?>">
            <span class="sale-product-icon">
                <?php echo __('Sale'); ?>
            </span>
        </div>
<?php }
}
?>
<?php
if ($newsTo != '' || $newsFrom != '') {
    if (($newsTo != '' && $newsFrom != '' && $now >= $newsFrom && $now <= $newsTo) || ($newsTo == '' && $now >= $newsFrom) || ($newsFrom == '' && $now <= $newsTo)) { ?>
        <div class="label-product label-new">
            <span class="new-product-icon"><?php echo __('New'); ?></span>
        </div>
    <?php }
} ?>
Was it helpful?

Solution

This should help you:

<?php if ($_product->getTypeId() == 'bundle') {
    $finalPrice = $_product->getPriceInfo()->getPrice('final_price')->getMinimalPrice()->getValue();
    $orgPrice = $_product->getPriceInfo()->getPrice('regular_price')->getMinimalPrice()->getValue();
} elseif ($_product->getTypeId() == 'grouped') {
    $usedProds = $_product->getTypeInstance(true)->getAssociatedProducts($product);
    $finalPrice = 0;
    $orgPrice = 0;
    foreach ($usedProds as $child) {
        if ($child->getId() != $product->getId()) {
            $finalPrice += $child->getFinalPrice();
            $orgPrice += $child->getPrice();
        }
    }
} elseif ($_product->getTypeId() == 'configurable') {
    $finalPrice = $_product->getPriceInfo()->getPrice('final_price')->getMinimalPrice()->getValue();
    $orgPrice = $_product->getPriceInfo()->getPrice('regular_price')->getAmount()->getValue();
} else { //simple product
    $finalPrice = $_product->getFinalPrice();
    $orgPrice = $_product->getPrice();
}
?>

Now you can just compare final price and org price.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top