when writing a new set of .phtml & block:
What is the recommanded way to know if the block is currently on a product page ?
If it is on a product page then how do I get to the data of the currently being viewed product ?
Thanks,
Eyal
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
1st question) How to know that you’re on product view page.
Your problem is not clear because you don’t say the purpose of such functionality. I interpret your question as “How can my blockknow that it’s embedded in Product View page and not in some other page?”.
The most common way is to get layout of page and search it for standard block designed to view product. So in your block you can make following request:
The other method is to check layout handles used to form current page layout:
However it’s more concrete method, it returns positive result only when viewing product page and not pages with similar designs using product view blocks. For, example Product Review page will show you same product page with minor differences but it uses its own (not ‘catalog_product_view’) handle for it. As far as you said nothing about your purpose I cannot recommend you to choose first or second method.
Notice: you architecture can be wrong, because usually
a) your block either doesn’t need to know where it’s embedded, so it doesn’t need not check currently viewed page
b) or your block is used only for product view page. But in this case it’s a layout xml task to put your block only in required pages and blocks.
2nd question) How to get current product on product view page.
Use following code:
And you’ll get the product currently viewed.