Friday, December 31

How to add search by category option In Magento

On the advanced search page, “search by category” is not an option by default, but can be very helpful. To add this, you will need to edit the following files:

app/code/core/Mage/CatalogSearch/Block/Advanced/Form.php

app/code/core/Mage/CatalogSearch/Model/Advanced.php

app/design/yourdesign/yourdesign/template/catalogsearch/advanced/form.phtml


At the very end of app/code/core/Mage/CatalogSearch/Block/Advanced/Form.php (before the closing brace), add:

<?php
public function getStoreCategories()
{
        $helper = Mage::helper('catalog/category');
        return $helper->getStoreCategories();
}
?>

In app/code/core/Mage/CatalogSearch/Model/Advanced.php, replace the getSearchCriterias() function (line 157) with the code below:

public function getSearchCriterias()

          {

              $search = $this->_searchCriterias;

              /* display category filtering criteria */
  if(isset($_GET['category']) && is_numeric($_GET['category'])) {

                  $category = Mage::getModel('catalog/category')->load($_GET['category']);

                  $search[] = array('name'=>'Category','value'=>$category->getName());
              }

              return $search;

          }

replace the next function, getProductCollection(), with:

<?php
public function getProductCollection(){

        if (is_null($this->_productCollection)) {

            $this->_productCollection = Mage::getResourceModel('catalogsearch/advanced_collection')

                ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())

                ->addMinimalPrice()

               ->addStoreFilter();

                Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($this->_productCollection);

                Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($this->_productCollection);

            /* include category filtering */

            if(isset($_GET['category']) && is_numeric($_GET['category'])) $this->_productCollection->addCategoryFilter(Mage::getModel('catalog/category')->load($_GET['category']),true);

        }

 

        return $this->_productCollection;

    }
?>

In app/design/yourdesign/yourdesign/template/catalogsearch/advanced/form.phtml, after this code:

<?php foreach ($this->getSearchableAttributes() as $_attribute): ?>

        <?php $_code = $_attribute->getAttributeCode() ?>

        <li>

            <label for="<?php echo $_code ?>"><?php echo $this->getAttributeLabel($_attribute) ?></label>

            <?php switch($this->getAttributeInputType($_attribute)):

                case 'number': ?>

                <div class="range field-row">

                    <input name="<?php echo $_code ?>[from]" value="<?php echo $this->htmlEscape($this->getAttributeValue($_attribute, 'from')) ?>" id="<?php echo $_code ?>" title="<?php echo $this->htmlEscape($this->getAttributeLabel($_attribute)) ?>"  class="input-text validate-number" type="text" />

                    <input name="<?php echo $_code ?>[to]" value="<?php echo $this->htmlEscape($this->getAttributeValue($_attribute, 'to')) ?>" id="<?php echo $_code ?>_to" title="<?php echo $this->htmlEscape($this->getAttributeLabel($_attribute)) ?>"  class="input-text validate-number" type="text"/>

                </div>

                <?php break;

                case 'select': ?>

                    <?php echo $this->getAttributeSelectElement($_attribute) ?>

                <?php break;

                case 'yesno': ?>

                    <?php echo $this->getAttributeYesNoElement($_attribute) ?>

                <?php break;

               case 'date': ?>

                    <?php echo $this->getDateInput($_attribute, 'from') ?>

                    -

                    <?php echo $this->getDateInput($_attribute, 'to') ?>

                <?php break;

                default: ?>

                <input name="<?php echo $_code ?>" id="<?php echo $_code ?>" value="<?php echo $this->htmlEscape($this->getAttributeValue($_attribute)) ?>" title="<?php echo $this->htmlEscape($this->getAttributeLabel($_attribute)) ?>"  class="input-text <?php echo $this->getAttributeValidationClass($_attribute) ?>" type="text" />

            <?php endswitch; ?>

        </li>

        <?php endforeach; ?>

add

<li>

            <label for="category_search_field">Search by Category:</label>

            <select name="category" id="category_search_field">

                <option value="">-- Any Category --</option>

                <?php foreach ($this->getStoreCategories() as $_category): ?>

                <?php if($_category->hasChildren()): ?>

                <option class="parent-cat" value="<?php echo $_category->getId(); ?>"><?php echo $_category->getName();?></option>

                <?php foreach ($_category->getChildren() as $subcategory):

                if($subcategory->getIsActive()) : ?>

                    <option value="<?php echo $subcategory->getId(); ?>"<?php echo ($this->getRequest()->getQuery('category') == $subcategory->getId() ? ' selected="selected"': "") ?>><?php echo $subcategory->getName(); ?></option>

                <?php endif; endforeach; ?>

                <?php elseif($_category->getIsActive()): ?>

                <option value="<?php echo $_category->getId(); ?>"><?php echo $_category->getName();?></option>

                <?php endif; ?>

                <?php endforeach ?>

 

            </select>

        </li>

Now, If you are going to search only by Category then you will face an error message like “You have to specify at least one search term”. To solve it you need to modify following file:

*
app/code/core/Mage/CatalogSearch/Model/Advanced.php

