如何在 magento 2.2.1 中按类别 ID 获取产品?
How to get products by category id in magento 2.2.1?
我在 Magento 2.2.1 中工作,我试图通过类别 ID 获取类别的产品集合。
每次调用using this example时,总是出错。
试试下面的代码:
<?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
}
?>
希望对你有所帮助
按类别获取产品的更好、更实际的方法 - 通过 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>
我在 Magento 2.2.1 中工作,我试图通过类别 ID 获取类别的产品集合。
每次调用using this example时,总是出错。
试试下面的代码:
<?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
}
?>
希望对你有所帮助
按类别获取产品的更好、更实际的方法 - 通过 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>