:::: MENU ::::
Posts tagged with: product model

Magento 2 get min price or max price from product collection

Today we learn about how to get min price and max price from product collection. First you have to get collection of product. Then their two predefined function for get min price getMinPrice and get max price getMaxPrice Now you can get the collection following tutorial Magento 2 get product collection.

Now we implement the code for getting min price and max price.

protected $_productCollectionFactory;

public function __construct(
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productFactory
    ) {
        $this->_productCollectionFactory = $productFactory;
    }
    public function getProductCollection()
    {
      $productCollection = $this->_productCollectionFactory->create();
      $maxPrice = $productCollection>getMaxPrice();
     $minPrice = $productCollection>getMinPrice();
    }

}

You can see the example of Min Price and Max Price in Layer collection.Follow Magento\Catalog\Model\Layer\Filter\AbstractFilter.php

$this->getLayer()->getProductCollection()->getMaxPrice();
$this->getLayer()->getProductCollection()->getMinPrice();
Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018


Magento 2 get product collection

Today we talk about Magento 2 get product collection. First we need to familiar with model collection structure of magento 2. Magento 2 use the collection class for get all record from database table. SO let start with coding. We are using the Factory method here.

protected $_productCollectionFactory;

public function __construct(
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productFactory
    ) {
        $this->_productCollectionFactory = $productFactory;
    }
    public function getProductCollection()
    {
      return   $this->_productCollectionFactory->create()->addAttributeToSelect('*');
     
     }

To Fetch Data in phtml file

$productCollection = $block->getProductCollection();

foreach ($productCollection as $product) {

   
        echo $product->getName().'<br />';

}
Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018


Magento 2 overriding Rewriting helper

Today we talk about Magento 2 overriding Rewriting helper. In our daily coding routine we need to customization with core files. For that we need to overriding or rewrite the core classes because modify the core classes is not good practice. If you modify the code classes when you update the new version your code will be loss. So today we learn how to Magento 2 overriding or Rewriting helper. In start we are overriding core Product helper. We are jut checking our rewrite is working or not. You can modify the helper as you want following this tutorial. But first you need to know How to create module in Magneto 2.For any suggestions & question, please feel free to drop a comment.

Overriding or Rewrite Magento 2 Helper

sometimes, you may want to add new classes or override different functions in your Magento 2 module. Therefore, Magento 2 came up with the Helpers that are the right entity to fulfill your needs. A Helper in Magento 2 is an object that contains practical methods.Now we start to overriding or rewriting core Product helper. The helper we are overriding is Product helper.
Step 1.

Create a di.xml file in a following directory QaisarSatti/HelloWorld/etc/

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Helper\Product" type="QaisarSatti\HelloWorld\Helper\Rewrite\Product" />
</config>

Step 2
The step2 to overriding or rewriting Magento2 Helper is to create a Product.php helper file in the following directory QaisarSatti/HelloWorld/Helper/Rewrite/

<?php
/**
* Catalog Product overridingHelper
*
* @category QaisarSatti
* @package QaisarSatti_HelloWorld
* @author Qaisar Satti
*
*/

namespace QaisarSatti\HelloWorld\Helper\Rewrite;
class Product extends \Magento\Catalog\Helper\Product
{
   public function __construct()
   {
     echo "Our Rewrite Working"; die();
   }
}

As following code you can rewrite or override any Magento 2 helper using the same method.

Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018