如何获取Magento 2中所有产品的列表?


Answers:


22

您应该使用Magento\Catalog\Model\ProductRepositoryMagento\Catalog\Model\ResourceModel\Product\Collection根据需要。您可以使用两种方法来获取包含所有数据的产品实例。

示例1(存储库):

/**
 * @param \Magento\Catalog\Model\ProductRepository $productRepository
 * @param \Magento\Framework\Api\SearchCriteriaInterface $criteria
 * @param \Magento\Framework\Api\Search\FilterGroup $filterGroup
 * @param \Magento\Framework\Api\FilterBuilder $filterBuilder
 * @param \Magento\Catalog\Model\Product\Attribute\Source\Status $productStatus
 * @param \Magento\Catalog\Model\Product\Visibility $productVisibility
 */
public function __construct(
    \Magento\Catalog\Model\ProductRepository $productRepository,
    \Magento\Framework\Api\SearchCriteriaInterface $criteria,
    \Magento\Framework\Api\Search\FilterGroup $filterGroup,
    \Magento\Framework\Api\FilterBuilder $filterBuilder,
    \Magento\Catalog\Model\Product\Attribute\Source\Status $productStatus,
    \Magento\Catalog\Model\Product\Visibility $productVisibility
) {
    $this->productRepository = $productRepository;
    $this->searchCriteria = $criteria;
    $this->filterGroup = $filterGroup;
    $this->filterBuilder = $filterBuilder;
    $this->productStatus = $productStatus;
    $this->productVisibility = $productVisibility;

    $this->getProductData();
}

/**
 * @return \Magento\Cms\Model\Block|null
 * @throws \Magento\Framework\Exception\NoSuchEntityException
 */
protected function getProductData()
{

    $this->filterGroup->setFilters([
        $this->filterBuilder
            ->setField('status')
            ->setConditionType('in')
            ->setValue($this->productStatus->getVisibleStatusIds())
            ->create(),
        $this->filterBuilder
            ->setField('visibility')
            ->setConditionType('in')
            ->setValue($this->productVisibility->getVisibleInSiteIds())
            ->create(),
    ]);

    $this->searchCriteria->setFilterGroups([$this->filterGroup]);
    $products = $this->productRepository->getList($this->searchCriteria);
    $productItems = $products->getItems();

    return $productItems;
}

结果:

资料库

示例2(集合):

/**
 * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
 * @param \Magento\Catalog\Model\Product\Attribute\Source\Status $productStatus
 * @param \Magento\Catalog\Model\Product\Visibility $productVisibility
 * @throws \Magento\Framework\Exception\LocalizedException
 */
public function __construct(
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    \Magento\Catalog\Model\Product\Attribute\Source\Status $productStatus,
    \Magento\Catalog\Model\Product\Visibility $productVisibility
) {
    $this->productCollectionFactory = $productCollectionFactory;
    $this->productStatus = $productStatus;
    $this->productVisibility = $productVisibility;
}

/**
 * @return \Magento\Framework\DataObject[]
 * @throws \Magento\Framework\Exception\LocalizedException
 */
public function getProducts()
{
    /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
    $collection = $this->productCollectionFactory->create();
    $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
    $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
    $collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()])
        ->addAttributeToFilter('visibility', ['in' => $this->productVisibility->getVisibleInSiteIds()]);

    return $collection->getItems();
}

结果:

采集


嗨,@ siarhey,我的代码得到的结果是空的。您对此有任何提示吗?可能是因为我试图从cron任务中检索列表吗?
Lorenzo S

@LorenzoS您好,您在使用哪个示例1或2?删除所有过滤器/条件时是否存在任何产品?
Siarhey Uchukhlebau '02

如何在屏幕快照中获得带有对象树的“变量”选项卡?
DevonDahon


很好的答案,@ SiarheyUchukhlebau,但这是什么意思。 如果仅需要产品数据,请使用头等舱。如果您需要获取产品实例,请使用第二类。
Yehia A.Salam's

2

使用REST API调用:

https://{host}/rest/V1/products/?searchCriteria=

检索所有产品。如上所述,“ searchCriteria”是必需的参数,但可以留空。


2

最受支持的答案是有效的,但我想在这里提及,直接注入产品存储库实现将导致违反服务合同原则,而Magento希望开发人员对此进行纠正。您应该注入\Magento\Catalog\Api\ProductRepositoryInterface $productRepository而不是实现\Magento\Catalog\Model\ProductRepository $productRepository。这样,您将拥有将来升级的空间。底线是尽可能使用服务合同。


0

您可以通过api请求执行此操作。看看默认的M2 API

认为您最好的选择是catalogProductRepositoryV1

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.