Magento – How to get customer session id in magento 2

blocksmagento2modulesession

I want to get the session-trail in a custom module. Without the session-related code, it is working fine, but it breaks the module when I add it

namespace Scaledesk\Trails\Block;

use \Magento\Framework\View\Element\Template;
use \Magento\Framework\View\Element\Template\Context;
use \Scaledesk\Trails\Model\ResourceModel\Post\Collection as PostCollection;
use \Scaledesk\Trails\Model\ResourceModel\Post\CollectionFactory as PostCollectionFactory;
use \Scaledesk\Trails\Model\Post;

class Users extends Template
{
    /**
     * CollectionFactory
     * @var null|CollectionFactory
     */
    protected $_postCollectionFactory = null;


    protected $customerSession;

    /**
     * Constructor
     *
     * @param Context $context
     * @param PostCollectionFactory $postCollectionFactory
     * @param \Magento\Customer\Model\Session $customerSession
     * @param array $data
     */
    public function __construct(
        Context $context,
        PostCollectionFactory $postCollectionFactory,
        \Magento\Customer\Model\Session $customerSession,
        array $data = []
    ) {
        $this->customerSession = $customerSession;
        $this->_postCollectionFactory = $postCollectionFactory;
        parent::__construct($context, $data);
    }

    /**
     * @return Post[]
     */
    public function getPost()
    {
        /** @var PostCollection $postCollection */

        $customerId=$this->customerSession->getCustomer()->getId();


        $sort=1;
        $postCollection = $this->_postCollectionFactory->create();
        $postCollection->addFieldToSelect('*');
        $postCollection->addFieldToFilter('main_table.entity_id',$customerId);
        $postCollection->getSelect()->join( array('customer_entity'=> $postCollection->getTable('customer_entity')), 'customer_entity.entity_id = main_table.entity_id');
        $postCollection->setOrder('trails_id', ($sort==1 ? 'asc':'desc'));
        $postCollection->load();
//        var_dump($postCollection->getSelect()->__toString());
//        die();
//        var_dump($postCollection->getItems()); die();
        return $postCollection->getItems();



    }

    /**
     * For a given post, returns its url
     * @param Post $post
     * @return string
     */
    public function getPostUrl(
        Post $post
    ) {
        return $this->getUrl().'trails/post/view/id/'. $post->getId();
    }




}

$postCollection->addFieldToFilter('main_table.entity_id',$customerId);  

how to get session customerSession->getCustomer()->getId()

Best Answer

You need to inject \Magento\Customer\Model\Session class for set and get data in customer session

Using Factory Method

protected $customerSession;

public function _construct(
    ...
    \Magento\Customer\Model\SessionFactory $customerSession
    ...
) {
    ...
    $this->customerSession = $customerSession;
    ...
}   

public function setValue()
{
    return $this->customerSession->setId('YourValue'); //set value in customer session
}

public function getValue()
{
    return $this->customerSession->getId(); //Get value from customer session
}

Using ObjectManager(Need to Inject ObjectManager in constructor):

$customerSession = $this->objectManager->create("Magento\Customer\Model\Session");

if($customerSession->isLoggedIn()){
  echo $customerSession->getCustomerId();
}