Saturday, December 15, 2018

Magento 2 Enable Template Path Hints

1.

Enabling template path hints via Admin panel


For storefront: Store > Configuration > Advanced > Developer > Debug > Enabled Template Path Hints for Storefront > Yes.
For Admin: Store > Configuration > Advanced > Developer > Debug > Enabled Template Path Hints for Admin > Yes.
Enabling/Disabling Template Path Hints via env.php file

app/etc/env.php

In this file you can change the value template_hints_storefront and template_hints_admin.

Enabling Template Path Hints via Command Line

php bin/magento dev:template-hints:enable
php bin/magento dev:template-hints:disable
php bin/magento cache:flush

Magento 2 checkout cause cart to empty

Magento 2 checkout cause cart to empty

I have implemented some changes that seem to resolve the issue but if anyone can look over this and feedback that would be great
vendor/magento/framework/Session/SessionManager.php
The regenerateId function needs replacing with this
public function regenerateId()
    {
        if (headers_sent()) {
            return $this;
        }

        if ($this->isSessionExists()) {
            $oldSessionId = session_id();            
            session_regenerate_id();   //regen the session
            $new_session_id = session_id();

            $_SESSION['new_session_id'] = $new_session_id;

            // Set destroy timestamp
            $_SESSION['destroyed'] = time();

            // Write and close current session;
            session_commit();
            $oldSession = $_SESSION;   //called after destroy - see destroy!
            // Start session with new session ID
            session_id($new_session_id);
            ini_set('session.use_strict_mode', 0);
            session_start();
            ini_set('session.use_strict_mode', 1);
            $_SESSION = $oldSession;
            // New session does not need them
            unset($_SESSION['destroyed']);
            unset($_SESSION['new_session_id']);  
        } else {
            session_start();
        }
        $this->storage->init(isset($_SESSION) ? $_SESSION : []);

        if ($this->sessionConfig->getUseCookies()) {
            $this->clearSubDomainSessionCookie();
        }
        return $this;
    }

and also the start function needs replacing with this
public function start()
    {
        if (!$this->isSessionExists()) {
            \Magento\Framework\Profiler::start('session_start');

            try {
                $this->appState->getAreaCode();
            } catch (\Magento\Framework\Exception\LocalizedException $e) {
                throw new \Magento\Framework\Exception\SessionException(
                    new \Magento\Framework\Phrase(
                        'Area code not set: Area code must be set before starting a session.'
                    ),
                    $e
                );
            }

            // Need to apply the config options so they can be ready by session_start
            $this->initIniOptions();
            $this->registerSaveHandler();
            if (isset($_SESSION['new_session_id'])) {
             // Not fully expired yet. Could be lost cookie by unstable network.
             session_commit();
             session_id($_SESSION['new_session_id']);
             }
            // potential custom logic for session id (ex. switching between hosts)
            $this->setSessionId($this->sidResolver->getSid($this));
            session_start();
            if (isset($_SESSION['destroyed'])) {
               if ($_SESSION['destroyed'] < time()-300) {
                   $this->destroy(['clear_storage' => true]);

               }
            }
            $this->validator->validate($this);

            register_shutdown_function([$this, 'writeClose']);

            $this->_addHost();
            \Magento\Framework\Profiler::stop('session_start');
        }
        $this->storage->init(isset($_SESSION) ? $_SESSION : []);
        return $this;
    }

Friday, December 14, 2018

Magento 2 Certified Professional Developer Practice Test


1. As you create a controller in Magento's adminhtml, you must configure it to respond appropriately to the ACL. Keeping simplicity in mind, what steps do you take to implement this?

A. 
Create a plugin to ensure that the ACL resource is accessible.
B. Check the authorization class' isAllowed method as the first check in the execute function.
C. 
Set the value of the ADMIN_RESOURCE constant to be the ACL resource.
D. 
Override _isAllowed and check the authorization class' isAllowed method for the ACL resource.

Correct Answer :  C
2. There is a product with a price of $25. The special price is $20 and is active through 2017. The tiered pricing has a discount for 2 or more products at $22.00. Additionally, a catalog price url applies a 10% off discount. What is the final price that is shown on the product page?
A. 22.50
B. 22.
C. 
20.
D. 
18.Correct Answer :  A


Setup Xdebug in your system

1.Find your PHP ini file. 2.Run command “php --ini” to find loaded ini file.
3.Uncomment xdebug module.
zend_extension="/Applications/MAMP/bin/php/php7.0.8/lib/php/extensions/no-debug-non-zts-20151012/xdebug.so"
  • 4.Set below settings for xdebug
xdebug.remote_enable=1
xdebug.remote_host=127.0.0.1
xdebug.remote_connect_back=1    # Not safe for production servers
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_autostart=true

5.Set-up debugger with PHPStorm

Open drop down and click on edit configuration as display in below screenshot.

Click on + icon and select PHP Web Page from the list.
Under Configuration section set Server details and for that click on
  • Step-out, Step-into, Continue, Terminate
Step-out: F8
Step-Into: F7
Continue: F9

  • Debugging step by step
  • Inspect variables
  • Watch variable
  • Conditional debugging point

Thursday, December 13, 2018

Install and Configure Redis Cache On Magento 2


  1. Compare to magento 1.x , magento 2 has come up with various cache configuration, one of these is redis.
  2. Redis is an open source high performance database, in-memory data structure store. Redis is a key-value store meaning is primarily design to store data using a unique Key.
  3. Redis is an optional backend cache solution to replace magento2's default Zend_Cache_Backend_File.
  4. Limitation of default Zend_Cache_Backend_File is core_cache_tag table constantly grows. If a Magento instance has multiple web sites and web stores with large catalogs.
  5. Advantages of Redis:
  1. Extreme fast in-memory storage
  2. Vertical and Horizontal Scaleable
  3. Shareable among Multi Servers
  4. Cache Tag Support
  5. Persistence
Requirements :

  • The following are the prerequisites to configure Redis Cache on Magento 2:
  • Redis Server
  • PHP Redis Extension
  • How to configure Magento 2 to use Redis as cache storage?
  • To configure Redis for Magento 2 cache, just add the following example to your app/etc/env.php:
'cache' =>

 array (

 'frontend' =>

  array (

   'default' =>

    array (

     'backend' => 'Cm_Cache_Backend_Redis',

      'backend_options' =>

      array (

       'server' => 'https://www.linkedin.com/redir/invalid-link-page?url=127%2e0%2e0%2e1',

       'port' => '6379',

       'persistent' => 0,

       'database' => '0',

       'force_standalone' => '0',

       'connect_retries' => '10',

       'read_timeout' => '30',

       'automatic_cleaning_factor' => '0',

       'compress_data' => '1',

       'compress_tags' => '1',

       'compress_threshold' => '20480',

       'compression_lib' => 'gzip',

     ),

   ),

  ),

),

  • Now flush your cache:
  • rm -rf /data/web/magento2/var/cache/*
  • redis-cli flushall
  • NOTE:
  • If redis is working you should not see any files and directories in the folder var/cache/ anymore.
  • Further you can connect with "redis-cli info" to check if there entries in the databases.
  • Enter the following command:
  • redis-cli ping
  • PONG should be the response.
  • If both commands succeeded, Redis is set up properly.

Thursday, September 21, 2017

speed up Magento using .htaccess file

Speed Magento with .htaccess file

1) Enable Gzip Compression For Magento

Gzip is a method of compressing files (reducing file’s size) for faster network transfers. In your htaccess file, find.  #php_flag zlib.output_compression on  and un-comment it to enable Gzip Compression

2) Compressing CSS and JavaScript files
Compressing CSS and JavaScript files is important to speed your Magento site, we can enable compression for CSS and Java files by adding the following lines to .htaccess file:

<IfModule mod_deflate.c>
  # Compress HTML, CSS, JavaScript, Text, XML and fonts
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE font/opentype
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE image/x-icon
</IfModule>

Enable Expires Headers
Expire header is used here to indicate how long browser should store files for caching. Generally, we use expire headers for images file only but for Magento, we should apply expire header for all elements like script, styles or flash.

############################################
## Add Expires header to Magento - by Magentoexplorer.com
## http://developer.yahoo.com/performance/rules.html#expires
    <FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
    ExpiresActive On
    ExpiresDefault "access plus 1 year"

Disable ETags
Entity tags (ETags) help web servers and browsers use to determine if the component in the browser’s cache matches the one on the origin serverETages allow browsers to validate cached page. We can disable eTags to improve performance:
FileETag none
Disable some unwanted modules