[英]Alphabetical Product list in magento
我將在頁腳顯示字母順序.. like
A | B | C | ....Y | Z
如果用戶點擊任何字母,請說用戶點擊字母“B”。產品列表頁面應該帶有以字母“B”開頭的產品名稱
magento中可用的任何擴展名,或者我必須為此編碼。
我剛剛修改了以上代碼.......哪個工作正常...
轉到您的管理面板“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>
添加它就是它!!!!
請在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核心。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.