从类别ID获取产品集合


18

我正在尝试从类别ID获取产品集合。我尝试过的几件事在块中:

 $category = Mage::getModel('catalog/category')->load(123)
        ->getProductCollection();

    $category = Mage::getModel('catalog/category')->load(123);
    $products = $category->getProductCollection()->addCategoryFilter($category)
                         ->addAttributeToFilter('type_id', 'simple')
                         ->addAttributeToSelect('*');

也尝试只是从phtml做到这一点

$oCatId = Mage::getModel('catalog/category')->load(769); 
        $products->addCategoryFilter($oCatId);

这些都不起作用,但是我也没有看到任何错误。我看到了另一个似乎是相同问题的帖子:Magento-从特定类别获取产品,但是该方法对我也不起作用。谢谢你的帮助!

Answers:


32

尝试这个:

$products = Mage::getModel('catalog/category')->load($category_id)
 ->getProductCollection()
 ->addAttributeToSelect('*') // add all attributes - optional
 ->addAttributeToFilter('status', 1) // enabled
 ->addAttributeToFilter('visibility', 4) //visibility in catalog,search
 ->setOrder('price', 'ASC'); //sets the order by price

资料来源:http : //overlycaffeinated.com/blog/2011/02/get-all-sale-products-from-a-category-in-magento/

这应该起作用,因为它已经加载了类别,从而为您添加了类别过滤器:

Mage_Catalog_Model_Category

public function getProductCollection()
{
    $collection = Mage::getResourceModel('catalog/product_collection')
        ->setStoreId($this->getStoreId())
        ->addCategoryFilter($this);
    return $collection;
}

等一下,它仍然不起作用!

好的,所以您可能遇到更大的问题,大概是覆盖问题getProductCollection。因此,让我们尝试绕过该便捷方法:

$category = Mage::getModel('catalog/category')->load($category_id);
$products = Mage::getResourceModel('catalog/product_collection')
        ->setStoreId(Mage::app()->getStore()->getId())
        ->addCategoryFilter($category);

谢谢你的帮助!我感觉更近了,但仍然挣扎着。使用您的最后一种方法,我添加了 foreach ($products as $product) { echo $product->getId(); }并且得到了一系列ID。但是,当我echo $product->getName();什么也不尝试。我->addAttributeToSelect('*')也尝试添加但不行。多谢您的协助。
扎克

@philwinkle有没有办法获取某个类别的所有产品?我无法获得被禁用的人。我正在使用Mage::getResourceModel('catalog/product_collection')->setStoreId(Mage::app()->getStore()->getId())->addCategoryFilter($category)->addAttributeToSelect('sku');
Nick Rolando 2014年

9

如何使用前端通常需要产品列表的所有数据来加载产品集合:

$_categoryId = 123;
$category = Mage::getModel('catalog/category')->load($_categoryId );
$productCollection = $category->getProductCollection();
$productCollection
        ->addStoreFilter()
        ->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds())
        ->addMinimalPrice()
        ->addFinalPrice()
        ->addTaxPercents()
        ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
        ->addUrlRewrite();

这将为产品集合做准备,以加载必要的数据以显示价格,产品链接和配置为“用于产品清单”的任何属性,但不包括更多内容。


如何限制结果?->limit(5)没用。
Pedram Behroozi

->setPageSize(5)
Fabian Schmengler,2015年

@fschmengler,您;$category = Mage::getModel('catalog/category')->load(123)代码后错过了
Murtuza Zabuawala

5

下面的代码将为您提供类别ID为10的产品集合。

$categoryId = 10;    
$products = Mage::getSingleton('catalog/category')->load($categoryId)
            ->getProductCollection()
            ->addAttributeToSelect('*');


2

与使用属性加载产品集合并进行过滤相比,下面的代码要简单,有效得多,

$categoryId = 32; // Replace with your category

$category = Mage::getModel('catalog/category')
                 ->setStoreId(Mage::app()->getStore()->getId())
                 ->load($categoryId);

Mage::register('current_category', $category);

$products = Mage::getSingleton('catalog/layer')->getProductCollection();

echo $products->getSize();

0

在Magento 2中,尝试使用类别ID忘记分类收集

  $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  $subCategory =  $objectManager->create('Magento\Catalog\Model\Category')-> 
         load('categorey_id');
  foreach($subCategory as $subcat)
   { 
        print_r($subcat->getData();
  }

-2
<?php

$categoryid = 123; // Category Id

$category = new Mage_Catalog_Model_Category();
$category->load($categoryid);
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*'); 

foreach ($_productCollection as $_product) { ?>
    <div class="pr_section">
        <div class="pr_desc">
            <a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?></a>
            <?php echo $this->getReviewsSummaryHtml($_product, false, true); // Reviews ?>
                <?php echo $this->getPriceHtml($_product, true) ?>
                <?php echo $_product->getShortDescription();?>

                <?php if(!$_product->canConfigure() && $_product->isSaleable()): ?>
                    <p><button type="button" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Add to Cart')) ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></p>
                <?php elseif($_product->getStockItem() && $_product->getStockItem()->getIsInStock()): ?>
                    <p><a title="<?php echo $this->__('View Details') ?>" class="button btn-cart" href="<?php echo $_product->getProductUrl() ?>"><?php echo $this->__('View Details') ?></a></p>
                <?php else: ?>
                    <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                <?php endif; ?>
        </div>
        <div class="pr_img">
                <a href="<?php echo $_product->getProductUrl() ?>"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200); ?>" width="200" height="200" alt="" /></a> 
        </div>
<?php } ?>

详细获取产品系列。希望对您有所帮助。


1
我认为没有必要进行尸检,因为对此问题有足够的答案。此外,您对的使用存在new Mage_Catalog_Model_Category()疑问。
朱利安·拉查
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.