我想添加一个购物车块cms页面,但每当我尝试,什么都没有发生…连错误都没有
我试着遵循这个教程,http://www.magento.cc/how-to-use-php-on-a-cms-page.html.
所以我在app/code/local中创建了新文件夹,然后创建了一个Test.php文件,然而当我试图包含
时 {{block type="YourModule_Custom/test" my_param1="value 1" another_param="value 2"}}
在CMS页面中,没有任何显示
下面是我在Test.php页面中的代码:<?php
class YourModule_Custom_Block_Test extends Mage_Core_Block_Abstract
{
protected function _toHtml()
{
echo 'TEST';
$this->getChildHtml('header');
return $html;
}
}
可以进行local.xml更新。并将内容放置为. php文件
<cms_index_index>
<reference name="content">
<block type="Your_custom/Block" name="home_main" as="home_main" template="cms/default/home.phtml">
</block>
</reference>
</cms_index_index>
创建一个名为home的文件。然后你可以使用你自己的块类型来使用你的自定义模块/函数
我不知道你想达到什么目的。但是从你的问题中,我有一种强烈的感觉,你是想通过CMS页面设置一个你自己的模板。如果是这样的话,让我们分析一下为什么你的代码块没有显示任何输出。
你的块定义是这样的
{{block type="YourModule_Custom/test" my_param1="value 1" another_param="value 2"}}
这个定义没有问题。但如果你把name
加到你的块里,它就很好了。如果你设置了一个template
,那么你不需要任何后端代码来为你的块设置模板。你的block应该是这样的
{{block type="YourModule_Custom/test" name="test.block" template="test.phtml"}}
现在当magento遇到这个,它会找到你的块,设置描述的名称为该块(为了以后参考,这个name
将被使用)。然而,在这种情况下是不相关的),设置模板指定到您的块,然后呈现该模板中的内容。
所以你应该有一个指定类型的块。你现在有它(你不需要_toHtml()
在里面)。此外,你需要一个模板文件test.phtml
,它应该在位置app/design/frontend/<your_package>/<your_theme>/template/test.phtml
。你现在没有那份文件。因此创建它并将内容添加到文件
<p><?php echo "I am here. Can you see me ?"; ?></p>
现在检查CMS页面输出。你可以看到内容。不是吗?
所以现在你要做的是,而不是设置一个模板与那个块定义,你试图设置它通过你的块。这有错吗?很明显,没有。在某些情况下,我们需要这样做。我猜你真的需要它。因此,让我们再次以这种形式重新定义我们的块。
{{block type="YourModule_Custom/test" name="test.block"}}
嗯。这里我们没有为这个block设置模板。因此,您可以通过块定义设置它。您使用了_toHtml()
方法
<?php
class YourModule_Custom_Block_Test extends Mage_Core_Block_Abstract
{
protected function _toHtml()
{
echo 'TEST';
$this->getChildHtml('header');
return $html;
}
}
?>
这个方法用来设置模板,然后呈现内容。所以你在正确的轨道上。但这里的问题是,你没有设置任何模板!!加上你的方法返回一个没有任何内容的变量$html
。那么我们应该通过_toHtml()
返回什么呢?答案就在Mage_Core_Block_Template
。让我们看看_toHtml()
定义
protected function _toHtml()
{
if (!$this->getTemplate()) {
return '';
}
$html = $this->renderView();
return $html;
}
基本上,它的作用是检查模板是否被设置,如果没有返回什么。如果是,就会渲染。这意味着,很明显我们需要设置一个模板。所以你的block应该是这样的
<?php
class YourModule_Custom_Block_Test extends Mage_Core_Block_Template
{
protected function _toHtml()
{
$this->setTemplate('test.phtml');
$html = parent::_toHtml();
return $html;
}
}
请注意,您的块从Mage_Core_Block_Template
扩展而不是从Mage_Core_Bock_Abstract
。这是因为setTemplate()
方法定义在Mage_Core_Block_Template
类中。在_toHtml()
中,我们正在设置模板,然后将rest留给父块。现在检查test.phtml
中的内容是否显示在您的CMS页面中。对吧?
如果我的理解是正确的,那么这里有一个解决方案。
您可以从admin将此代码插入到cms页面。
Admin -> CMS -> Pages ->选择您想要显示块的任何页面->单击左侧导航中的设计选项卡->在页面布局部分下插入下面的代码"布局更新XML"字段。点击保存
<reference name="content">
<block type="checkout/cart" name="checkout.cart">
<action method="setCartTemplate"><value>checkout/cart.phtml</value></action>
<action method="setEmptyTemplate"><value>checkout/cart/noItems.phtml</value></action>
<action method="chooseTemplate"/>
<action method="addItemRender"><type>simple</type><block>checkout/cart_item_renderer</block><template>checkout/cart/item/default.phtml</template></action>
<action method="addItemRender"><type>grouped</type><block>checkout/cart_item_renderer_grouped</block><template>checkout/cart/item/default.phtml</template></action>
<action method="addItemRender"><type>configurable</type><block>checkout/cart_item_renderer_configurable</block><template>checkout/cart/item/default.phtml</template></action>
<block type="core/text_list" name="checkout.cart.top_methods" as="top_methods" translate="label">
<label>Payment Methods Before Checkout Button</label>
<block type="checkout/onepage_link" name="checkout.cart.methods.onepage" template="checkout/onepage/link.phtml"/>
</block>
<block type="page/html_wrapper" name="checkout.cart.form.before" as="form_before" translate="label">
<label>Shopping Cart Form Before</label>
</block>
<block type="core/text_list" name="checkout.cart.methods" as="methods" translate="label">
<label>Payment Methods After Checkout Button</label>
<block type="checkout/onepage_link" name="checkout.cart.methods.onepage" template="checkout/onepage/link.phtml"/>
<block type="checkout/multishipping_link" name="checkout.cart.methods.multishipping" template="checkout/multishipping/link.phtml"/>
</block>
<block type="checkout/cart_coupon" name="checkout.cart.coupon" as="coupon" template="checkout/cart/coupon.phtml"/>
<block type="checkout/cart_shipping" name="checkout.cart.shipping" as="shipping" template="checkout/cart/shipping.phtml"/>
<block type="checkout/cart_crosssell" name="checkout.cart.crosssell" as="crosssell" template="checkout/cart/crosssell.phtml"/>
<block type="checkout/cart_totals" name="checkout.cart.totals" as="totals" template="checkout/cart/totals.phtml"/>
</block>
</reference>
之后,您将在CMS页面上看到购物车块。如果你不需要它们中的任何块,你可以从上面的代码中删除该块。
如果您尝试从CMS页面调用添加到购物车,则添加url
checkout/cart/add?product=$id&qty=$qty
,
<a href="{{store url='checkout/cart/add?product=5&qty=3'}}">
<img src="{{skin url='images/addtocart.jpg'
}}" alt="product5" /></a>