Tag: magento2

How to get customer id from block when full page cache enable in magento 2

Today I discuss about getting customer_id from block when full page cache(FPC) is enable. If FPC is enable then you can’t get customer id from block just because magento2 reset all customer session. You can get customer session data if your layout set cacheable=”false” in that case magento2 just ignore cache whole page. How? lets dive.

If you open vendor/magento/module-customer/Model/Layout/DepersonalizePlugin.php


/**
 * After generate Xml
 *
 * @param \Magento\Framework\View\LayoutInterface $subject
 * @param \Magento\Framework\View\LayoutInterface $result
 * @return \Magento\Framework\View\LayoutInterface
 */
public function afterGenerateXml(\Magento\Framework\View\LayoutInterface $subject, $result)
{
    if ($this->depersonalizeChecker->checkIfDepersonalize($subject)) {
        $this->visitor->setSkipRequestLogging(true);
        $this->visitor->unsetData();
        $this->session->clearStorage();
        $this->customerSession->clearStorage();
        $this->session->setData(\Magento\Framework\Data\Form\FormKey::FORM_KEY, $this->formKey);
        $this->customerSession->setCustomerGroupId($this->customerGroupId);
        $this->customerSession->setCustomer($this->customerFactory->create()->setGroupId($this->customerGroupId));
    }
    return $result;
}

Now check the if condition, basically this return true/false from following function
vendor/magento/module-page-cache/Model/DepersonalizeChecker.php


/**
 * Check if depersonalize or not
 *
 * @param \Magento\Framework\View\LayoutInterface $subject
 * @return bool
 * @api
 */
public function checkIfDepersonalize(\Magento\Framework\View\LayoutInterface $subject)
{
    return ($this->moduleManager->isEnabled('Magento_PageCache')
        && $this->cacheConfig->isEnabled()
        && !$this->request->isAjax()
        && ($this->request->isGet() || $this->request->isHead())
        && $subject->isCacheable());
}

Here $subject->isCacheable() return false if your layout has ‘cacheable=”false”‘ attribute, lets check
vendor/magento/framework/View/Layout.php


/**
 * Check is exists non-cacheable layout elements
 *
 * @return bool
 */
public function isCacheable()
{
    $this->build();
    $cacheableXml = !(bool)count($this->getXml()->xpath('//' . Element::TYPE_BLOCK . '[@cacheable="false"]'));
    return $this->cacheable && $cacheableXml;
}

So if full page cahce enable and cacheable=”false” is not exist then magento 2 reset customer session by


$this->session->clearStorage();
$this->customerSession->clearStorage();

That’s why if you try to get customer session data from block, it’s return always empty basically only group_id is present in customer session data.

So how to get customer information then.

You can follow Magento2 official tutorial.

Now Today I discuss how you can get customer_id if cache enable. Lets dive.

Suppose vendor name ‘SR‘ and module name ‘CustomerSession

Create a contex class [SR/CustomerSession/Model/Customer/Context.php]


namespace SR\CustomerSession\Model\Customer;

class Context
{
    /**
     * Customer authorization cache context
     */
    const CONTEXT_CUSTOMER_ID = 'logged_in_customer_id';
}

