Posts

Featured Post

Magento 2 How to create custom Log file ?

Assuming your module is in YourNamespace/YourModule: 1) Write Logger class in Logger/Logger.php: <?php namespace YourNamespace\YourModule\Logger; class Logger extends \Monolog\Logger { } 2) Write Handler class in Logger/Handler.php: <?php namespace YourNamespace\YourModule\Logger; use Monolog\Logger; class Handler extends \Magento\Framework\Logger\Handler\Base {     /**      * Logging level      * @var int      */     protected $loggerType = Logger::INFO;     /**      * File name      * @var string      */     protected $fileName = '/var/log/myfilename.log'; } Note: This is the only step which uses Magento code. \Magento\Framework\Logger\Handler\Base extends Monolog's StreamHandler and e.g. prepends the $fileName attribute with the Magento base path. 3) Register Logger in Dependency Injection etc/di.xml: <?xml vers...

Magento 2 call static block in phtml file

Method to call CMS static block in phtml file in Magento 2 Call your static block in phtml file: <?php echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('your_block_identifier')->toHtml(); ?> Display CMS Static Block In CMS Content: {{block class="Magento\\Cms\\Block\\Block" block_id="block_identifier"} Display CMS Static Block In XML: <referenceContainer name="content">      <block class="Magento\Cms\Block\Block" name="block_identifier">          <arguments>              <argument name="block_id" xsi:type="string">block_identifier</argument>          </arguments>      </block>  </referenceContainer> Happy Coding :)

Magento 2 SSH Commands

Below is the list of most important SSH / CLI commands for Magento 2 that I have found useful. To use these commands you will need to have SSH access to your server or use the Command Line for local access. Setup Upgrade Using Command Line php bin/magento setup:upgrade If you don’t want to remove pub/static files while installing/updating database then use following command. php bin/magento setup:upgrade --keep-generated Cache Clean Using Command Line php bin/magento cache:clean Cache Flush Using Command Line php bin/magento cache:flush View cache status Using Command Line php bin/magento cache:status Enable Cache Using Command Line php bin/magento cache:enable [cache_type] Disable Cache Using Command Line php bin/magento cache:disable [cache_type] Static Content Deploy Using Command Line (Use -f for force deploy on 2.2.x or later) php bin/magento setup:static-content:deploy Static Content Deploy For Particular Language Using Command Line php bin/ma...

Running cron in magento

Setting cron In your command line use this command sudo crontab -e //add your cron */1 * * * * /bin/sh /var/www/html/canon_2016/cron.sh >> /var/log/myjob.log 2>&1 sudo crontab -l (shows the list of cron) To test cron is running or not  try {     Mage::getConfig()->init()->loadEventObservers('crontab');     Mage::app()->addEventArea('crontab');     Mage::dispatchEvent('default'); $log = fopen(__FILE__.'.log', 'a'); fwrite($log, date("Y-m-d H:i:s").PHP_EOL); fclose($log); } catch (Exception $e) {     Mage::printException($e); } place this code at the bottom of cron.php file in magento root directory,it will create cron.php.log file,which will show when the cron job start

Magento 2 CSS and JavaScript not loading

If you are facing problem of css and js page load design after installation in magento2 please follow the following step-: open the terminal and navigate to magento web root  $ cd /var/www/html/magento2  Step 1.  $ php bin/magento setup:static-content:deploy  Step 2.  $ php bin/magento indexer:reindex Step 3. make sure apache “rewrite_module” is enable and then restart the server Step 4.  $ chown -R www-data:www-data /var/www/html/magento2  Step 5.  $ chmod -R 777 /var/www/html/magento2  Step 6. delete cache folder under var/cache The above step working. I hope this will work for you also. Command to change the mode (Developer,Production..etc) open the terminal and navigate to magento web root $ php bin/magento deploy:mode:show $ php bin/magento deploy:mode:set production

Event and Observers in Magento2

To create observer in magento2, first we need to define observer in file: app/code/Rahul/HelloWorld/etc/events.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">     <event name="controller_action_catalog_product_save_entity_after">         <observer name="Rahul_HelloWorld_Product_Save_After" instance="Rahul\HelloWorld\Observer\ProductSaveAfter" />     </event> </config> Note: There are different places to create files for different handlers.     To create observer for frontend you can create file under : app/code/Rahul/HelloWorld/etc/frontend/event.xml     To create observer for frontend you can create file under : app/code/Rahul/HelloWorld/etc/adminhtml/event.xml     To create observer for both end, you need to cre...

Create log file in Magento2

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log'); $logger = new \Zend\Log\Logger(); $logger->addWriter($writer); $logger->info('Your text message'); You can also print PHP objects and arrays like below : $logger->info(print_r($yourArray, true));