Magento类别中的最低价格

时间:2015-10-08 09:12:16

标签: php magento

所以我创建了一个sub.phtml文件,通过撤消类别的子类别并在页面上显示相关的标题,图像等,为我创建类别登陆页面。

但是我也希望将产品的最低价格放在这些类别中("从19.99和#34;例如)

这是我目前的代码:

<?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>

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

要获得某个类别中价格最低的产品,您可以执行以下操作:

$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; ?>