1

I have a .phtml file that loops through dropdowns. There are 6 dropdowns belonging to two different groups (so 3 dropdowns per group).

I set a session on button click:

if ($this->getRequest()->getParam(self::USERBIKE)) {
    $block = $this->layoutFactory->create()->createBlock('Vendor\Module\Block\Garage');
    if ($block->getGarageCollection()->getFirstItem()->getCustomerId()) {
        $block->getCoreSession()->setUserBike(1);
    }
}

Which I then use to check in the dropdown .phtml file:

if ($session->getUserBike() == 1) {
    $useUserBike = true;
    # etc.
}

However, nothing seems to happen.

I added some logging to check each dropdown to see what was going on:

$writer = new \Zend\Log\Writer\Stream(BP. '/var/log/logfile.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);

# log each dropdown ID
$logger->info('OUTSIDE: '. $block->getDropdown()->getDropdownId());

# log Session value per dropdown
$logger->info('OUTSIDE USER BIKE SESH: '. $session->getUserBike());

if ($session->getUserBike() == 1) {
    $useUserBike = true;
    $userBike = $blockGarage->getGarageCollection()->getFirstItem();

    # log cur dropdown ID
    $logger->info('CURDROPDOWNID: '. $block->getDropdown()->getDropdownId());

    # log cur dropdown group ID
    $logger->info('CURFINDERID: '. $block->getDropdown()->getFinderId());

    if ($block->getDropdown()->getFinderId() == 1) {
        switch($block->getDropdown()->getDropdownId())
        {
            case 1: $compareTo = $userBike->getMakeId(); break;
            case 2: $compareTo = $userBike->getModelId(); break;
            case 3: $compareTo = $userBike->getYearId(); break;
            default: $compareTo = 0; break;
        }
    }

    # log compareTo value
    $logger->info(isset($compareTo) ? $compareTo : 'empty str');
    $logger->info('======================================'); # visibility break
}

The output of my logfile:

2019-01-09T16:33:47+00:00 INFO (6): OUTSIDE: 1
2019-01-09T16:33:47+00:00 INFO (6): OUTSIDE USER BIKE SESH:
2019-01-09T16:33:47+00:00 INFO (6): OUTSIDE: 2
2019-01-09T16:33:47+00:00 INFO (6): OUTSIDE USER BIKE SESH:
2019-01-09T16:33:47+00:00 INFO (6): OUTSIDE: 3
2019-01-09T16:33:47+00:00 INFO (6): OUTSIDE USER BIKE SESH:
2019-01-09T16:33:47+00:00 INFO (6): OUTSIDE: 4
2019-01-09T16:33:47+00:00 INFO (6): OUTSIDE USER BIKE SESH: 1
2019-01-09T16:33:47+00:00 INFO (6): CURDROPDOWNID: 4
2019-01-09T16:33:47+00:00 INFO (6): CURFINDERID: 2
2019-01-09T16:33:47+00:00 INFO (6): empty str
2019-01-09T16:33:47+00:00 INFO (6): ======================================
2019-01-09T16:33:47+00:00 INFO (6): OUTSIDE: 5
2019-01-09T16:33:47+00:00 INFO (6): OUTSIDE USER BIKE SESH: 1
2019-01-09T16:33:47+00:00 INFO (6): CURDROPDOWNID: 5
2019-01-09T16:33:47+00:00 INFO (6): CURFINDERID: 2
2019-01-09T16:33:47+00:00 INFO (6): empty str
2019-01-09T16:33:47+00:00 INFO (6): ======================================
2019-01-09T16:33:47+00:00 INFO (6): OUTSIDE: 6
2019-01-09T16:33:47+00:00 INFO (6): OUTSIDE USER BIKE SESH: 1
2019-01-09T16:33:47+00:00 INFO (6): CURDROPDOWNID: 6
2019-01-09T16:33:47+00:00 INFO (6): CURFINDERID: 2
2019-01-09T16:33:47+00:00 INFO (6): empty str
2019-01-09T16:33:47+00:00 INFO (6): ======================================

As you can see, The session doesn't have a value 'til the 4th dropdown. Why is this happening? What am I doing wrong?

Edit:

Minified Block Controller Session Code:

class Garage extends Tempalte
{
    protected $customerSession;

    public function __construct(
        \Magento\Customer\Model\Session $customerSession
    )
    {
        $this->customerSession = $customerSession;
    }

    public function getCustomerSession()
    {
        return $this->customerSession;
    }
}
  • Post your block coreSession function code – Prashant Valanda Jan 9 at 17:27
1

Change your block code as below.

class Garage extends Tempalte
{
    protected $customerSessionFactory;

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

    public function getCustomerSession()
    {
        return $this->customerSessionFactory->create();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.