简体   繁体   中英

How to set my custom URL for product collection in Magento custom module?

I have setup following page for product collection in custom module page which come from the following url as

http://localhost/magento/brand/htc

xml from frontend.xml file

<shop_index_pageview>
        <reference name="root">
            <action method="setTemplate"><template>page/2columns-left.phtml</template></action>
        </reference>
        <reference name="content">
            <block type="shop/shop" name="testattrproduct" template="shop/product.phtml" />
            <block type="shop/shop" name="shop" template="catalog/product/list.phtml">
              <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>
              <action method="setColumnCount"><column_count>6</column_count></action>
              <action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
              <action method="addColumnCountLayoutDepend"><layout>empty</layout><count>6</count></action>
              <action method="addColumnCountLayoutDepend"><layout>one_column</layout><count>5</count></action>
              <action method="addColumnCountLayoutDepend"><layout>two_columns_left</layout><count>4</count></action>
              <action method="addColumnCountLayoutDepend"><layout>two_columns_right</layout><count>4</count></action>
              <action method="addColumnCountLayoutDepend"><layout>three_columns</layout><count>3</count></action>
              <action method="setToolbarBlockName"><name>product_list_toolbar</name></action>     
            </block>              
      </reference>      
    </shop_index_pageview>

Using Routing i have set which module,controller,action to use.

<events>
        <controller_front_init_routers>
            <observers>
                <test_initRouter>
                    <type>singleton</type>
                    <class>Test_Shop_Controller_Router</class>
                    <method>initController</method>
                </test_initRouter>
            </observers>
        </shop_initRouter>
        </events>

File from Controller/Router.php

            ...
            ...
    public function match(Zend_Controller_Request_Http $request)
{  

        $front = $this->getFront(); 
    $identifier = trim($request->getPathInfo(), '/');  
    if ($identifier) {
        $p = explode('/', $identifier);
    } else {
        $p = explode('/', $this->_getDefaultPath());
    }    
        ...
            ...
    if(substr('brand', 0, strlen('brand')) =='brand'){ 
        if($p[1] == '')
            {
                $action = "view";
                $param = $action;
            }
        else
            {
                $action = "pageview";
                $param = $p[1];
            }
        $request->setModuleName('shop')
            ->setControllerName('index')
            ->setActionName(trim($action, '/'));
        if($p[1] == '')
            $request->setParam('param', $param);
        else
            $request->setParam('identifier', $param);
        return true;
    } 
}       
            ...
            ...

From block file

...
    protected function _getProductCollection() {

        if (is_null($this->_productCollection)) {
            $_getCurrentAttrObj = $this->_selected_attribute;
            // d($_getCurrentAttrObj->getData());
            if($_getCurrentAttrObj){
                $_defConfigAttrId = Mage::getStoreConfig('shop_by_brand/all_brands_page_setup/attribute_list');
                $arg_attribute = Mage::helper("shop")->getAttributeNameById($_defConfigAttrId);
                $collection = Mage::getModel('catalog/product')
                    ->getCollection('*')
                    ->addAttributeToSelect('*');
                $collection->addAttributeToFilter('test',12);
                Mage::getModel('catalog/layer')->prepareProductCollection($collection);
                $this->_productCollection = $collection;
            }
        }
        return parent::_getProductCollection();
    }   
....

This comes as expected.I can list the product collection from product.phtml Here i can see the url like as follows

> http://localhost/magento/shop/index/pageview/identifier/htc/?mode=list

Instead I need url in product collection as

> http://localhost/magento/brand/htc/?mode=list

How can i set/acheive routing url in product collection ?

I think setting the controller's "frontName" to "brand" would be the easiest way to achieve this without using a custom router.

<routers>
        <Example>
            <use>standard</use>
            <args>
                <module>Example</module>
                <frontName>Cool</frontName>
            </args>
        </Example>
 </routers>

But if the routing logic is more complex for your particular use case then you should define the router properly, not as an event observer.

<default>
    <web>
        <routers>
            <shop_router>
                <area>frontend</area>
                <class>Test_Shop_Controller_Router</class>
            </shop_router>
        </routers>
    </web>
</default>

Use overriding or rewriting in config.xml

 <global>
    <blocks>
      <shop>
        <class>Test_Shop_Block</class>
      </shop>
     <catalog>
        <rewrite>
          <product_list_toolbar>Test_Shop_Block_Product_List_Toolbar</product_list_toolbar>
        </rewrite>
      </catalog>      
    </blocks>
  </global>

And From Toolbar.php

class Test_Shop_Block_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar
{

    public function getPagerUrl($params=array())
    {
        $getDefaultUrl = new Magecart_Shopbyattribute_Block_Shop();
        $getDefaultUrlKeyForAttr = $getDefaultUrl->getDefaultUrlKeyForAttr();
        $_params = $this->getRequest()->getParams();
        $identifier = $_params['identifier'];
        $urlParams = array();
        $urlParams['_current']  = false;
        $urlParams['_escape']   = true;
        $urlParams['_use_rewrite']   = true;
        $urlParams['_query']    = $params;

        return $this->getUrl($getDefaultUrlKeyForAttr.'/'.$identifier, $urlParams);
    }


}

That is it. Now the magento will use the url from getPagerUrl() function :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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