当购物车的数量为空的时候,magentonotes.com/checkout/cart/index/ 页面默认显示的页面有点简单,我们可以通过编写简单的自定义模块,
require_once 'Mage/Checkout/controllers/CartController.php';
class Mynamespace_Myextension_Checkout_CartController extends Mage_Checkout_CartController
{
public function indexAction()
{
...
$helper = Mage::helper('checkout/cart');
if (!$helper->getItemsCount()) {
$this->loadLayout()
->getLayout()
->getBlock('root')
->setTemplate('page/2columns-left.phtml');
}
$this->renderLayout();
....
}
....
}
<?xml version="1.0"?> <config> ... <global> ... <models> <myextension> <class>Mynamespace_Myextension_Model</class> </myextension> </models> ... </global> ... <frontend> ... <events> <controller_action_layout_render_before_checkout_cart_index> <observers> <myextension_observer> <class>myextension/observer</class> <method>setRootTemplate</method> </myextension_observer> </observers> </controller_action_layout_render_before_checkout_cart_index> </events> ... </frontend> ... </config>
class Mynamespace_Myextension_Model_Observer
{
public function setRootTemplate()
{
$cartHelper = Mage::helper('checkout/cart');
$layout = Mage::getSingleton('core/layout');
if (!$cartHelper->getItemsCount()) {
$layout->getBlock('root')->setTemplate('page/2columns-left.phtml');
}
}
}
$layout->getBlock('root')->setTemplate('page/2columns-left.phtml');
... <frontend> ... <events> ... <controller_action_layout_load_before> <observers> <customcheckout_observer> <class>customcheckout/observer</class> <method>setCustomLayoutHandle</method> </customcheckout_observer> </observers> </controller_action_layout_load_before> ... </events> ... </frontend> ...
public function setCustomLayoutHandle($observer)
{
$cartHelper = Mage::helper('checkout/cart');
$layout = $observer->getEvent()->getLayout();
$update = $layout->getUpdate();
if (!$cartHelper->getItemsCount() && in_array('checkout_cart_index', $update->getHandles())) {
$layout->getUpdate()
->removeHandle('checkout_cart_index')
->addHandle('checkout_cart_index_empty');
}
}
... <checkout_cart_index_empty translate="label"> ... customize the content of checkout_cart_index handle ... </checkout_cart_index_empty>
|