Now pluginize Magento\Framework\App\Action\AbstractAction dispatch method.
So create a di.xml [SR/CustomerSession/etc/frontend/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">
    <type name="Magento\Framework\App\Action\AbstractAction">
        <plugin name="sr-customer-app-action-dispatchController-context-plugin" type="SR\CustomerSession\Plugin\App\Action\Context" sortOrder="15"/>
    </type>
</config>

Now create SR/CustomerSession/Plugin/App/Action/Context.php


namespace SR\CustomerSession\Plugin\App\Action;

use SR\CustomerSession\Model\Customer\Context as CustomerSessionContext;

class Context
{
    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $customerSession;

    /**
     * @var \Magento\Framework\App\Http\Context
     */
    protected $httpContext;

    /**
     * @param \Magento\Customer\Model\Session $customerSession
     * @param \Magento\Framework\App\Http\Context $httpContext
     */
    public function __construct(
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\App\Http\Context $httpContext
    ) {
        $this->customerSession = $customerSession;
        $this->httpContext = $httpContext;
    }

    /**
     * @param \Magento\Framework\App\ActionInterface $subject
     * @param callable $proceed
     * @param \Magento\Framework\App\RequestInterface $request
     * @return mixed
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function aroundDispatch(
        \Magento\Framework\App\ActionInterface $subject,
        \Closure $proceed,
        \Magento\Framework\App\RequestInterface $request
    ) {
        $customerId = $this->customerSession->getCustomerId();
        if(!$customerId) {
            $customerId = 0;
        }

        $this->httpContext->setValue(
            CustomerSessionContext::CONTEXT_CUSTOMER_ID,
            $customerId,
            false
        );

        return $proceed($request);
    }
}

That’s it. Now you can able to get customer id following way even cache enable:


/**
* @var \Magento\Framework\App\Http\Context
*/
$this->httpContext->getValue(SR\CustomerSession\Model\Customer\Context::CONTEXT_CUSTOMER_ID);

Download full module from here

How to create a custom form in Magento2 admin

Magento2 have excellent feature which allow a developer to create the admin forms without coding any HTML. It is easy to create any form with any data. This article will guide you how to create such form.

If You miss my previous tutorial, take a overview from here. This tutorial is fully dependent on previous one. So read carefully.

Get a copy of this module? Download from here

Happy Coding!

Creating magento2 adminhtml grid

Magento2 provides an infrastructure, Adminhtml Grid, for you to do the admin backend development in an elegant way. In this article, I’ll introduce you the mechanism of the Adminhtml Grid and you’ll know how to manage the data through it easily and efficiently.

If you miss my previous magento2 related article, take a look from here.

Suppose Namespace : SR and Module : Weblog

Get a copy of this module? Download from here

Creating a Custom Module in Magento 2

In magento2 is fully new structure, truly fast, more structured,use jQuery as a default javascript library. Today I create a new module for magento2.

In Magento 1 we had module declaration files. These lived in

app/etc/modules/*.xml

But in magento2 there is no “app/etc/modules/” for module declaration. In magento2 a new module configuration file, located in

app/code/Packagename/Modulename/etc/module.xml

In magento2, there is no codepool any more. So you can forgot about codepool concept. lets start to create our first module called “Weblog” and namesapce called “SR”.

Step 1. Create a module configuration file, location is

app/code/SR/Weblog/etc/module.xml

 

</pre>
<pre><?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="SR_Weblog" setup_version="2.0.0">
    </module>
</config>

Step 2. Create a composer.json file inside module in following location
app/code/SR/Weblog/composer.json

{
    "name": "sr/module-weblog",
    "description": "N/A",
    "require": {
        "php": "~5.5.0|~5.6.0|~7.0.0",
        "magento/module-store": "100.0.0",
        "magento/module-backend": "100.0.0",
        "magento/module-media-storage": "100.0.0",
        "magento/framework": "100.0.0",
        "lib-libxml": "*"
    },
    "type": "magento2-module",
    "version": "100.0.0",
    "license": [
        "OSL-3.0",
        "AFL-3.0"
    ],
    "autoload": {
        "files": [ "registration.php" ],
        "psr-4": {
            "SR\\Weblog\\": ""
        }
    }
}

Step 3. Create registration.php following location SR/Weblog/registration.php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'SR_Weblog',
    __DIR__
);

Step 4. Register module. app/etc/config.php can be updated manually by adding the extension to the array of modules with a value of 1 indicating that it is enabled. The following shows adding the new module to the end of the file.

 modules' => array (
  'Magento_Core' => 1,
    ...    
  'Magento_Wishlist' => 1,
  'SR_Weblog' => 1
),

If we had pre-installed Magento 2 instance, then we need to run setup update:

php bin/magento setup:upgrade

Clear cache and go to admin->Sores->Configuration->advanced.Here list your new module and look like
SR_Weblog

Perfect, Our first Weblog module creation is done!

Now I create a controller for displaying something 🙂

Let’s start…

In magento 1, router config is declare inside config.xml file, but in magento2 need to new file which is routes.xml.
So create a new file routes.xml, location is

app/code/SR/Weblog/etc/frontend/routes.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="weblog" frontName="weblog">
            <module name="SR_Weblog" />
        </route>
    </router>
</config>

Configuration is done!

So, now create a new controller file. In magento 1 all controller are locate controllers folder, but magento2 , here no controllers folder. All controller are locate “Controller” folder.

So, Create our index controller inside “Controller” folder, location is

app/code/SR/Weblog/Controller/Index/Index.php

namespace SR\Weblog\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    public function execute()
    {
        die('Hello World!');
    }
}

Clear cache, and run, (url should be: http://something/weblog)

That’s it!