Reputation: 11
So I have created a sub.phtml file which creates category landing pages for me by retrieving the child categories of a category and displaying them on the page with the relevant title, image etc.
However I also would like to put the lowest price of a product in those categories ("From 19.99" for example)
Here is my current code:
<?php
$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$categories = $category->getCollection()
->addAttributeToSelect(array('name', 'thumbnail'))
->addAttributeToFilter('is_active', 1)
->addIdFilter($category->getChildren())
?>
<?php
$lowest_product = Mage::getModel('catalog/product')->getCollection()
->setOrder('price', 'asc')
->setPageSize(1) // just select one product, which will be the cheapest
->setCurPage(1)
->joinField(
'category_id', 'catalog/category_product', 'category_id',
'product_id = entity_id', null, 'left'
) // add a join so you can filter by category id
->addAttributeToSelect('*')
->addAttributeToFilter('category_id', $category->getId())
->addAttributeToFilter('status', 1) // only select active products
->addAttributeToFilter(array(array('attribute'=>'visibility', 'eq'=>"4"))) // only products which are visible
->getFirstItem(); // only get the first item
?>
<div class="container">
<?php foreach ($categories as $category): ?>
<a class="catA" href="<?php echo $category->getUrl() ?>">
<div class="catBox">
<div class="catBoxImage"><img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $category->getThumbnail() ?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>"></div>
<div class="catBoxText">
<h2><?php echo $category->getName() ?></h2>
<p class="catP">From: <span class="red"><?php echo Mage::helper('core')->currency(Mage::helper('tax')->getPrice($lowest_product, $lowest_product->getPrice()), true, false) ?></span></p>
</div>
</div>
</a>
<?php endforeach; ?>
</div>
Thanks for the help in advance.
Upvotes: 1
Views: 1280
Reputation: 5463
To get the lowest priced product within a category you can do the following:
$lowest_product = Mage::getModel('catalog/product')->getCollection()
->setOrder('price', 'asc')
->setPageSize(1) // just select one product, which will be the cheapest
->setCurPage(1)
->joinField(
'category_id', 'catalog/category_product', 'category_id',
'product_id = entity_id', null, 'left'
) // add a join so you can filter by category id
->addAttributeToSelect('*')
->addStoreFilter(Mage::app()->getStore()->getId())
->addAttributeToFilter('category_id', $category->getId())
->addAttributeToFilter('status', 1) // only select active products
->addAttributeToFilter(array(array('attribute'=>'visibility', 'eq'=>"4"))) // only products which are visible
->getFirstItem(); // only get the first item
Now you can call:
<?php echo Mage::helper('core')->currency(Mage::helper('tax')->getPrice($lowest_product, $lowest_product->getPrice()), true, false) ?>
UPDATED
<?php
$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$categories = $category->getCollection()
->addAttributeToSelect(array('name', 'thumbnail'))
->addAttributeToFilter('is_active', 1)
->addIdFilter($category->getChildren())
?>
<div class="container">
<?php foreach ($categories as $category): ?>
<?php
$lowest_product = Mage::getModel('catalog/product')->getCollection()
->setOrder('price', 'asc')
->setPageSize(1) // just select one product, which will be the cheapest
->setCurPage(1)
->joinField(
'category_id', 'catalog/category_product', 'category_id',
'product_id = entity_id', null, 'left'
) // add a join so you can filter by category id
->addAttributeToSelect('*')
->addStoreFilter(Mage::app()->getStore()->getId())
->addAttributeToFilter('category_id', array('in' => $category->getId()))
->addAttributeToFilter('status', 1) // only select active products
->addAttributeToFilter(array(array('attribute'=>'visibility', 'eq'=>"4"))) // only products which are visible
->getFirstItem(); // only get the first item
?>
<a class="catA" href="<?php echo $category->getUrl() ?>">
<div class="catBox">
<div class="catBoxImage"><img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $category->getThumbnail() ?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>"></div>
<div class="catBoxText">
<h2><?php echo $category->getName() ?></h2>
<p class="catP">From: <span class="red"><?php echo Mage::helper('core')->currency(Mage::helper('tax')->getPrice($lowest_product, $lowest_product->getPrice()), true, false) ?></span></p>
</div>
</div>
</a>
<?php endforeach; ?>
Upvotes: 1