购物车为空时隐藏结帐按钮

时间:2013-07-10 17:50:26

标签: magento magento-1.7

当购物车为空时,如何隐藏顶部菜单上的结帐按钮。对我来说,当客户端没有添加任何产品时,按钮就没有意义了。

解决!

谢谢你们。在这里,我的代码如何(Links.php):

public function addCartLink()
{
    $parentBlock = $this->getParentBlock();
    if ($parentBlock && Mage::helper('core')->isModuleOutputEnabled('Mage_Checkout')) {
        $count = $this->getSummaryQty() ? $this->getSummaryQty()
            : $this->helper('checkout/cart')->getSummaryCount();
        if ($count == 1) {
            $text = $this->__('My Cart (%s item)', $count);
        } elseif ($count > 0) {
            $text = $this->__('My Cart (%s items)', $count);
        } else {
            //$text = $this->__('My Cart');
           //added 
           $text = '';
        }

        $parentBlock->removeLinkByUrl($this->getUrl('checkout/cart'));
        //$parentBlock->addLink($text, 'checkout/cart', $text, true, array(), 50, null, 'class="top-link-cart"');

        //added
        if($text != ""){
            $parentBlock->addLink($text, 'checkout/cart', $text, true, array(), 50, null, 'class="top-link-cart"');
        }
    }
    return $this;
}

/**
 * Add link on checkout page to parent block
 *
 * @return Mage_Checkout_Block_Links
 */
public function addCheckoutLink()
{
    if (!$this->helper('checkout')->canOnepageCheckout()) {
        return $this;
    }

    $parentBlock = $this->getParentBlock();
    //if ($parentBlock && Mage::helper('core')->isModuleOutputEnabled('Mage_Checkout')) {
    if ($parentBlock && Mage::helper('core')->isModuleOutputEnabled('Mage_Checkout') && count(Mage::getModel('checkout/cart')->getQuote()->getAllItems())) {
        $text = $this->__('Checkout');
        $parentBlock->addLink(
            $text, 'checkout', $text,
            true, array('_secure' => true), 60, null,
            'class="top-link-checkout"'
        );
    }
    return $this;
}

2 个答案:

答案 0 :(得分:1)

if ( count(Mage::getModel('checkout/cart')->getQuote()->getAllItems()) ) {

YOUR LINK TO CART

}

答案 1 :(得分:1)

你可以为此写一个新的扩展,这是最佳实践。 如果您认为核心文件不是什么大问题,也可以对核心文件进行更改。 在

  

应用\代码\核心\法师\结帐\块\ Links.php

使用整个addCartLink函数添加替换以下代码行

 public function addCartLink()
        {
            $parentBlock = $this->getParentBlock();
            if ($parentBlock && Mage::helper('core')->isModuleOutputEnabled('Mage_Checkout')) {
                $count = $this->getSummaryQty() ? $this->getSummaryQty()
                    : $this->helper('checkout/cart')->getSummaryCount();
                if ($count == 1) {
                    $text = $this->__('My Cart (%s item)', $count);
                } elseif ($count > 0) {
                    $text = $this->__('My Cart (%s items)', $count);
                } else {
                    //$text = $this->__('My Cart'); 
                    $text = ''; //change this line
                }

                $parentBlock->removeLinkByUrl($this->getUrl('checkout/cart'));
                //$parentBlock->addLink($text, 'checkout/cart', $text, true, array(), 50, null, 'class="top-link-cart"');
                if($text != ""){
                $parentBlock->addLink($text, 'checkout/cart', $text, true, array(), 50, null, 'class="top-link-cart"');
                 }
            }
            return $this;
        }