简体   繁体   English

magento中的字母顺序产品列表

[英]Alphabetical Product list in magento

I am going to show alphabet sequence at the footer ..like 我将在页脚显示字母顺序.. like

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

if user clicks on any alphabet , say user click on letter "B" .. product listing page should come with Products name starting with letter "B" 如果用户点击任何字母,请说用户点击字母“B”。产品列表页面应该带有以字母“B”开头的产品名称

any extension available in magento or I have to code for this . magento中可用的任何扩展名,或者我必须为此编码。

I Just Modified The Above Code ....... Which is Working Perfectly.. 我刚刚修改了以上代码.......哪个工作正常...

Go To Your Admin Panel "app/design/frontend/default/custom_theme/layout" add following Block To Your "Catalog.xml". 转到您的管理面板“app / design / frontend / default / custom_theme / layout”将以下块添加到您的“Catalog.xml”。

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

Right Under 正确的

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

Create New Html Block with name "alphabetical_search.phtml" Inside app/design/frontend/default/custom_theme/template/catalog/product/list 创建名为“alphabetical_search.phtml”的新Html块内部app / design / frontend / default / custom_theme / template / catalog / product / list

Add Following Code In File 在文件中添加以下代码

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

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

Find This Function "public function setCollection($collection)" 找到这个函数“public function setCollection($ collection)”

Replace Following Code With 替换以下代码

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

With

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());
}
}

Inside "app/design/frontend/default/responsive/template/catalog/product/list" Open File "toolbar.phtml" and paste Following Code any where you want 在“app / design / frontend / default / responsive / template / catalog / product / list”里面打开文件“toolbar.phtml”并在任何你想要的地方粘贴以下代码

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

Add That's It !!!! 添加它就是它!!!!

Please create below mentioned files in the app/core/code/local directory with complete directory structure as in Magento core. 请在app / core / code / local目录中创建下面提到的文件,其中包含完整的目录结构,如Magento核心。

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

In this file replace the setCollection function with following: 在此文件中,使用以下内容替换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;
    }

And

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

Open this file an replace the code with given below: 打开此文件,替换下面给出的代码:

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

Now add the function 'str_replace_once' in the following file 现在在以下文件中添加函数'str_replace_once'

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

The function, 功能,

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));
 }

And it is done. 它完成了。 Your alphabetical search is ready to use:) To add it to footer you can add the following block into the footer bloack. 您的字母搜索已准备好使用:)要将其添加到页脚,您可以将以下块添加到页脚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>

Note: Do not edit or add this code to your core files if you wish to upgrade Magento to newer versions in the future. 注意:如果您希望将来将Magento升级到更新版本, 不要编辑或将此代码添加到核心文件中。 Please create below mentioned files in the app/core/code/local directory with complete directory structure as in Magento core. 请在app / core / code / local目录中创建下面提到的文件,其中包含完整的目录结构,如Magento核心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM