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/25/2011

How to use or mysql condition in $collection data of magento

Magento provide  addAttributeToFilter to use and operator in $collection, But using of OR operator is little bit different . I was guess that there must be and OrAttributeToFilter , But I was wrong I think magento forgot to write this function, Lastly I found the solution to use mysql OR in magento . In addAttributeToFilter we have to write the condition in the form of array.

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

To upload CSV file and to read that file line by line , Please write the below code 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

8/04/2011

How to uncheck Radio button by jquery








Write the Below code to Unchecked Radio Button by Jquery




<script type="text/javascript">
$(document).ready(function(){
$("#btn").click(function(){
$('input[name="rd"][type="radio"]:checked').each(function(){
$(this).attr("checked", false);
});
});
});
</script>






<form action="" name="aa" method="post">
<input type="radio" name="rd" value="1"/>
<input type="radio" name="rd" value="2"/>
<input type="radio" name="rd" value="3"/>
<input type="button" id="btn" value="Click" />
</form>

How to change value and fetch value of Minimum Order Amounts of free shipping in magento


First of all to make Minimum Order Amounts of Free Shipping to [STORE VIEW] instead of [WEBSITE] then Go to app->code->core->Mage->Shipping->etc->System.xml


then change the value of <show_in_store> to 1 under carriers -> groups -> freeshipping ->free_shipping_subtotal.


Now to fetch the value of the Minimum Order Amount according to store view write the below code



<?php echo Mage::getStoreConfig('carriers/freeshipping/free_shipping_subtotal'); ?>