1

i want product id from local storage in observer is there any easy way to do it?

enter image description here

1

You can use below code in your controller

$product_id="<script>document.write(parseInt(Object.keys(JSON.parse(window.localStorage.getItem('product_data_storage')))))</script>";
echo 'product id'.$product_id;
2
  • it work fine but maybe its not proper method
    – Hiren
    Aug 16 '18 at 9:25
  • yes its not good way to add like that in controller you can use ajax from phtml to get value in controller. by the way can you up vote my answer ? Aug 16 '18 at 9:27
0

local storage it a client-side element and controller is a server-side element. So you have read this variable using js and send to via an ajax request.

'Magento_Customer/js/customer-data'

Can able to provide local storage data

customerData.get('product_data_storage');
4
  • can you provide me any reference for that ??
    – Hiren
    Aug 17 '18 at 10:27
  • no, Just have an idea.
    – Amit Bera
    Aug 17 '18 at 10:42
  • how can i add custom tab in local storage not in under "mage-cache-storage" ?
    – Hiren
    Aug 21 '18 at 5:45
  • this return all cached product mean all those product on which customer visited, but how get current product?
    – HaFiz Umer
    Feb 25 '20 at 5:23

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.