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";
Magento Tutorial, Magneto modules, Magento Banckend, Magento admin, Magento Frontend, Magento Interview Question, Jquery, CSS, Javascript, PHP, XHTML
3/28/2012
How to refresh magento cache using magento php code
12/28/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
}
}?>
11/25/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.
11/17/2011
cache is not generating in magento
10/31/2011
How To Display Best Selling Products On Magento Store
<?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
10/29/2011
How to print product collection query in magento
Let you have write a product collection with some attribute to select some product, but you are not getting proper result then you must need to print the sql query to see whether you have write the correct syntax in magento default collection. then write the below code to get the sql query .
<?php
$productcollection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('bestseller', array('eq' => 1))
->addAttributeToSelect('*');
echo $productcollection->getSelect()->assemble();
?>
9/27/2011
How to delete all sales order in magento
In Magento Connect you will find some extension which will delete all cancel order and Pending Order, But it never delete all processed order. To delete all orders following code snippet can be used.
<?php
require 'app/Mage.php';
Mage::app('admin')->setUseSessionInUrl(false);
$sales_orders = Mage::getModel('sales/order')->getCollection()->getData();
foreach($sales_orders as $sales_order){
$id = 0;
$id = $sales_order['increment_id'];
try{
Mage::getModel('sales/order')->loadByIncrementId($id)->delete();
echo "order #".$id." is removed".PHP_EOL;
}catch(Exception $e){
echo "order #".$id." could not be remvoved: ".$e->getMessage().PHP_EOL;
}
}
echo "complete."
?>
9/26/2011
How to use or mysql condition in $collection data of magento
protected function _prepareCollection()
{
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter(
array(
array('attribute'=>'category_ids','finset'=>2),
array('attribute'=>'category_ids','finset'=>3)
)
);
}
use your own attribute name and value to get the data
9/16/2011
How to upload and read csv file in magento
if(isset($_FILES['import_file']['name']) && $_FILES['import_file']['name'] != '')
{
$uploaderFile = new Varien_File_Uploader('import_file');
$uploaderFile->setAllowedExtensions(array());
$uploaderFile->setAllowRenameFiles(false);
$uploaderFile->setFilesDispersion(false);
$uploaderFilepath = Mage::getBaseDir('media') . DS . 'importcsv' . DS ;
$uploaderFile->save($uploaderFilepath, $_FILES['import_file']['name'] );
$file = $_FILES['import_file']['name'];
$filepath = $uploaderFilepath.$file;
$i = 0;
if(($handle = fopen("$filepath", "r")) !== FALSE) {
while(($data = fgetcsv($handle, 1000, ",")) !== FALSE){
if($i>0 && count($data)>1){
updateData($data);
}
$i++;
}
}
else{
Mage::getSingleton('adminhtml/session')->addError("There is some Error");
$this->_redirect('*/*/index');
}
}
function updateData($data)
{
//Write your code here and Update it to magento tables
}
8/30/2011
How to get contact us email address in magento
Here’s the code to get Contacts email:
<?php echo Mage::getStoreConfig('contacts/email/recipient_email');?>
Or you can change it from admin -> System-> Configuration. From the left tab select Contacts then On the right side you will see Email Options Tab. From that tab you can see there is a field for send Emails To.Change th evalue to your require email address