尝试在 Magento 中构建 AJAX 购物车,配置选项未显示
问题: 我正在尝试在 magento 中构建 AJAX 购物车,但我似乎无法获取可配置产品的所选配置选项。我相信正在调用正确的方法($this->getOptionList()
)来获取它们。它应该返回一个带有选项和标签的数组,但它什么也没返回!需要明确的是,它们出现在普通购物车中。
解释: 我是如何做到这一点的简短故事: 我使用 Mage_Checkout 模块的购物车控制器,但我将模板更改为非常小的内容(使用当前主题中的布局更新),以及其中定义了路由器的自定义模块。
长话短说:
我创建了一个模块 MyNameSpace_Checkout,在其中定义了一个路由器,将 frontName 'ajaxcart' 绑定到 Mage_Checkout 模块中的控制器。 现在,在我当前主题的 local.xml 文件中,我将以下布局更新放在“ajaxcart_cart_index”句柄下:
<ajaxcart_cart_index>
<reference name="root">
<action method="setTemplate">
<template>ajaxcart/cart/index.phtml</template>
</action>
<block type="checkout/cart" name="checkout.cart" template="ajaxcart/cart/show.phtml" as="cart"></block>
</reference>
</ajaxcart_cart_index>
我的模板(“ajaxcart/cart/show.phtml”)正在使用,因此效果很好。我在访问 http://domain.com/ajaxcart/cart
index.phtml:
<?php echo $this->getChildHtml('cart'); ?>
show 时测试了它.phtml:
<?php foreach($this->getItems() as $_item): ?>
<?php $_renderer = $this->getItemRenderer($_item->getProductType())->setItem($_item); ?>
<?php /* render an item */ ?>
<?php endforeach; ?>
在这个上下文中看到 $this
指的是 Mage_Checkout 模块的 Cart 块,并深入研究这个类的方法 getItemHtml()
(实际上是它的超类) ) 我发现购物车中每个商品的块对象是使用上面 show.phtml
示例中的第二行 ($_renderer
) 检索的。
有谁知道为什么信息丢失?我使用原始控制器的全部原因是它可能正在做一些重要的事情,但它仍然无法工作!
提前致谢。
PROBLEM:
I'm trying to build an AJAX cart in magento, but I can't seem to fetch the chosen configuration option(s) of configurable products. I believe am invoking the the right method ($this->getOptionList()
) to get them. It should return an array with options and labels, but it returns nothing!! To be clear, they are showing up in the normal cart.
EXPLANATION:
The short story of how I'm doing it:
I use the Cart Controller of the Mage_Checkout module, but I change the template to something very minimal (using layout updates in the current theme), and a custom module with a router defined in it.
Long story:
I have created a module MyNameSpace_Checkout in which I have defined a router that binds the frontName 'ajaxcart' to the controllers in the Mage_Checkout module.
Now in the local.xml file of my current theme I put the following layout updates under the 'ajaxcart_cart_index' handle:
<ajaxcart_cart_index>
<reference name="root">
<action method="setTemplate">
<template>ajaxcart/cart/index.phtml</template>
</action>
<block type="checkout/cart" name="checkout.cart" template="ajaxcart/cart/show.phtml" as="cart"></block>
</reference>
</ajaxcart_cart_index>
My template ('ajaxcart/cart/show.phtml') is being used, so this worked pretty well. I tested it when I went to http://domain.com/ajaxcart/cart
index.phtml:
<?php echo $this->getChildHtml('cart'); ?>
show.phtml:
<?php foreach($this->getItems() as $_item): ?>
<?php $_renderer = $this->getItemRenderer($_item->getProductType())->setItem($_item); ?>
<?php /* render an item */ ?>
<?php endforeach; ?>
Seeing as $this
in this context refers to the Cart Block of the Mage_Checkout module, and digging around in the method getItemHtml()
of this class (its superclass actually) I found that the block object per item in the cart is retrieved using the second line in the show.phtml
sample above ($_renderer
).
Does anyone know why information is missing? The whole reason I'm using the original controller is that it is probably doing some essential stuff, but it's still not working!!
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己发现了问题,布局更新(当前主题的 local.xml)是问题所在:
我再次查看了基本/默认主题的 checkout.xml,它显示了一些
addItemRender
(布局xml)checkout_cart_index
句柄下的cart/checkout
块中的方法。特别是对于我的问题,上面的元素丢失了,这弄乱了购物车中可配置产品项目的呈现。需要加载一种特殊类型的块对象,该对象实际上具有 (PHP) 方法getOptionList()
。I found the problem myself, the layout updates (local.xml of the current theme) were the problem:
I took another look at checkout.xml of the base/default theme and it sported some
addItemRender
(layout xml) methods in thecart/checkout
block under thecheckout_cart_index
handle. Specifically for my problem, the above element was missing and that messed up the rendering of a configurable product item in the cart. A special type of block object needs to be loaded that actually has the (PHP) methodgetOptionList()
.