0

I've tried researching this like crazy, and all I've come up with is PHP solutions.. which sadly, our Magento 2 site is managed by Magento Certified third-party, and I don't have access to play around with the themes or PHP files myself..

(and yes, I know that is technically the 'better way' of doing things.. but obviously some values are already accessible without doing so, and that's what I'm looking for.. just whatever is already accessible without making file changes.)

I'm simply trying to find a list of variables (? not really sure what to call these, specifically.) that I can pull with just HTML or Javascript, to use in Magento's built in CMS Blocks, dynamic pages, etc.; such as the following example..

I know that I can use something like this to pull the Customer's First Name (also 'fullname' works, but I don't know what else does.):

<div data-bind="scope: 'customer'" style="display: none;">
    <span data-bind="text: customer().firstname">
</div>

Is there a list somewhere of other attribute-names I can use to get other values? Also, what are some of the other scopes, and some of their values? (Company, etc.?)

Thanks in advance!

EDIT: Furthermore, could someone please demonstrate how to write the code I showed above but just in Javascript, so I'm not actually creating a hidden DOM element, just to get the value from? (Currently I'm using JS to find that element by ID, and storing it's contents into a variable of its own, which is obviously not very efficient at all.)

1 Answer 1

0

Magento has a sync mechanism between back-end and front-end. Whenever specific information has to be updated, Magento sends an Ajax request to fetch the required information, and store it in local storage for later use. If you check the local storage → mage-cache-storage, you will see different sections, like customer, company, cart, etc., and each section might have multiple subsections. Local Storage This is where Magento is getting private values for the frontend. So, if you just need something simple, just to get a value one-time, you can just use directly JS and get the local storage value. In this case, to get the customer's first name, you can do something similar (data in sections is in JSON format, so you have to parse it):

JSON.parse(window.localStorage.getItem('mage-cache-storage')).customer.firstname

Else, for example if you want to subscribe to specific changes, then you will have to create a component in your module, and use customer-data.js there. You can read more about this in this post, or just google around:

https://belvg.com/blog/customer-data-management-in-magento-2.html

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.