magento中的字母顺序产品列表

时间:2013-06-11 09:27:43

标签: php magento

我将在页脚显示字母序列..赞

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

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

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

2 个答案:

答案 0 :(得分: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">

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

在文件中添加以下代码

<?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>

转到app / code / core / Mage / Catalog / Block / Product / List

查找此函数“public function 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>

添加它就是它!!!!

答案 1 :(得分: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));
 }

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

<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核心。