在magento中获取产品或产品ID
当编写一组新的 .phtml & 时块:
推荐的方式来了解该块当前是否位于产品页面上?
如果它位于产品页面上,那么我如何获取当前正在查看的产品的数据?
谢谢, 埃亚勒
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
第一个问题)如何知道您正在查看产品页面。
您的问题不清楚,因为您没有说出此类功能的目的。我将您的问题解释为“我的块如何知道它嵌入在产品视图页面而不是其他页面中?”。
最常见的方法是获取页面布局并搜索用于查看产品的标准块。因此,在您的块中,您可以提出以下请求:
另一种方法是检查用于形成当前页面布局的布局句柄:
但是它是更具体的方法,仅在查看产品页面而不是使用类似设计的页面时才返回积极结果产品视图块。例如,产品评论页面将向您显示相同的产品页面,但有细微差别,但它使用自己的(不是“catalog_product_view”)句柄。至于你没有提到你的目的,我不能建议你选择第一种或第二种方法。
注意:你的架构可能是错误的,因为通常
a)您的块不需要知道它嵌入的位置,因此不需要检查当前查看的页面
b) 或者您的块仅用于产品查看页面。但在本例中,这是一个布局 xml 任务,仅将块放入所需的页面和块中。
第二个问题)如何在产品视图页面上获取当前产品。
使用以下代码:
您将获得当前查看的产品。
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.
您可以检查当前请求对象 - 它保存有关当前正在使用的模块、控制器、操作和参数的所有数据。之后,您可以将其与产品页面进行比较:模块、控制器和操作是否相同以及产品 ID 是否匹配
但是您应该意识到第三方自定义可以用他们的产品替换标准 Magento 产品视图控制器
You can check current request object - it holds all data about module, controller, action and params which is currently in use. After that you could compare it with product page ones: does the module, controller and action the same and does product id matches
But you should aware of third-party customizations which could replace standard Magento product view controller with theirs ones
这会让您知道正在查看什么产品。
如果存在
current_product
,那么您可能位于产品页面上,尽管这并不能保证该页面由目录控制器处理。This will let you know what product is being viewed.
If there is a
current_product
then you are probably on a product page, although this doesn't guarantee the page is being handled by a catalog controller.