Thứ Năm, 17 tháng 5, 2012

addFiledToFilter Condition In Magento


As I already mentioned on the previous post that we only can give the condition of attribute name to filter the result. There is another method to filter result that you can use in your customer Module. Let you are having a customer module which is responsible to show some banner on the homepage. For that let you have some place to upload image and also have feature to give sort order and enable disable feature. Here in the below code I will show how to filter the result with addFieldtoFilter



$banner = Mage::getModel('mymodulename/banner')->getCollection();



Now I wish to show banner whose status are active. I already have a field name/column name 'status' in my custom table.



$banner -> addFieldToFilter('status', array('eq' => 1));


Now I will sort the result according to sort order in my table. I already have a field name 'sort_order' in my table. To sort according to that column I will write



$banner -> setOrder('sort_order', 'DESC');


Thứ Tư, 16 tháng 5, 2012

addAttributeToFilter Condition In Magento


addAttributeToFilter Condition is used to Filter the collection query of magento . we can use to to filter the result of category collection, Product Collection, Customer collection, Order collection etc.


When you use this condition into the collection make sure your filterable option should be an attribute not should be a table column or fieldname.


Below is the example to fetch all products and using addAttributeToFilter to filter the result



$collection = Mage::getModel('catalog/product')->getCollection();



Below are all types of condition which you can use To Filter the result



$collection -> addAttributeToFilter('status', array('eq' => 1));

$collection -> addAttributeToFilter('status', array('neq' => 0));

$collection -> addAttributeToFilter('id' , array('in' => array(1,2,3,4)));

$collection -> addAttributeToFilter('id' , array('nin' => array(5,6)));

$collection -> addAttributeToFilter('sku' , array('like' => "%a%"));

$collection -> addAttributeToFilter('sku' , array('nlike' => "test%"));

$collection -> addAttributeToFilter('weight', array('gt' => 10));

$collection -> addAttributeToFilter('weight', array('lt' => 1));

$collection -> addAttributeToFilter('name' , 'notnull');

$collection -> addAttributeToFilter('sort_description' , 'null');


Thứ Ba, 27 tháng 3, 2012

How to refresh magento cache using magento php code

create a file in the root folder of your magento for example refreshcache.php and write the below code to refresh your magento cache without logging to Admin panel
require_once 'app/Mage.php';

umask( 0 );

Mage :: app( "default" );

$ver = Mage :: getVersion();

$userModel = Mage :: getModel( 'admin/user' );

$userModel -> setUserId( 0 );

Mage :: getSingleton( 'admin/session' ) -> setUser( $userModel );

echo "Refreshing cache...\n";

Mage :: app() -> cleanCache();

$enable = array();

foreach ( Mage :: helper( 'core' ) -> getCacheTypes() as $type => $label ) {

$enable[$type] = 1;

}

Mage :: app() -> saveUseCache( $enable );

echo "Cache refreshed";

Thứ Tư, 28 tháng 12, 2011

How to override catalogsearch model class in magento

In magento if you wish to override some controller then definately you will get lots of site which will describe how to do that but for model class, you will have some result. So I think to share my knowledge here. I am going to override default catalog search Model of magento. For that I will create an module with name space name Mage and module name Searchby. I hope you people know how to create an module so I will show you how to override model class only

Go to config.xml of your custom module then write the below code to override the catalogsearch model class

<?xml version="1.0" encoding="UTF-8"?>

<config>

    <modules>

        <Mage_Searchby>

            <version>0.1.0</version>

        </Mage_Searchby>

    </modules>

    <frontend>

        <routers>

            <searchby>

                <use>standard</use>

                <args>

                    <module>Mage_Searchby</module>

                    <frontName>searchby</frontName>

                </args>

            </searchby>

        </routers>        

    </frontend>

    <global>

        <models>

            <catalogsearch>

                <rewrite>

                    <layer>Mage_Searchby_Model_Layer</layer>

                </rewrite>

            </catalogsearch>

        </models>

    </global>

