0

我将在页脚显示字母顺序 ..like

A | B | C | ....Y | Z 

如果用户单击任何字母,则说用户单击字母“B”..产品列表页面应带有以字母“B”开头的产品名称

magento 中可用的任何扩展,或者我必须为此编写代码。

4

2 回答 2

2

我刚刚修改了上面的代码.. 工作完美..

转到您的管理面板“app/design/frontend/default/custom_theme/layout”将以下块添加到您的“Catalog.xml”。

<block type="catalog/product_list" name="alphabetical_search" template="catalog/product/list/alphabetical_search.phtml"/>

正下方

<block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">

在 app/design/frontend/default/custom_theme/template/catalog/product/list 中创建名为“alphabetical_search.phtml”的新 Html 块

在文件中添加以下代码

<?php
//Create Array For Alphabets A-Z
$search = array( 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','ALL' );
?>
<div>
<?php 
//Get Value Of Search String From URL
$postdata = Mage::app()->getRequest()->getParam('alpha');
foreach( $search as $value )
{
//If Current Url Already Using Filter Then Clean It...
if (strstr( $this->helper('core/url')->getCurrentUrl(), "?" )) 
{
//Clean Url Each Time For New Filter
$newurl = substr( $this->helper('core/url')->getCurrentUrl(), 0, strpos( $this->helper('core/url')->getCurrentUrl(), '?' ) );
}
// Create Mainurl For search...
$mainurl = $newurl.'?alpha='.strtolower($value);
?>
<a href="<?php echo $mainurl; ?>">
<?php   if( $this->helper('core/url')->getCurrentUrl() == $mainurl )
{
?>
<strong><?php echo $value; ?></strong>
<?php
}
else
{
echo $value;
}
?>
</a>
<?php
}
?>
</div>

转到应用程序/代码/核心/法师/目录/块/产品/列表

查找此函数“公共函数 setCollection($collection)”

将以下代码替换为

if ($this->getCurrentOrder()) {
$this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
}

if ($this->getCurrentOrder()) {
// Get String Name To Filter
$postdata = Mage::app()->getRequest()->getParam('alpha');
// if postdata is not empty and not equal to all then ....
if( $postdata != '' && $postdata != 'all' )
{
// Custom Set Attribute To Filter Using Name 
$this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection())->addAttributeToFilter(array(array('attribute'=>'name', 'like'=>$postdata.'%')));
}
else
{
$this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
}
}

在“app/design/frontend/default/responsive/template/catalog/product/list”中打开文件“toolbar.phtml”并将以下代码粘贴到您想要的任何位置

<div class="alphabetical_search">
<?php echo $this->getChildHtml('alphabetical_search'); ?>
</div>

添加就是它!!!!

于 2014-05-07T14:04:52.763 回答
0

请在 app/core/code/local 目录中创建下面提到的文件,并具有完整的目录结构,如 Magento 核心。

app\code\local\Mage\Catalog\Block\Product\List\Toolbar.php

在此文件中,将 setCollection 函数替换为以下内容:

public function setCollection($collection)
    {
        $this->_collection = $collection;

        $this->_collection->setCurPage($this->getCurrentPage());

        // we need to set pagination only if passed value integer and more that 0
        $limit = (int)$this->getLimit();
  $postData = '';
        if ($limit) {
            $this->_collection->setPageSize($limit);
        }
        if ($this->getCurrentOrder()) 
  {
   /**********Alphabetic search Code Start From here**********/
   $postData = Mage::app()->getRequest()->getParam('alpha').'%';
   if(isset($postData['alpha']) && $postData['alpha']!= '' && trim($postData) !='ALL')
   {
    $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection())->addAttributeToFilter(array(
                    array('attribute'=>'name', 'like'=>$postData)
                ));
   }
   else
   {
      $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
   }
   /**********Alphabetic search Code ends here**********/
        }
        return $this;
    }

app\design\frontend\default\default\template\catalog\product\list\toolbar_bottom.phtml

打开这个文件,用下面给出的代码替换:

<?php 
$search_array = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','ALL');

/*Find if the URL already contains any querystring variable or not */
if (strstr( $this->helper('core/url')->getCurrentUrl(), "&" ))
{
 $separator = '&'; 
}
else
{
    $separator = '?';
}
?>
    <div>
        <p class="view-mode-list-bot">
            <?php 
   $postData = Mage::app()->getRequest()->getParam('alpha');
   foreach ($search_array  as $search_array_value):

   /*Clean the URL*/
   if (strstr( $this->helper('core/url')->getCurrentUrl(), "?" ) )
   {
    $new_Url =  $this->str_replace_once('&','?',str_replace("?alpha=".trim($postData['alpha']),'',str_replace($separator."alpha=".trim($postData['alpha']),'',$this->helper('core/url')->getCurrentUrl())));
   }
   else
   {
    $new_Url = str_replace("?alpha=".trim($postData['alpha']),'',str_replace($separator."alpha=".trim($postData['alpha']),'',$this->helper('core/url')->getCurrentUrl()));
   }

   $alphaURL = $new_Url.$separator.'alpha='.$search_array_value;
?>

                    <a href="<?php echo $alphaURL; ?>" title="<?php echo $_label ?>" class="<?php echo strtolower($_code); ?> <?php if($search_array_value == $postData){ echo 'remove_under'; } ?>"><?php echo $search_array_value; ?></a>   

            <?php endforeach; ?>

        </p>

    </div>

现在在以下文件中添加函数“str_replace_once”

app\code\local\Mage\Catalog\Block\Product\List\Toolbar.php

功能,

public function str_replace_once ($needle, $replace, $haystack) {
     // Looks for the first occurence of $needle in $haystack
     // And replaces it with $ replace.
         $pos = strpos ($haystack, $needle);
         if ($pos === false) {
             // Nothing found
         return $haystack;
         }
    return substr_replace ($haystack, $replace, $pos, strlen($needle));
 }

它已经完成了。您的字母搜索已准备好使用:) 要将其添加到页脚,您可以将以下块添加到页脚块中。

<block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
                            <block type="page/html_pager" name="product_list_toolbar_pager"/>
</block>

注意:如果您希望将来将 Magento 升级到更新版本,请不要编辑或将此代码添加到您的核心文件中。请在 app/core/code/local 目录中创建下面提到的文件,并具有完整的目录结构,如 Magento 核心。

于 2013-06-11T09:40:12.280 回答