简体   繁体   English

Magento 类别中的最低价格

[英]Lowest price in Magento category

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.因此,我创建了一个 sub.phtml 文件,它通过检索类别的子类别并将它们显示在具有相关标题、图像等的页面上来为我创建类别登录页面。

However I also would like to put the lowest price of a product in those categories ("From 19.99" for example)但是,我也想将产品的最低价格放在这些类别中(例如“从 19.99”)

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.我在这里先向您的帮助表示感谢。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM