-1

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

每次我用这个例子打电话时,我总是会出错。

4

2 回答 2

4

试试下面的代码:

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

我希望它会帮助你

于 2018-03-21T07:54:16.340 回答
0

按类别获取产品的更好和更实际的方法 - 通过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 中关于“搜索标准统一处理”的信息很少 - 更好且当前正确的过滤、排序模型的方法。

这里 Magento 文档关于搜索条件统一处理

您也可以注册自己的 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>
于 2021-10-13T12:23:36.583 回答