</config>

Now create an Layer.php file inside yourmodulename/Model then write the below code

<?php

class Mage_Searchby_Model_Layer extends Mage_CatalogSearch_Model_Layer

{

    public function prepareProductCollection($collection)

    {

        //Code to be executed

    }

}?>

That's all thanks

Thứ Sáu, 25 tháng 11, 2011

How to add search by category in magento mini search



There is no free extension in magento commerce which will help you to search your store with particular category. I tried from myself and able to do, I am sharing my exprerience here with.

First create the drop down list of all category in form.mini.phtml

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

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

<?php foreach ($catalog->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>

Now go to app/code/core/Mage/CatalogSearch/Helper and open the Data.php and add the below code

public function getStoreCategories()

{

$helper = Mage::helper('catalog/category');

return $helper->getStoreCategories();

}

public function getSelectedCategory()

{

$catid = (int)addslashes($_REQUEST['category']);

$cat="";

if($catid>1)

$cat = Mage::getModel('catalog/category')->load($catid);

return $cat;

}

Now go to app/code/core/Mage/CatalogSearch/Model and open the Layer.php

replace

public function prepareProductCollection($collection)

{

$collection

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

->addSearchFilter(Mage::helper('catalogsearch')->getQuery()->getQueryText())

->setStore(Mage::app()->getStore())

->addMinimalPrice()

->addFinalPrice()

->addTaxPercents()

->addStoreFilter()

->addUrlRewrite();

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

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

return $this;

}

with

public function prepareProductCollection($collection)

{

if(Mage::helper('catalogsearch')->getSelectedCategory()!="")

{

$collection

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

->addSearchFilter(Mage::helper('catalogsearch')->getQuery()->getQueryText())

->setStore(Mage::app()->getStore())

->addMinimalPrice()

->addFinalPrice()

->addTaxPercents()

->addStoreFilter()

->addCategoryFilter(Mage::helper('catalogsearch')->getSelectedCategory())

->addUrlRewrite();

}

else

{

$collection

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

->addSearchFilter(Mage::helper('catalogsearch')->getQuery()->getQueryText())

->setStore(Mage::app()->getStore())

->addMinimalPrice()

->addFinalPrice()

->addTaxPercents()

->addStoreFilter()

->addUrlRewrite();

}

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

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

return $this;

}

save and Enjoy.

Thứ Tư, 16 tháng 11, 2011

cache is not generating in magento

Magento stores cache inside the cache folder which resides under Var folder of your root magento ,But if your cache folder or var folder have not correct file permisson that is 777 then magento will not be able to generate any cache. So change Var folder permission to 777 , you will see the effect.

Thứ Hai, 31 tháng 10, 2011

How To Display Best Selling Products On Magento Store

To promote best selling products on your site Magento use so many way to show bestseller product, One of them I am going to show. Below code will fetch the all the list of all product which have already sell.

<?php

$_productCollection = Mage::getResourceModel('reports/product_collection')

->addOrderedQty()

->setOrder('ordered_qty', 'desc');

?>



This is the collection of all the product which already have sell.

Now use foreach loop to show some number of product and it details. I wish to show only the name of the product which has sell most. And I will show on the left sidebar of the page.

<div class="block best-seller">

<div class="right-title-bg"><h3>Best Selling Product</h3></div>

<div class="block-content">

<ul class="best-selling-item">

$count = 0;

foreach($_productCollection as $product):

$id = $product->getId();

$prod = Mage::getModel('catalog/product')->load($id);?>

<li><a href="<?php echo $prod->getProductUrl();?>" title="<?php echo $prod->getName();?>"><?php echo $prod->getName();?></a></li>

<?php

$count++ ;

if($count==$totalPerPage)

break;

endforeach;

?>

</ul>

</div>

</div>



If you wish to show all the details of the product then you also can do that as you will get the product Id from $prod->getId(); and from product Id you can fetch each and every details of the product