Posts

Showing posts from November, 2020

Magento 2 How to Override Core Block, Model and controller

Magento 2 How to Override Core Block, Model and controller Create di.xml file at app/code/{Vendor}/{Module}/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">         <preference for="Magento\Catalog\Controller\Product\View" type="{Vendor}\{Module}\Controller\Catalog\Product\View" /> </config> Create File View.php at app/code/{Vendor}/{Module}/Controller/Catalog/Product/ <?php namespace Vendor\Module\Controller\Catalog\Product; class View extends \Magento\Catalog\Controller\Product\View { public function execute(){ echo 'Hello Called from Overriden File '.__DIR_;exit; } } If like efforts, Please share, comment and subscribe for future posts and inspire more.

How to check if customer is logged in or not?

How to check if customer is logged in or not?   1) Using Object Manager. $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $customerSession = $objectManager->get('Magento\Customer\Model\Session'); if($customerSession->isLoggedIn()) {       // Yes customer is logged in } 2) From Controller. $this->_objectManager->get('Magento\Customer\Model\Session'); if($customerSession->isLoggedIn()) {    // Yes customer is logged in } 3) From Block or Model Or Helper. protected $customerSession; public function __construct(     \Magento\Customer\Model\SessionFactory $customerSession ) {     $this->customerSession = $customerSession; } public function isLoggedIn() // use this function in phtml file {     return $this->customerSession->create()->isLoggedIn(); } If like efforts, Please share, comment and subscribe for future posts and inspire more.

Magento 2 How to load product by id

1)   Object method $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $product = $objectManager->create('Magento\Catalog\Model\Product')->load($product_id); 2)  Factory Method <?php namespace Hello\Module\Block; class Product extends \Magento\Framework\View\Element\Template {   protected $_productloader;     public function __construct(         \Magento\Framework\View\Element\Template\Context $context,         \Magento\Catalog\Model\ProductFactory $_productloader     ) {         $this->_productloader = $_productloader;         parent::__construct($context);     }     public function getLoadProduct($id)     {         return $this->_productloader->create()->load($id);     } } If like efforts, Please share, comment and subscribe for future posts and inspire more.

Magento 2 popup modal

 How to use magento 2 modal for popup. There are tow ways to initiate popup modal in magento 2. 1) Using js. require(     [      'jquery',         'Magento_Ui/js/modal/modal'     ],     function(         $,         modal     ) {         var options = {             type: 'popup',             responsive: true,             innerScroll: true,             buttons: [{                 text: $.mage.__('Continue'),                 class: 'mymodal1',                 click: function () {                     this.closeModal();                 }             }]         };         var popup = modal(options, $('#popup-modal'));         $("#click-me").on('click',function(){             $("#popup-modal").modal("openModal");         });     } );  2) Binding in html using data-bind attribute of magneto. <button type="button" class="action" data-trigger="trigger"> <span