cancel
Showing results for 
Search instead for 
Did you mean: 

get Magento 2.1 customer session data outside of Magento

SOLVED

get Magento 2.1 customer session data outside of Magento

I have a Magento 2.1 based website and I want to run a query after a customer clicks a button in there customer dashboard that would run an external php file. My issue is I can not figure out in Magento 2.1 how to get magento and the customers session data into the external php file?

Here is the code I am trying to use

 

require('app/bootstrap.php');
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
/** @var \Magento\Framework\App\Http $app */
//$app = $bootstrap->createApplication('DemoApplication');
//$bootstrap->run($app);

//mysql connection
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();


$customerSession = $om->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {
    $thecustomeridis = $customerSession->getCustomer()->getId(); 
    //echo   $customerSession->getCustomer()->getName()."<br/>";  // get  Full Name
    //echo   $customerSession->getCustomer()->getEmail()."<br/>"; // get Email Name
    $origional_groupid = $customerSession->getCustomer()->getGroupId();  // get Customer Group Id

    //lets run sql to update the customer to be a vip 
    $sql = "UPDATE customer_entity SET group_id = 3 WHERE entity_id = $thecustomeridis AND store_id= 1 AND website_id = 1";
   $result = $connection->fetchAll($sql); 
  }  
1 ACCEPTED SOLUTION

Accepted Solutions

Re: get Magento 2.1 customer session data outside of Magento

Hi Jayreis, sorry but this is the wrong way to go about this solution as it goes against Magento best practices. You should instead be creating a real Magento extension. This will probably need to create a new controller that your button can link to. This controller will then handle communication with the relevant customer models to update their VIP status.  It's unwise to use direct database queries, and is a security risk to pass session data straight into the SQL query as well. 

 

Check out this introduction to building a Magento 2 extension: http://inchoo.net/magento-2/how-to-create-a-basic-module-in-magento-2/

----
If you've found one of my answers useful, please give "Kudos" or "Accept as Solution" as appropriate. Thanks!

View solution in original post

4 REPLIES 4

Re: get Magento 2.1 customer session data outside of Magento

Hi Jayreis, sorry but this is the wrong way to go about this solution as it goes against Magento best practices. You should instead be creating a real Magento extension. This will probably need to create a new controller that your button can link to. This controller will then handle communication with the relevant customer models to update their VIP status.  It's unwise to use direct database queries, and is a security risk to pass session data straight into the SQL query as well. 

 

Check out this introduction to building a Magento 2 extension: http://inchoo.net/magento-2/how-to-create-a-basic-module-in-magento-2/

----
If you've found one of my answers useful, please give "Kudos" or "Accept as Solution" as appropriate. Thanks!

Re: get Magento 2.1 customer session data outside of Magento

That answer isn't helpful at all.  CMS's are built for everyone but seldom work for anyone.  Best practices?  Best practice would be to let developers have easy access to normal DBA things like stored procedures or joins.  Not everyones uses a cms for simple crud.

Re: get Magento 2.1 customer session data outside of Magento

@Tom Robertshaw 

That is a monumentally unhelpful answer.

 

As a Magento developer of many years I cannot express how incredibly frustrating it is to come across responses like yours when investigating issues.

 

So-called "best practices" do not apply to all use cases. Magento (and its developer community) need to grow up and realise that is the reality of development. It is wholly unnecessary - not to mention massively impractical - to create a module for every single programmatic task you want to do with Magento.

 

Not all Magento developers develop for external clients, customers, or third party usage. Plenty of us develop for internal use only, for exclusive use by a single website, or even one-off "quick scripts" and all of this is totally fine.

 

The mantra of "never use ObjectManager" is espoused and parroted by almost every "knowledgeable" respondent of Magento but is, in my opinion, ultimately, damaging to the platform because it limits the flexibility of Magento and makes it incredibly difficult to develop in an agile and fast-paced way.

 

Anyway, @jayreis, here is the answer to your question. I hope it helps you and anyone else who comes here looking for assistance to real-world problems:

 

The following script uses ObjectManager to fetch the browser's frontend session and get the items in your cart. It should be run from [magento_root_folder]/example.php. To run it from another folder you should adjust the bootstrap.php location appropriately.

 

<?php

require("app/bootstrap.php");
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();

// Set the state (not sure if this is neccessary)
$state = $obj->get('Magento\Framework\App\State')->setAreaCode('frontend');

// Get cart items
$quote      = $obj->get('Magento\Checkout\Model\Session')->getQuote();
$quoteItems = $quote->getAllVisibleItems();

echo "Found " . count($quoteItems) . " cart items";

If you have problems running this, make sure your cookie and session settings are correctly set in Magento (see https://magento.stackexchange.com/questions/275900/get-the-cart-contents-from-an-external-script-usi...).

 

Also, a reminder that session issues can also be related to browser caching/cookies, Redis/Memcached (if used), and so on.

Re: get Magento 2.1 customer session data outside of Magento

@good_stuffTotally agreed about ObjectManager etc.

 

See this Stackoverflow post for details about obtaining session data in an external script: https://magento.stackexchange.com/questions/275900/get-the-cart-contents-from-an-external-script-usi...

 

That post is specifically about the Checkout session object, but you should be able to replace "Checkout" with "Customer" and access the customer session data in much the same way.

 

P.S. I wrote a longer rant about not hating on ObjectManager etc. which was removed by the mods!