【问题标题】:How to get products by category id in magento 2.2.1? [closed]如何在 magento 2.2.1 中按类别 id 获取产品? [关闭]
【发布时间】:2018-03-20 10:55:51
【问题描述】:

我在 Magento 2.2.1 中工作,我正在尝试通过其类别 ID 获取类别的产品集合。

每次我打电话给using this example,总是报错。

【问题讨论】:

  • 在此处添加错误和您的代码。

标签: php magento2.2


【解决方案1】:

试试下面的代码:

<?php
$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();        

$categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');
$categoryHelper = $objectManager->get('\Magento\Catalog\Helper\Category');
$categoryRepository = $objectManager->get('\Magento\Catalog\Model\CategoryRepository');
$store = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore();

$categoryId = 47; // YOUR CATEGORY ID
$category = $categoryFactory->create()->load($categoryId);

$categoryProducts = $category->getProductCollection()
                             ->addAttributeToSelect('*');

foreach ($categoryProducts as $product) 
{
    $imageUrl = $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product' . $product->getImage();
    ?>

     <div class="product-container">
                  <a href="<?= $product->getProductUrl(); ?>">

                     <div class="new-arrivals-image"><img src="<?= $imageUrl;?>"></div>
                     <div class="product-name"><span class="name"><?= $product->getName(); ?></span></div>
                  </a>
                  <div class="price"><span class="pt"><?= $product->getPrice(); ?></span></div>
               </div>

<?php
}
?>

希望对你有帮助

【讨论】:

    【解决方案2】:

    通过ProductRepository 和内置过滤器(来自 Magento 2.2)更好、更实际地按类别获取产品

    public function __construct(
        ProductRepositoryInterface $productRepository,
        SearchCriteriaBuilder $criteriaBuilder
    ) {
        $this->productRepository = $productRepository;
        $this->criteriaBuilder = $criteriaBuilder;
    }
    
    /**
     * @return ProductInterface[]
     */
    public function getProducts(): array
    {
        $categoryIdsToExport = $this->config->getCategoriesToExport();
    
        return $this->productRepository->getList(
            $this->criteriaBuilder
                //It's Custom Filter from di.xml
                ->addFilter('category_id', $categoryIdsToExport, 'in') 
                //Here you cat filter products in standart Magento way
                ->addFilter('status', \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
                ->addFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
                ->create()
        )->getItems();
    }
    

    不幸的是,stackexchange 中关于“搜索标准统一处理”的信息很少 - 更好且当前正确的过滤、排序模型的方法。

    Here Magento doc about Search Criteria Unify Processing

    您也可以注册自己的 CustomFilter 来过滤产品。 请参阅vendor/magento/module-catalog/etc/di.xml 中的示例:

    <virtualType name="Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\ProductFilterProcessor" type="Magento\Eav\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor">
        <arguments>
            <argument name="customFilters" xsi:type="array">
                <!-- You can specify your attribute and map a class to apply filter -->
                <item name="category_id" xsi:type="object">Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor\ProductCategoryFilter</item>
                <item name="store" xsi:type="object">Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor\ProductStoreFilter</item>
                <item name="store_id" xsi:type="object">Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor\ProductStoreFilter</item>
                <item name="website_id" xsi:type="object">Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor\ProductWebsiteFilter</item>
            </argument>
        </arguments>
    </virtualType>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多