Skip to main content

Posts

Magento 2 How to get product collection using controller

Getting product collection in Magento 2 Step 1: Declare in Vendor_ModuleName Block Step 2: Display product collection in phtml file .. Step 1: Declare in Vendor_ModuleName Block You will use a block class of the module Vendor_ModuleName, then possibly inject the object of `\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory` in the constructor of the module’s block class.     app/code/Vendor/ModuleName/Block/ProductCollection.php add code this code  <?php namespace Vendor\ModuleName\Block; class ProductCollection extends \Magento\Framework\View\Element\Template { protected $_productCollectionFactory; public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory, array $data = [] ) { $this->_productCollectionFactory = $product
Recent posts

Magento 2: How to use redirect URl in Router.php

Module name - **Aks_CustomUrlRouter** You have to create below listed files 1. Registration.php :: app/code/Aks/CustomUrlRouter/registration.php <?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Aks_CustomUrlRouter', __DIR__ ); ?> 2. Module.xml :: app/code/Aks/CustomUrlRouter/etc/module.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> <module name="Aks_CustomUrlRouter" setup_version="2.0.0"></module> </config> 3. di.xml :: app/code/Aks/CustomUrlRouter/etc/di.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

Magento 2 Update product image attributes value using custom script programetically

Magento 2 Update product image programetically <?php //increase the max execution time @ini_set('max_execution_time', -1); //memory_limit @ini_set('memory_limit', -1); error_reporting(E_ALL); ini_set('display_errors', '1'); use \Magento\Framework\App\Bootstrap; require __DIR__ . '/app/bootstrap.php'; $bootstrap = Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $instance = \Magento\Framework\App\ObjectManager::getInstance(); $state = $objectManager->get('\Magento\Framework\App\State'); $state->setAreaCode('frontend'); $product_collections = $instance->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collections = $product_collections->create(); foreach ($collections as $product) { if ($product->getId() == 2052) { $product = $objectManager->create('\Magento\Catalog\Model\Pr