4/28/2011

How to upload file in magento

If you are making your custom module in magento and if you unable to use default magento file upload then you can use magento custom file uploading code to upload file.

// If you want to upload files under media folder
$absolute_path = Mage::getBaseDir('media') . DS ;
$relative_path = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
// File Upload
$files = $_FILES['file_upload']['name'];
if(isset($files) && $files != '')
{
try
{
if(file_exists($absolute_path.DS.$files))
{
$var = rand(0,99);
$files = $var.'_'.$files;
}
// Starting upload
$uploader = new Varien_File_Uploader('file_upload');
// Any extention would work
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
$uploader->setAllowRenameFiles(false);
//false -> get the file directly in the specified folder
//true -> get the file in the product like folders /media/catalog/product/file.gif
$uploader->setFilesDispersion(false);
//We set media as the upload dir
$uploader->save($absolute_path, $files);
}
catch(Exception $e)
{
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
}
// Your uploaded file Url will be
echo $file_url = $relative_path.$files;
}

How to fetch all associative product of a configurable product in magento

Configurable product is the product which is created by some Associated product, If you have an configurable product and you want to get list of all associated product and it's attribute then just write the below

You must have your configurable product Id, My configurable product Id is 11 $id=11; //Use your Configurable product Id
$_product = new Mage_Catalog_Model_Product();
$childIds = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($id);
foreach($childIds[0] as $key=>$val)
{
   $associatedProduct = Mage::getModel('catalog/product') ->load($val);
   echo $associatedProduct ->getName();
//Let you have a color attribute in each associated product then you can fetch by
   $colorids[] = $associatedProduct ->getColor();
   $colorname[] = $associatedProduct ->getAttributeText('color');
}

How to and where to change default product image in Magento

If you forgot to upload any product image then Magento always show a default image,If you think to change default image then login to admin panel then goto System -> Configuration then click on Catalog tab. From Product Image Placeholders change your Base Image , Small Image and Thumbnail . Now click on Save Config button to make changes.


4/20/2011

How to Add option to select box by jquery

I have write the below code to add option to select drop down by jquery. Add value to the below input box then click on add button.You can see the effect.That option will be added to the below selectbox

The code you need to write is

$('#selectbox').append($("<option></option>").attr("value",option_value).text(text_to_display));

4/01/2011

How to add new order status in magento admin panel

To add New Status in magento admin panel, you need to write your own custom module , then in your config.xml file write the below code to add new Status

<config>
<global>
<sales>
<order>
<statuses>
<custom_status translate="label"><label><![CDATA[Custom Status]]></label></custom_status>
</statuses>
<states>
<new><!-- order state where you want to assign your custom status e.g:- New, Onhold, Canceld etc-->
<statuses>
<custom_status/>
</statuses>
</new>
</states>
</order>
</sales>
</global>
</config>

It will work upto 1.4.2 , But in Magento 1.5.0 or above there is Special Option to do this. To make own Custom status above magento 1.5 version go to System->Order Statuses. Then Click on Create New Status. After creating new status click on Assign Status to State to assign that status