skip to Main Content

I need to get the Customer ID primary in Home Page, but not have a Session Interface to give this information.

I tried theses:

MagentoBackendModelSession
MagentoCatalogModelSession
MagentoCheckoutModelSession
MagentoCustomerModelSession
MagentoNewsletterModelSession

Exists one interface to give Customer ID on every page?

2

Answers


  1. There are methods available in magento2 to get customer id, one of the method is using Object Manager as below code.

    $objectManager = MagentoFrameworkAppObjectManager::getInstance();  
    $customerSession = $objectManager->get('MagentoCustomerModelSession');  
    $customerId = $customerSession->getCustomer()->getId(); // get the customer Id
    
    Login or Signup to reply.
  2. namespace VendorModuleBlock;

    class Form extends MagentoFrameworkViewElementTemplate

        protected $customerSession;
    
        /**
         * Construct
         *
         * @param MagentoFrameworkViewElementTemplateContext $context
         * @param MagentoCustomerModelSession $customerSession
         * @param array $data
         */
        public function __construct(
            MagentoFrameworkViewElementTemplateContext $context,
            MagentoCustomerModelSession $customerSession,
            array $data = []
        ) {
            parent::__construct($context, $data);
    
            $this->customerSession = $customerSession;
        }
    
        public function _prepareLayout()
        {
    
            var_dump($this->customerSession->getCustomer()->getId());
            exit();
            return parent::_prepareLayout();
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search