Posts

How to override js in magento 2

 How to override js in magento 2 To override js we need requirejs-config.js requirejs-config.js need to create at [Vendor]/[Module]/view/frontend/requirejs-config.js var config = {     map: {         '*': {             'Magento_Checkout/js/model/shipping-rates-validator':'Vendor_Module/js/model/shipping-rates-validator'         }     } }; Create file at app/code/Vendor/Module/view/frontend/web/js/model/shipping-rates-validator.js Write your inside file and boom!!!  If developer mode is on and symlinks are generating clear cache bin/magento cache:clean If production mode is on , need to deploy static content files using command bin/magento s:s:d  If like efforts, Please share, comment and subscribe for future posts and inspire more.

Adding custom attribute to Customer

Adding custom attribute to Customer Create InstallData.php file at app/code/{Vendor}/{Module Name}/Setup/InstallData.php <?php namespace Hello\CustomerAttribute\Setup; use Magento\Eav\Setup\EavSetup; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Eav\Model\Config; use Magento\Customer\Model\Customer; class InstallData implements InstallDataInterface { private $eavSetupFactory; public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig) { $this->eavSetupFactory = $eavSetupFactory; $this->eavConfig       = $eavConfig; } public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); $eavSetup->addAttribute( \Magento\Customer\Model\Customer::ENTITY, 'mobil

Magento 2 Add Product Attribute Programmatically

 Magento 2 Add Product Attribute Programmatically Create file InstallData.php at app/code/Vendor/Module/Setup/InstallData.php <?php namespace Vendor\Module\Setup; use Magento\Eav\Setup\EavSetup; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; class InstallData implements InstallDataInterface { private $eavSetupFactory; public function __construct(EavSetupFactory $eavSetupFactory) { $this->eavSetupFactory = $eavSetupFactory; } public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); $eavSetup->addAttribute( \Magento\Catalog\Model\Product::ENTITY, 'brand_attribute', [ 'type' => 'text', 'backend' => '', 'frontend' =&g

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