I have code for test a customer session....

$sessionOrderBy = $this->_customerSession->setSortOrderBy('2');
echo $this->_customerSession->getSortOrderBy();

return is: 2 value

after commented

 // $sessionOrderBy = $this->_customerSession->setSortOrderBy('2');
and run 

return : null;

So, how do save $sessionOrderBy in _customerSession?

  • are you tried saved in you controller file?
    – Abdul
    Dec 9, 2015 at 10:04
  • this code in Myblock. it extends product_view. so how save it? @Abdul
    – xanka
    Dec 9, 2015 at 10:10

2 Answers 2


Bellow code add you block file

use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
use Magento\Customer\Model\Session as CustomerSession;

    protected $customerSession;

    protected $customerFactory;

    protected $customerRepository;

    public function __construct(
        CustomerSession $customerSession,
        CustomerInterfaceFactory $customerFactory,
        CustomerRepositoryInterface $customerRepository,
    ) {
        $this->customerSession = $customerSession;
        $this->customerFactory = $customerFactory;
        $this->customerRepository = $customerRepository;

    public function setSortOrderBy($sortOrder)
        $sortOrder = 2;
  • thanks @Abdul. i tried it. when i run $this->customerSession->getSortOrderBy() return Null;
    – xanka
    Dec 9, 2015 at 10:21

When i add

$sessionOrderBy = $this->_customerSession->setSortOrderBy('2');

it work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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