skip to Main Content

I am trying to get product(s) name and price from product collection for that I have created a custom script for test.

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('MagentoFrameworkAppState');
$state->setAreaCode('adminhtml');
$collection = $objectManager->create('MagentoCatalogModelResourceModelProductCollectionFactory')->create();
try {
    
    echo $collection->addAttributeToSelect(['name','sku'])->getSelect();
} catch (Exception $exception) {
    echo $exception->getMessage()."n";
}

but when I run this script to check MySQL query I am getting below output:

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

How can I get only product name and price instead of whole data?

4

Answers


  1. As you’re trying to get a product collection it’s automatically going to get the main product entity table. It would need this in the query as a minimum as it needs the entity Id to be able to join with other tables to get the attributes required for the collection. You would not be able to retrieve your name attribute otherwise.
    Note that this table is quite small and doesn’t include any other attributes apart from the sku code.

    If you dunp the data returned you’ll see it doesn’t actually grab all of the attributes, but it does need the main table.

    If you have a specific need for only those two fields it would be better to use a custom query rather than a product collection.

    Login or Signup to reply.
  2. I suggest you try this

    public function getProductCollection()
    {
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        $collection->setPageSize(3); // fetching only 3 products
        foreach ($productCollection as $product) {
            $productPrice = $product->getPrice();
            $productName = $product->getName();
        }
    }
    
    Login or Signup to reply.
  3. Try this,

    <?php
    
    namespace BRINDAHelloWorldBlock;
    class HelloWorld extends MagentoFrameworkViewElementTemplate
    {
        protected $productCollectionFactory;
        protected $categoryFactory;
        public function __construct(
            MagentoFrameworkViewElementTemplateContext $context,
            MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
            MagentoCatalogModelCategoryFactory $categoryFactory,
            array $data = []
        ) {
            $this->productCollectionFactory = $productCollectionFactory;
            $this->categoryFactory = $categoryFactory;
            parent::__construct($context, $data);
        }
        public function getProductCollection()
        {
            $collection = $this->productCollectionFactory->create();
            $collection->setPageSize(3);
            foreach ($collection as $product)
            {
                echo "<pre>";
                print_r($product->getPrice());
                print_r($product->getName());
            }
            return $collection;
        }
    }
    
    Login or Signup to reply.
  4. <?php
        $objectManager = MagentoFrameworkAppObjectManager::getInstance();
        $productCollection = $objectManager->create('MagentoCatalogModelResourceModelProductCollectionFactory');
        $collection = $productCollection->create()->addAttributeToSelect('*')->load();
    
        foreach ($collection as $product){
            echo 'Name  =  '.$product->getName().'<br>';
            echo 'Price  =  '.$product->getFinalPrice().'<br>';
            echo 'Url  =  '.$product->getProductUrl().'<br>';
            echo 'Image = '.$product->getImage().'<br>';
        }
    ?>
    

    Try This…..

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search