Magento 2: Get all Categories of any/current Product

This article shows how we can get list of all categories from current product or any particular product as well. Both ways (Dependency Injection & Object Manager way) are shown below: Using Dependency Injection (DI) Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory, \Magento\Catalog\Model\ProductRepository and \Magento\Framework\Registry classes … Read more

Magento 2: Get Product Collection

This article shows how we can get product collection in Magento 2. Using Dependency Injection (DI) Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory class in the constructor of my module’s block class. app/code/Chapagain/HelloWorld/Block/HelloWorld.php <?php namespace Chapagain\HelloWorld\Block; class HelloWorld extends \Magento\Framework\View\Element\Template { protected $_productCollectionFactory; public function __construct( … Read more

Magento: Unable to select custom attribute on product collection

Problem: I had added a new product attribute. Suppose the attribute code is ‘test’. I have been trying to fetch/select that newly added product attribute. Here is my Magento collection code:- $collection = Mage::getModel('catalog/product') ->getCollection() ->addAttributeToSelect('test') ->addAttributeToFilter('url_key', 'SOME_URL_KEY') ->getFirstItem(); I don’t see the attribute ‘test’ when I print the collection data. echo "<pre>"; print_r($collection->getData()); echo … Read more

Magento: Adding OR and AND query condition to collection

Here is a quick tip to add OR query and AND query to collection object. I am implementing this on product collection. Getting product collection // PRODUCT COLLECTION $collection = Mage::getModel('catalog/product')->getCollection(); Adding AND query condition Filtering collection to select only those products whose sku is like ‘ch’ AND status is equal to 1 (i.e. enabled … Read more

Magento: Set Random Order in Collection using RAND()

Scenario: You have created a custom module. You have entered certain data in your database. You need to show the data randomly. Solution: In MySQL the rand() function helps the select query to fetch data randomly. In Magento, you can select random rows from MySQL table using Zend_Db_Expr(‘RAND()’). You have to create a new function … Read more

Magento: Very Useful Collection Functions

There are different important functions that you can implement in your Collection object. The functions are present in Varien_Data_Collection_Db class. The class file is present in lib/Varien/Data/Collection/Db.php Here are some of the functions that you can use in your collection object:- /** * Get Zend_Db_Select instance */ $collection->getSelect(); /** * Get collection size */ $collection->getSelect()->getSize(); … Read more

Magento: How to filter product collection using 2 or more category filters?

Suppose, you have a product collection and you want to filter it by category. Suppose, you want to filter it by more than one category. You can use addCategoryFilter if you have only one category. But, what if you want to filter by more than one category? Category ids are stored for product in a … Read more