Posts

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();             ...

Magento 2 404 error for scripts and css

 Magento 2 404 error for scripts and css 1)  Please check mistakenly not deleted .htaccess file at pub/static path. 2) When Magento is not running on production mode , it will create symlinks for some static resource. So what to do is delete pub/static/frontend and pub/static/adminhtml   Alert : Do not delete .htaccess under pub/static/ folder.  3) Changing the behavior. Open up app/etc/di.xml and find the virtualType name="developerMaterialization" section. In that section one may find code item name="view_preprocessed" that needs to be modified or deleted. You can modify it by changing the contents from  Magento\Framework\App\View\Asset\MaterializationStrategy\Symlink   to  Magento\Framework\App\View\Asset\MaterializationStrategy\Copy This should  have solve your problem with the symlink creation. If like efforts, Please share, comment and subscribe for future posts and inspire more.

How to use Zend log Magento 2

 To use Zend log use below code in any PHP file. $yourmessageString = 'Hey from magento zend log'; $yourmessageArray = ['Hey from magento zend log Array ']; $writer = new \Zend\Log\Writer\Stream(BP . ‘/var/log/custom.log’); $logger = new \Zend\Log\Logger(); $logger->addWriter($writer); $logger->info($yourmessageString); $logger->info(print_r($yourmessageArray,true)); //for array and objects

Knock out js Short hands

 Knock out JS short hand properties. You can use  1.  this.knockoutProperty /= value;  instead  this.knockoutProperty(this.knockoutProperty() / value) 2.   this.knockoutProperty *= value;  instead  this.knockoutProperty(this.knockoutProperty() * value) 3. this.knockoutProperty -= value;  instead  this.knockoutProperty(this.knockoutProperty() - value) 4.  this.knockoutProperty += value;  instead  this.knockoutProperty(this.knockoutProperty() + value) Please like , share and subscribe.