【问题标题】:Magento add id attribute to top linksMagento 将 id 属性添加到顶部链接
【发布时间】:2015-02-26 21:23:37
【问题描述】:

我想为所有顶部链接添加 id 属性。我有一个解决方案如下

<action method="addLink" translate="label title" module="customer"><label>My Account</label><url helper="customer/getAccountUrl"/><title>My Account</title><prepare/><urlParams/><position>10</position><liParams>id="myaccount"</liParams></action>

添加 liParams 仅适用于“我的帐户”链接,此解决方案不适用于“我的购物车”和“结帐”链接。

【问题讨论】:

  • 哪个magento版本???

标签: magento magento-1.7


【解决方案1】:

结帐链接使用位于app/code/core/Mage/Checkout/Block/Links.php的以下函数

public function addCartLink()
    {
        ////.....
        $parentBlock->addLink($text, 'checkout/cart', $text, true, array(), 50, null, 'class="top-link-cart"');
        return $this;
    }

您可以看到它不包含任何&lt;liParams&gt;&lt;aParams&gt;。而位于Mage_Page_Block_Template_LinksaddLink() 方法包含如下所示的参数

public function addLink($label, $url='', $title='', $prepare=false, $urlParams=array(),
        $position=null, $liParams=null, $aParams=null, $beforeText='', $afterText='')

因此您可以执行以下任一操作

  1. 覆盖您本地的 addCartLink() 并将参数添加为 需要

  2. 删除结帐链接,然后再次使用显式 URL 添加它,然后 参数

例如在您的local.xml 中,通过local.xml 添加自定义链接到Top Links:

<reference name="top.links">
    <action method="addLink" translate="label title">
        <label>My Link</label>
        <url>path/to/page</url>
        <title>My link tooltip</title>
        <prepare>true</prepare>
        <urlParams/>
        <position>150</position>
        <liParams>id="my-custom-id"</liParams>
    </action>
</reference>

【讨论】:

  • 谢谢!我会仔细看看的!如果它有效,你会得到一个很好的复选标记!
【解决方案2】:

@Nilesh Yadav、购物车和结帐链接使用不同的方法创建顶部链接

<action method="addCartLink"></action>
   <action method="addCheckoutLink"></action>

其他用到的addLink函数

结账和购物车使用类Mage_Checkout_Block_Links函数addCheckoutLink and addCartLink

修改xml代码

  <reference name="top.links">
            <block type="checkout/links" name="checkout_cart_link">
                <action method="addCartLink"><liParams>id="my-custom-id"</liParams></action>
                <action method="addCheckoutLink">  <liParams>id="my-custom-id"</liParams></action>
            </block>
        </reference>

Copy app/code/core/Mage/Checkout/Block/Links.php

app/code/local/Mage/Checkout/Block/Links.php

addCartLink中的goto函数修改逻辑

 public function addCartLink($liparams=null)
 {
.....
if(is_null())
{ $parentBlock->addLink($text, 'checkout/cart', $text, true, array(), 50, null, 'class="top-link-cart"');
}else
{
$parentBlock->addLink($text, 'checkout/cart', $text, true, array(), 50, $liparams, 'class="top-link-cart"');
}

....

还有

  public function addCheckoutLink($liparams=null)
    {
....
        if ($parentBlock && Mage::helper('core')->isModuleOutputEnabled('Mage_Checkout')) {
            $text = $this->__('Checkout');
        if(is_null()){
            $parentBlock->addLink(
                $text, 'checkout', $text,
                true, array('_secure' => true), 60, null,
                'class="top-link-checkout"'
            );
    }else{
            $parentBlock->addLink(
                $text, 'checkout', $text,
                true, array('_secure' => true), 60, $liparams=null,
                'class="top-link-checkout"'
            );

        }
        }
..
    }

【讨论】:

    【解决方案3】:

    addCartLink() 函数用于在magento 中创建顶部购物车链接。 重写 Mage_Checkout_Block_Links 类并更改 addCartLink() 函数如下 或者简单地将 app/code/core/Mage/Checkout/Block/Links.php 复制到 app/code/local/Mage/Checkout/Block/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->__('Cart (%s)', $count);
            } elseif ($count > 0) {
                $text = $this->__('Cart (%s)', $count);
            } else {
                $text = $this->__('Cart (0)');
            }
    
            $parentBlock->removeLinkByUrl($this->getUrl('checkout/cart'));
            $parentBlock->addLink($text, 'checkout/cart', $text, true, array(), 50, 'id="top_cart"', 'class="top-link-cart"'); //add your custom class or id here
        }
        return $this;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-23
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多