<?php
      if ($allConditions) {
   
                  $this->getProductCollection()->addFieldsToFilter($allConditions);
   
              } else if (!count($filteredAttributes)) {
   
                  Mage::throwException(Mage::helper('catalogsearch')->__('You have to specify at least one search term'));
   
              }?>

with following codes:

 <?php
      if (($allConditions) || (isset($values['category']) && is_numeric($values['category']))) {
   
                  $this->getProductCollection()->addFieldsToFilter($allConditions);
   
              } else if (!count($filteredAttributes)) {
   
                  Mage::throwException(Mage::helper('catalogsearch')->__('You have to specify at least one search term'));
   
              }?>

Now you won’t face any problem in advanced search only by Category or any single attribute search.

Hope it Helps... Thanks......

Saturday, December 11

Change default Home page url to any page url in Magento

To change default index page of magento any cms page or any page like login page is quite easy .To do so you need to login to the admin panel then goto System -> Configuration , then click on web tab from the right side tab.Now click on Default Pages tab and change the Default web url as per your required Url.Here is one example where I have changed my Homepage url to Login page Url


Monday, December 6

A clean way to move Magento from Local to Server without edit any file (12 steps)

1.  Compress your local Magento folder in a zip file, and put the package in a safe place in your hard drive.

2.  Export Magento data base with phpmyadmin (make sure to check SQL and the “save as file” option) and save the data base in a safe place in your hard drive

3.  Upload the Magento zip package in your live server (root directory), and uncompress it (I use c-panel file manager).

4.  Once unzipped the package, make sure there is nothing in the cache directory (if you found some files delete them)

a.  var/cache /

b.  var/session/

5.  Delete this file: app/etc/local.xml (This run a new installation, that we start on step 10)
(Making sure that Magento connect will work)

6.  Download an original installation package of your Magento version, and copy this file:

a.  downloader/pearlib/clean

b.  downloader/pearlib/ pear

c.  downloader/pearlib/ peardev

d.  downloader/pearlib/ pecl


7.  Go to your live server Magento folder: downloader/pearlib/ and replace the files that you find, with files copied in step number 6 (you need to use on more time an ftp client)
(data base setup)

8.  Open your live server phpmyadmin and create a new data base for example magdb

9.  Import inside your new data base (for example magdb) the SQL data base that you have exported in step number 2
(Installation of Magento)

10.  Go to your domain using your favorite browser, and run a new installation of magento

11.  Connect it to the data base created on step number 8

12.  Choose your final data ex: admin patch, user and password, etc. And make sure to save the new Key encrypt
That’s all.
Note that all of your extensions are installed and operating, including Magento Connect
Possible problems divided by step

• Step 3: If you don’t’ have an unzipper tool on your hosting upload the uncompressed folder using an ftp client

• Step 9: if your data base is too big, try to upload it in phpmyadmin as a zip file

• Step 10: If you find any errors, make sure folder permissions are set to 755 and file permissions to 644. If your administration submenu pulldowns don’t work, make sure /js/index.php is 644.

• Step 11: If your Magento installation does not connect to the data base, make sure to use the data access of your live server data base, and make sure that the step number 4 is done

Thursday, December 2

Display Subcategory Listing in Parent Caategories

1. In cms select Static Pages
a. Set the Block Identifyer to for example “Sub Category Listing”
b. Set the Identifyer to “subcategory_listing”
c. In the content section add “{{block type="catalog/navigation" template="catalog/navigation/subcategory_listing.phtml"}}"

2. Now take this code and create a new file called for example “subcategory_listing.phtml” this file needs to live in the “catalog/navigation” directory


<?php $_categories=$this->getCurrentChildCategories(); ?>
<div class="listing-type-list catalog-listing">
    
<?php foreach ($_categories as $_category): ?>
    
<table width="100%" border="0" cellspacing="0" cellpadding="0">
            
<?php if($_category->getIsActive()): ?>
            
<tr>
                <
td width="30%">
                <
div class="listing-item">
                <
div class="cat-box-text">
                      <
h6><a href="<?php echo $_category->getURL() ?>"

  title="<?php echo $this->htmlEscape($_category->getName()) ?>"> 
<?php echo $this->htmlEscape($_category->getName()) ?></a></h6>
                        </
div>
                        <
div class="product-image">
                            <
a href="<?php echo $_category->getURL() ?>" 

 title="<?php echo $this->htmlEscape($_category->getName()) ?>">
                                <
img src="<?php echo $_category->getImageUrl() ?>"

 width="100" alt="<?php echo $this->htmlEscape($_category->getName()) ?>" />
                            </
a>
                                
<?php /* echo "Find this item->" */ ?>
                        
</div>
                    </
div>
              
                </
td>
                <
td width="40%">
                    <
div class="listing-item">
                        <
div class="cat-box-text">
                        
<?php echo $_category->getDescription() ?>
                        
</div>
                    </
div>
                </
td>
            
<?php endif; ?>
            
</tr>
        </
table>
    
<?php endforeach; ?></div>



3. Now Goto Manage Categories. 
a. Pick a Sub Category Entry 
b. Set the display mode to “Static Block Only” 
c. From the CMS Block dropdown select the Static Page “Sub Category Listing” 
d. Set the is anchor to no

i have just set a site live you can see this in action http://www.shrinova.com