当用户点击添加到购物车时,我需要在顶部栏中显示minicart。我在这个位置创建了一个名为topcart.phtml
的文件
app/design/frontend/default/mytheme/template/checkout/cart/mycart.phtml.
现在我在我的控制器文件中调用此块并将响应返回到show mini cart。
$sidebar_block = $this->getLayout()->getBlock('mycart');
$sidebar = $sidebar_block->toHtml();
$response['sidebar'] = $sidebar;
但它显示此错误
“致命错误:在非对象”中调用成员函数toHtml() 我的控制器文件。
我是否还需要在布局或任何其他位置调用mycart.phtml文件?
mycart.phtml
<?php $_cartQty = $this->getSummaryCount() ?>
<div class="shoppingcart">
<div class="fadelink">
<div class="shopping_cart_mini hidden-phone hidden-tablet <?php if(!$_cartQty) echo 'empty'; ?>">
<div class="close1">X</div>
<div class="inner-wrapper">
<?php $_items = $this->getRecentItems() ?>
<?php print_r($_items); ?>
<?php if(count($_items)): ?>
<?php echo $this->__('Recently added item(s)') ?>
<?php foreach($_items as $_item): ?>
<?php echo $this->getItemHtml($_item) ?>
<?php endforeach; ?>
<div class="wrapper"> <a href="<?php echo Mage::helper('checkout/cart')->getCartUrl(); ?>" class="button"><?php echo $this->__('View shopping cart') ?></a> <a href="<?php echo $this->getCheckoutUrl() ?>" class="button"><?php echo $this->__('Proceed to Checkout') ?></a> </div>
<?php else: ?>
<span class="empty"><?php echo $this->__('You have no items in your shopping cart.') ?></span>
<?php endif ?>
</div>
</div>
</div>
</div>
<script>
jQuery(".close1").click(function()
{
jQuery(".shoppingcart").css("display", "none");
jQuery(".shopping_cart_mini").css("display", "none");
});
</script>
答案 0 :(得分:1)
你必须先loadLyaout
$this->loadLayout();
$sidebar_block = $this->getLayout()->getBlock('mycart');
$sidebar = $sidebar_block->toHtml();
$response['sidebar'] = $sidebar;
直接法
$block = $this->getLayout()->createBlock('core/template')->setTemplate('checkout/cart/mycart.phtml')->toHtml();
你可以看到这些在xml中使用
<block type="checkout/cart_sidebar" name="cart_sidebar" template="checkout/cart/sidebar.phtml" before="-">
<action method="addItemRender"><type>simple</type><block>checkout/cart_item_renderer</block><template>checkout/cart/sidebar/default.phtml</template></action>
<action method="addItemRender"><type>grouped</type><block>checkout/cart_item_renderer_grouped</block><template>checkout/cart/sidebar/default.phtml</template></action>
<action method="addItemRender"><type>configurable</type><block>checkout/cart_item_renderer_configurable</block><template>checkout/cart/sidebar/default.phtml</template></action>
<block type="core/text_list" name="cart_sidebar.extra_actions" as="extra_actions" translate="label" module="checkout">
<label>Shopping Cart Sidebar Extra Actions</label>
</block>
</block>