Magento – custom advanced search

advanced-searchmagento-1.9

I trying to duplicate advanced search forum with some custom attribute, this clone is a filter page so is different from the real advanced search, for do this I copy advanced search form into a template, now I need to replace

$this->getSearchableAttributes();

with my custom attribute, so i try something like this:

$attribute = Mage::getModel( 'eav/config' )->getAttribute( 'catalog_product' , 'formulazione' );
$array[] = $attribute;

but without success, one thing i note that $this in the original form is different from $this inside my template, I think the problem is this but I don't know how to solve it

Best Answer

You will need to extend few advance search classes.

First I believe you already familiar with magento module developing.

Now, you need to add this block file

class NameSpace_YourModule_Block_Search_Form extends Mage_CatalogSearch_Block_Advanced_Form
{

    /**
     * Retrieve collection of product searchable attributes
     *
     * @return Varien_Data_Collection_Db
     */
    public function getSearchableAttributes()
    {
        $attributes = $this->getModel()->getAttributes();
        return $attributes;
    }

    /**
     * Retrieve advanced search model object
     *
     * @return Mage_CatalogSearch_Model_Advanced
     */
    public function getModel()
    {
        return Mage::getSingleton('yourblock/block'); //this is your block name what you have declared in config.xml
    }
}

Now in your module

class NameSpace_YourModule_Block_Search_Form extends Mage_CatalogSearch_Model_Advanced
{
    /**
     * Retrieve array of attributes used in advanced search
     *
     * Filter for specific attributes only
     * @return array
     */
    public function getAttributes()
    {
        /* @var $allAttributes Mage_Catalog_Model_Resource_Eav_Resource_Product_Attribute_Collection */
        $allAttributes = $this->getData('attributes');
        if (is_null($allAttributes)) {
            $product = Mage::getModel('catalog/product');
            $allAttributes = Mage::getResourceModel('catalog/product_attribute_collection')
                ->addHasOptionsFilter()
                ->addDisplayInAdvancedSearchFilter()
                ->addStoreLabel(Mage::app()->getStore()->getId())
                ->setOrder('main_table.attribute_id', 'asc')
                ->load();

            $attributes = array();
            foreach ($allAttributes as $attribute) {
                if($attribute->getAttributeCode() == 'your_attribute_1' || $attribute->getAttributeCode() == 'your_attribute_2'){ //you can add as many as you want
                    $attributes[] = $attribute;
                }
            }
            foreach ($attributes as $attribute) {
                $attribute->setEntity($product->getResource());
            }
            $this->setData('attributes', $attributes);
        }
        return $attributes;
    }
}

Change your_attribute_1 and your_attribute_2 to your own attribute id.

Now, when you call $this->getSearchableAttributes() in your phtml file it will be showing only your own attributes.

Obviously you will need to create a controller to show your form and show the result too.

Your controller can have these codes only:

public function indexAction()
    {
        $this->loadLayout();
        $this->renderLayout();
        //Zend_Debug::dump($this->getLayout()->getUpdate()->getHandles());
    } 

Remember you will also need layout file to render your index action.

Related Topic