【问题标题】:product collection from sub-categories id from object manager where subcategory was created in custom category which located out of root category来自对象管理器的子类别 ID 的产品集合,其中子类别是在位于根类别之外的自定义类别中创建的
【发布时间】:2019-01-26 12:10:01
【问题描述】:

我想要基于类别 ID 的特定类别的所有产品。

我已经尝试过下面的代码但没有解决:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$cateinstance = $objectManager->create('Magento\Catalog\Model\CategoryFactory');
$allcategoryproduct = $cateinstance->create()->load($cateid)->getProductCollection()->addAttributeToSelect('*');

[{"entity_id":"245","sku":"U001","type_id":"simple","category_id":null,"cat_index_position":"0","product_image":"\/p\/e\/peach-boy-boys-shirts_1_a8c05a1421e5dcf8ad5308398e0b3591_1.jpg","small_image":"\/p\/e\/peach-boy-boys-shirts_1_a8c05a1421e5dcf8ad5308398e0b3591_1.jpg","thumbnail":"\/p\/e\/peach-boy-boys-shirts_1_a8c05a1421e5dcf8ad5308398e0b3591_1.jpg","productName":"DY Andheri Peach School Uniform","price":"500.0000","isProductInStock":19,"isProductEndorsed":false,"option_collection":[]},{"entity_id":"250","sku":"U003","type_id":"configurable","category_id":null,"cat_index_position":"0","product_image":"\/0\/a\/0aea950b-1c0a-4adf-85ce-c521555462651534842479667-chalk-by-pantaloons-girls-dresses-4851534842479574-1.jpg","small_image":"\/0\/a\/0aea950b-1c0a-4adf-85ce-c521555462651534842479667-chalk-by-pantaloons-girls-dresses-4851534842479574-1.jpg","thumbnail":"\/0\/a\/0aea950b-1c0a-4adf-85ce-c521555462651534842479667-chalk-by-pantaloons-girls-dresses-4851534842479574-1.jpg","productName":"DY Andheri Girls School Uniform","price":"450.0000","isProductInStock":0,"isProductEndorsed":false,"option_collection":[{"sku":"DY Andheri Girls School Uniform-S","product_id":"250","attribute_id":"151","default_title":"Size","value_index":"30","display_label":"S","price":"450.0000"},{"sku":"DY Andheri Girls School Uniform-M","product_id":"250","attribute_id":"151","default_title":"Size","value_index":"31","display_label":"M","price":"450.0000"},{"sku":"DY Andheri Girls School Uniform-L","product_id":"250","attribute_id":"151","default_title":"Size","value_index":"32","display_label":"L","price":"450.0000"},{"sku":"DY Andheri Girls School Uniform-XL","product_id":"250","attribute_id":"151","default_title":"Size","value_index":"33","display_label":"XL","price":"450.0000"}]}]

【问题讨论】:

  • 您发布的代码的结果是什么?发生什么了?如果有什么错误?
  • 我想要基于 ROOT 类别之外的类别的产品集合

标签: magento-2.0


【解决方案1】:

您可以通过

获取特定类别的产品集合
        $categoryId = 3;
        $objectManager =  \Magento\Framework\App\ObjectManager::getInstance(); 
        $categoryFactory = $objectManager->create('Magento\Catalog\Model\CategoryFactory');
        $category = $categoryFactory->create()->load($categoryId);

        $productCollectionFactory = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
        $collection = $productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        $collection->addCategoryFilter($category);
        $collection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
        $collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);

您可以将 categoryId 替换为您的类别 ID,集合将返回所有已启用且可见性设置为目录搜索且属于指定类别的产品

【讨论】:

  • 它显示 ProductCollection 用于根目录内的类别而不是根目录外的类别,我的类别放在根目录之外
猜你喜欢
  • 1970-01-01
  • 2021-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多