3

In my magento store majority of products are "grouped products". Right now when you go in category you can see the label under the product image says "Price starting at" and the lower price of two associated products.

For example if one associated product cost $150 and the second $210, so the price in category view will be displayed as "Price starting at - $150".

In my case I want that the grouped product price would be displayed as the sum of two associated products without any labels like "Price starting at".

It should be displayed as a sum which in this case equal to "$360".

I hope someone could help me to find the solution.

3
  • 1
    For your requirement you need to customized magento functionality. Commented Dec 19, 2016 at 13:32
  • Maybe you know what exactly? Commented Dec 20, 2016 at 0:56
  • Have you try any thing for this? Commented Dec 20, 2016 at 3:04

3 Answers 3

6

I just had the same requirement. For a quick solution copy theme file

Magento_GroupedProduct\templates\product\price\final_price.phtml

to your custom theme and sum prices in template with

<?php

/*
 * Show combined price instead of minimal price for grouped products.
 */

$products = $block->getSaleableItem()->getTypeInstance()->getAssociatedProducts($block->getSaleableItem());

$priceForAll = 0;
foreach ($products as $product) {
    $priceForAll += $product->getPrice();
}

?>

<div class="price-box price-final_price" data-role="priceBox" data-product-id="284">
    <span class="price-container price-final_price tax weee">
            <span id="product-price-284" data-price-amount="<?php echo $priceForAll?>" data-price-type="finalPrice" class="price-wrapper ">
                <span class="price"><?php echo number_format($priceForAll,2,',','.')?> €</span>
            </span>
    </span>
</div>

Please note that the final price formatting in html is a quick hack and just fits my requirements. A more felxible solution taking into account magento's price formatting features is welcome!

2
  • Can you show all code in final_price.phtml file? Commented Jan 6, 2017 at 19:22
  • Edited the code snippet. Commented Jan 9, 2017 at 8:25
0

I've refactored the code of @skymeissner:

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/**
 * Template for displaying grouped product price
 */
?>
<?php
$allProducts = $block->getSaleableItem()->getTypeInstance()->getAssociatedProducts($block->getSaleableItem());
$priceHelper = $this->helper(\Magento\Framework\Pricing\Helper\Data::class);


if ($allProducts) {

    $priceForAll = 0;
    foreach ($allProducts as $product) {
        $priceForAll += $product->getPrice();
    }
}

?>

<div class="price-box" itemprop="offers" itemscope itemtype="http://schema.org/Offer">
    <?php if ($priceForAll > 0 && \Magento\Framework\Pricing\Render::ZONE_ITEM_VIEW != $block->getZone()): ?>
        <p class="grouped-price">
            <span class="price-label">
                <?= /* @escapeNotVerified */ __('Starting at') ?>
            </span>
            <span class="price">
                <?= $formattedPrice = $priceHelper->currency($priceForAll, true, false); ?>
            </span>
        </p>
    <?php endif ?>
</div>
0

Adding to the above answers, if you would like to account for default quantities and show the final price as a sum of those values (instead of assuming qty 1 per line), you can change the following code:

$priceForAll += $product->getPrice();

To

$priceForAll += ($product->getPrice() * ($product->getQty());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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