Magento 2 : Get Product Collection

Posted by

在Magento2.4.2中同同时如何获取多个产品信息,即批量获取产品列表。

在这里我将获取产品数据的方法写在Data文件里面以便都可以调用。

app/code/Yaoli/PromotionalPage/Helper/Data.php

<?php
/**
 * Created By : yshuq
 * Date : 2021/12/03
 */

namespace Yaoli\PromotionalPage\Helper;

use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\UrlInterface;

class Data extends AbstractHelper
{
    /**
     * @var CollectionFactory
     */
    private  $productCollectionFactory;

    /**
     * @param CollectionFactory $productFactory
     * @param Context $context
     */
    public function __construct(
        CollectionFactory $productFactory,
        Context $context
    )
    {
        $this->productCollectionFactory = $productFactory;
        parent::__construct($context);
    }

    /**
     * @param string $ids
     * @param string $discount
     * @param string $coupon
     * @return array
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
  public function getProductListInfo($ids = '',$discount = '',$coupon = '')
    {
     
   } 
  
}

1. 获取产品集合

  public function getProductListInfo($ids = '',$discount = '',$coupon = '')
    {
       //Load Product Collection
        $collection = $this->productCollectionFactory->create();
        $collection->setPageSize(2);
        foreach ($collection as $product)
        {   echo "<pre>";
            print_r($product->getData());
            echo "</pre>";
        }
        die();
   } 

输出

Array
(
    [entity_id] =&gt; 2
    [attribute_set_id] =&gt; 4
    [type_id] =&gt; virtual
    [sku] =&gt; USK00001
    [has_options] =&gt; 0
    [required_options] =&gt; 0
    [created_at] =&gt; 2021-08-20 05:55:46
    [updated_at] =&gt; 2021-08-30 08:52:19
    [is_salable] =&gt; 1
    [store_id] =&gt; 1
)
Array
(
    [entity_id] =&gt; 3
    [attribute_set_id] =&gt; 4
    [type_id] =&gt; virtual
    [sku] =&gt; USK00002
    [has_options] =&gt; 0
    [required_options] =&gt; 0
    [created_at] =&gt; 2021-08-20 06:19:33
    [updated_at] =&gt; 2021-08-30 08:58:20
    [is_salable] =&gt; 1
    [store_id] =&gt; 1
)

2. 加载具有特定属性的产品集合

获取商品的名称和sku 属性

 public function getProductListInfo()
    {
        //Load Product Collection
        $collection = $this->productCollectionFactory->create();
        $collection->addAttributeToSelect(['name','sku']);
        $collection->setPageSize(1);
        foreach ($collection as $product)
        {   echo "<pre>";
            print_r($product->getData());
            echo "</pre>";
        }
        die();
}

输出

Array
(
    [entity_id] => 2
    [attribute_set_id] => 4
    [type_id] => virtual
    [sku] => USK00001
    [has_options] => 0
    [required_options] => 0
    [created_at] => 2021-08-20 05:55:46
    [updated_at] => 2021-08-30 08:52:19
    [is_salable] => 1
    [name] => Anno 1800 - PC
    [store_id] => 1
)

3. 加载具有所有属性的产品集合:

获取全部产品属性信息

public function getProductListInfo()
    {
        //Load Product Collection
        $collection = $this->productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        $collection->setPageSize(1);
        foreach ($collection as $product)
        {   echo "<pre>";
            print_r($product->getData());
            echo "</pre>";
        }
        die();
}

输出

Array
(
    [entity_id] => 2
    [attribute_set_id] => 4
    [type_id] => virtual
    [sku] => USK00001
    [has_options] => 0
    [required_options] => 0
    [created_at] => 2021-08-20 05:55:46
    [updated_at] => 2021-08-30 08:52:19
    [is_salable] => 1
    [status] => 1
    [visibility] => 4
    [quantity_and_stock_status] => 1
    [tax_class_id] => 0
    [price] => 84.990000
    [special_price] => 82.490000
    [name] => Anno 1800 - PC
    [meta_title] => Buy Anno 1800, Anno Uplay Key - ukeydeal.com
    [meta_description] => Buy Anno 1800 key with secure payments and instant email delivery. Download and activate Anno 1800 Key directly from official website.
    [image] => /a/n/anno-1800.jpg
    [small_image] => /a/n/anno-1800.jpg
    [thumbnail] => /a/n/anno-1800.jpg
    [options_container] => container2
    [url_key] => anno-1800-pc-eu
    [msrp_display_actual_price_type] => 0
    [gift_message_available] => 0
    [swatch_image] => /a/n/anno-1800.jpg
    [featured] => 1
    [timershow] => 1
    [soft_sort] => ANNO 1800
    [special_from_date] => 2019-09-16 00:00:00
    [news_from_date] => 2012-04-19 00:00:00
    [description] => ”法师法师法师“;
[meta_keyword] => Buy Anno 1800 key, buy Anno 1800, Anno 1800 key, Anno 1800 cdkey, Anno 1800 Uplay key, buy Anno 1800 game code, buy Anno 1800 code, Anno 1800 PC key.
    [store_id] => 1
)

4.按多个类别获取产品集合:

打印集合查询:

 $collection = $this->productCollectionFactory->create();
 echo "<br>". $collection->getSelectSql();
 echo "<br>". $collection->getSelect()->__toString();
 echo "<br>". $collection->getSelect()->assemble();

上面三个方法是等效的,都是输出下面结果

SELECT `e`.* FROM `catalog_product_entity` AS `e`
SELECT `e`.* FROM `catalog_product_entity` AS `e`
SELECT `e`.* FROM `catalog_product_entity` AS `e`