0

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).

1

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();
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.