Question

The following function works only for simple products (when they have an special price, a sale label is attached to the product image):

public function getLabels($product) {
    $labels = [];

    if ($product->getSpecialPrice()) {
        $labels['sale'] = 'Sale';
    }

    return $labels;
}

Now I am wondering how to combine a check that works for simple products and for configurable products (thus also in the case that all child products of a configurable product have an special price).

Was it helpful?

Solution

try this code block on listing page ( works for me ) , in my case all child products have same price and special price is applied, you can use logic something like this:

if ($_product->getTypeId() == "configurable") {
    $_children = $_product->getTypeInstance()->getUsedProducts($_product);

    foreach ($_children as $child){
         $salePrice = $child->getFinalPrice();
         $regularPrice = $child->getPriceInfo()->getPrice('regular_price')->getAmount()->getValue();
        break;
        }
    } else {
        $salePrice = $_product->getPriceInfo()->getPrice('final_price')->getAmount()->getValue();
        $regularPrice = $_product->getPriceInfo()->getPrice('regular_price')->getAmount()->getValue();
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top