开发者

Magento: get the lowest price of a grouped product by product ID?

I have a piece of code that returns product data including a price. However for a grouped product it doesn't give the price.

Is there a way to take the ID of the grouped product and get the lowest price of the group?

Here is my code:

<?php
    $id_array = strip_tags($this->getLayout()->createBlock('cms/block')->setBlockId('homepage-newest-product')->toHtml());
    $product_id = explode(',',$id_array);


    foreach ($product_id as $id):
        $productDetails = Mage::getModel('catalog/product')->load($id)->getData();
        //var_dump($productDetails);
    ?>
    <li class="a-center span-3">
        <a href="<?php echo $this->getBaseUrl() . $productDetails['url_path']; ?>"><img src="<?php echo $media_url . 'catalog/product' . $productDetails['thumbnail']; ?>" width="80" height="80"/></a>
        <a href="<?php echo $this->getBaseUrl() . $productDetails['url_path']; ?>" class="clearfix"><?php echo $productDetails['name']; ?></a>
        <?php if($productDetails['price']): ?>
        <span>
        <?php echo $_coreHelper->currency($productDetails['price'],true,false) ?>
        </span>
        <?php endif; ?>
    </li>
    <?php endforeach; ?>

Preferebly I'd have a function that takes the grouped ID and looks up the lowest price of the group, then returns the pri开发者_JAVA技巧ce.


The method looks like below:

public function prepareGroupedProductPrice($groupedProduct)
{
    $aProductIds = $groupedProduct->getTypeInstance()->getChildrenIds($groupedProduct->getId());

    $prices = array();
    foreach ($aProductIds as $ids) {
        foreach ($ids as $id) {
            $aProduct = Mage::getModel('catalog/product')->load($id);
            $prices[] = $aProduct->getPriceModel()->getPrice($aProduct);
        }
    }

    krsort($prices);
    $groupedProduct->setPrice(array_shift($prices));

    // or you can return price
}

Also you can prepare price with events, but this issue is the simplest way to get lowest group price.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