Top 6 Killer Performance Tips For Magento 2 Developers

Posted by
killer-performance-tips-magento-2

The eCommerce world has been evolving incredibly, amounting to such amount as 10% of retail sales in 2018. Magento is in the same trend, as a dynamic e-commerce platform, it experiences changes and improvements every minute around the world. 

Meanwhile, there still exist performance issues in Magento 2 stores. Some of such issues cannot be solved overnight but need long-term solutions. Therefore, utilizing small Magento 2 tips and tricks is a need for developers and store admins to optimize the performance of their websites. 

In this article, we have summed up the top 6 performance tips for your Magento 2 performance, aiming to help you attract more customers and boost sales. Let’s check them out!

Top Tips and Tricks for Better Magento 2 Performance

tips-and-tricks-magento-performance

Tip #1. Magento Optimization in Developer Mode

Magento 2 has been improved so much ever since it was released in 2010 with a bunch of more features and a brand new architecture. However, some tasks still need to be done by hand. Here we shall discuss the fine adjustments – the Magento 2 tips, to smoothen the customers’ experience at Magento 2 stores.

First thing is to activate the Developer mode: get SSH and use the following command:  


php bin/magento deploy:mode:set developer

The next step is to go to Stores → Configuration → Advanced → Developer. As you have set the Developer mode active, the Developer tab will appear. Any changes for better Magento 2 performance you make on this tab shall be carried out in other modes as well. 

magento-performance-tips-javascript-settings

Now in the Developer tab, we shall look at these Magento 2 tips:

  • Javascript Settings: You need to check whether Merge Javascript Files and Enable Javascript Bundling are turned off. You might as well make sure that CSS Merge and CSS Minify are on by scrolling further down the menu. 
  • CSS Settings: You are supposed to turn on the two CSS settings: Merge CSS Files and Minify CSS Files. 
magento-performance-tip-css-settings

To get back to the Production Mode, use this line: 


php bin/magento deploy:mode:set production

If you wish to ensure that you have returned to Production Mode, then this one:


php bin/magento deploy:mode:show

It then gets back to you:


Current application mode: production.

Tip #2. Javascript Quote Escape and URL Site Escaper

To escape the strings you are writing in javascript, it is not sufficient to just use the Javascript escape character, ‘\’, in the HTML context. Instead, you can use the following code snippet for better Magento 2 performance: 


$this->jsQuoteEscape ($item->getName());

You can also set to escape the website URL with this code snippet:


$this->escapeJsQuote($_SERVER['REQUEST_URI'])

In addition, it is possible to convert the format from number to currency with this Magento 2 tip:


$this->helper('Magento\Framework\Pricing\Helper\Data')->currency(number_format(50,2),true,false);

Tip #3. Use of Varnish Caching

magento-performance-tip-varnish-caching

It’s certain that you usually switch between the Production and Developer Modes. There comes this Magento 2 tip which is not available in Default nor Production Mode, but Developer only. 

In the natural Magento 2 setting, a built-in Magento catching tool administers the cache management, yet we have a better choice here for you. 

Navigate to Configuration in Stores, then choose Advanced → System, expand the Full Page Cache section. Now you are to disable Use system value in Catching Application, then select Varnish Cache (Recommended). Lastly, do not forget to click the Save Config button for applying changes. 

Tip #4. Object Order Restoration On Order Confirmation Page

magento-performance-tip-restoration-object

For retrieving order objects like ID or details on order-success page in Magento 2 performance, first, you need to get to the Checkout Session:


 /**
* Checkout session
*
* @var \Magento\Checkout\Model\Session
*/
protected $_checkoutSession;
 
/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Sales\Model\Order $salesOrderFactory
* @param \Magento\Checkout\Model\Session $checkoutSession
*/
public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Sales\Model\Order $salesOrderFactory,
        \Magento\Checkout\Model\Session $checkoutSession,
        array $data = []
    ) {
        $this->_checkoutSession = $checkoutSession;
        parent::__construct($context, $data);
    }

 

Next, you retrieve the order you want with this code snippet:


/**
* Retrieve current order
*
* @return \Magento\Sales\Model\Order
*/
public function getOrder()
{
   $orderId = $this->_checkoutSession->getLastOrderId();
   return $this->_salesFactory->load($orderId);
}

Alternatively, this Magento 2 tip also suggests you can use the following different code:


$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orderId = $objectManager->create('\Magento\Checkout\Model\Session')
                           ->getLastOrderId();      
$order = $objectManager->create('\Magento\Sales\Model\Order')
                           ->load($orderId);

Tip #5. Product Object and Request Object Retrievation

It is possible to fetch back not only product object page but request object as well with these 2 code snippets:

For product object:


$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('\Magento\Catalog\Model\Product')
                           ->load(291);

For request object:


$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('\Magento\Framework\App\Request\Http')
                            ->getParam('product', 0);  

Tip #6. Removal of Redundant Extensions

magento-performance-tip-remove-redundancies

This one is the last Magento 2 tip, but also the easiest and most important one for better Magento 2 performance. A research used to show that one-third of the extensions installed in a store are left untouched and just take up space and resources. 

One important thing to notice is that there is a real difference between disabling and deleting an extension. Setting an extension inactive means that it is still present, and slows down the website due to the cron time and space taken in the module directory. Therefore, the best choice is to remove unused extensions.

In SSH, get to your Magento install section. Let’s just call the extension you want to get rid of as ‘Extensiontodelete’, then apply these code:


php bin/magento module:disable Extensiontodelete --clear-static-content
php bin/magento setup:upgrade
cd app/code/Extensiontodelete/
rm -rf Extensiontodelete

The above command has certainly disabled the useless extension and cleared its static content. Later, get Magento revise itself and clear cache. Lastly, go to the extension folder and delete the disabled extension above. Repeat these steps with other unused extensions and you will get your site spotless. 

The Last Words

better-magento-performance-conclusion

The 6 Magento 2 tips listed are surely just some of the most needed actions for optimizing your website. You need other factors like content, image or hosting to get the Magento 2 performance as expected. We hope you find the performance tricks useful. Happy works with Magento!

Leave a Reply

Your email address will not be published. Required fields are marked *