Поэтому я создал файл sub.phtml, который создает для меня целевые страницы категории, отбрасывая дочерние категории категории и отображая их на странице с соответствующим заголовком, изображением и т.д.
Однако я также хотел бы поставить самую низкую цену продукта в этих категориях (например, "с 19.99")
Вот мой текущий код:
<?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>
Спасибо за помощь заранее.
Чтобы получить самый дешевый продукт в категории, вы можете сделать следующее:
$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
Теперь вы можете позвонить:
<?php echo Mage::helper('core')->currency(Mage::helper('tax')->getPrice($lowest_product, $lowest_product->getPrice()), true, false) ?>
ОБНОВЛЕНО
<?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; ?>