Monday 25 March 2013

upload file in magento


           if($_FILES['productimage']['name'] != ''){    
                $uploader = new Varien_File_Uploader('productimage');
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
$uploader->setAllowRenameFiles(false);
$uploader->setFilesDispersion(false);
                $productimage = $_FILES['productimage']['name'];
$path = Mage::getBaseDir() . DS . 'directoryname1' . DS . 'directoryname2' . DS;
                $uploader->save($path, $productimage);
          }

add field in custom module magento admin

go to   app\code\local\package\module\block\adminhtml\module\edit\tab

open file form.php  add custom field


                                                $fieldset->addField("lastname", "text", array(
"label" => Mage::helper("storeowner")->__("Last Name"),
"name" => "lastname",
));


$fieldset->addField("email", "text", array(
"label" => Mage::helper("storeowner")->__("email"),
"name" => "email",
));


in prepareForm function
here are  2 field has been added in this example

Saturday 23 March 2013

magento order get shipping cost


$orderid = 3;
$order = Mage::getModel('sales/order')->load($orderid);

$order->getShippingAmount();

magento order get shipping

$orderid = 3;
$order = Mage::getModel('sales/order')->load($orderid);
$order->getShippingDescription();

get order payment method magento

$payment_title = $order->getPayment()->getMethodInstance()->getTitle();

get customer billing address magento


$customerid = 1;
$visitorData = Mage::getModel('customer/customer')->load($customerid);
$billingaddress = Mage::getModel('customer/address')->load($visitorData->default_billing);
$addressdata = $billingaddress ->getData();
$addressdata['street'];
$addressdata['city'];
$addressdata['postcode'];
$addressdata['region'];
$addressdata['telephone'];

get customer group name magento


$customerid = 1;
$visitorData = Mage::getModel('customer/customer')->load($customerid);
$group = Mage::getModel('customer/group')->load($visitorData->getGroup_id());
$custgroup = $group->customer_group_code;

get customer group name magento


$customerid = 1;
$visitorData = Mage::getModel('customer/customer')->load($customerid);
$group = Mage::getModel('customer/group')->load($visitorData->getGroup_id());
$custgroup = $group->customer_group_code;

change date format in magento


$invoiceid = 3;
$invoice = Mage::getModel("sales/order_invoice")->load($invoiceid);
$orderid = $orderdata['increment_id'];
$time = explode(" ",$invoice['created_at']);
$datearr = explode("-",$time[0]);
$month = strpos($datearr[1], "0");
$mnthnub = ($month == 0) ? substr($datearr[1],1):$datearr[1];
$monthName = date("F", mktime(0, 0, 0, $mnthnub, 10));
$etime = date("g:i a", strtotime($time[1]));
$fineldate = $datearr[2].", ".$monthName.", ".$datearr[0].", ".$etime;
$orderid = $orderdata['increment_id'];
$time = explode(" ",$invoice['created_at']);
$datearr = explode("-",$time[0]);
$month = strpos($datearr[1], "0");
$mnthnub = ($month == 0) ? substr($datearr[1],1):$datearr[1];
$monthName = date("F", mktime(0, 0, 0, $mnthnub, 10));
$etime = date("g:i a", strtotime($time[1]));
$fineldate = $datearr[2].", ".$monthName.", ".$datearr[0].", ".$etime;

magento order item collection



$orderid = 3;
$order = Mage::getModel('sales/order')->load($orderid);

$order_item_collection = $order->getItemsCollection();;
foreach($order_item_collection as $item){
$item->getName();
$item->getSku();
..
..
}

magento get current currency code


 $storeid = 1;
 $currencyCode = Mage::app()->getStore($storeId)->getCurrentCurrencyCode(); /* Currncy Code*/

magento get current currency symbol



 $storeid = 1;
 $currencyCode = Mage::app()->getStore($storeId)->getCurrentCurrencyCode(); /* Currncy Code*/
$currencySymbol = Mage::app()->getLocale()->currency($currencyCode)->getSymbol(); /* Currency Symbol */

magento generate invoice order



$orderId = 2;
$order = Mage::getModel('sales/order')->load($orderId);

$invoice->register();
$invoice->getOrder()->setCustomerNoteNotify(false);
$invoice->getOrder()->setIsInProcess(true);
$order->addStatusHistoryComment('invoice by Aditya.', false);
$transactionSave = Mage::getModel('core/resource_transaction')
                    ->addObject($invoice)
                    ->addObject($invoice->getOrder());
                $transactionSave->save();

magento ship order



$orderId = 2;
$order = Mage::getModel('sales/order')->load($orderId);

$shipment = $order->prepareShipment();
 $shipment->register();
 $order->setIsInProcess(true);
 $order->addStatusHistoryComment('ship by aditya.', false);
 $transactionSave = Mage::getModel('core/resource_transaction')->addObject($shipment)      
  ->addObject($shipment->getOrder())
   ->save();

Saturday 16 March 2013

create and download zip


<?php

$image = $_REQUEST['img'];
$source = $_SERVER['DOCUMENT_ROOT']."/sourcefolder/".$image
$orderid = $_REQUEST['orderid'];

$zip = new ZipArchive();
$DestFilePath=$orderid.".zip";
$destinat  = Mage::getBaseDir().'/zip/'.$DelFilePath;
$destinat = $_SERVER['DOCUMENT_ROOT']."/destination/".$DestFilePath;
if(file_exists($destinat)) {
    unlink ($destinat);
}
if ($zip->open($destinat, ZIPARCHIVE::CREATE) != TRUE) {
        die ("Could not open archive");
}
$zip->addFromString(basename($source), file_get_contents($source));
$zip->close();

/**************************  Download file ********************************/
$file = $orderid.'.zip';
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header( "Content-Disposition: attachment; filename=".basename($file));
header( "Content-Description: File Transfer");
@nl2br(readfile($file));
/**************************  end Download file ********************************/
?>

Sunday 10 March 2013

change customer password magento


<?php
$customer = Mage::getModel('customer/customer')->load($_REQUEST['custid']);
$oldpassword = $_REQUEST['oldpassword']
$passwordhash = $customer['password_hash'];
$phasharray = explode(":",$passwordhash);
$passpostfix = $phasharray[1];
$completeOldPassword = $oldpassword.":".$passpostfix;
if($completeOldPassword==$passwordhash){
$customer->setPassword($_REQUEST['newpass']);
$customer->save();
}
?>

duplicate entry error in saving product magento

you are triying to save product again with same information
$product->save();

save product once when you update same product use
$product->getResource()->save($product);

hope it will be helpfull


Friday 8 March 2013

Cannot complete this operation from non-admin area magento


Mage::register('isSecureArea', true); /* set secure admin area*/
 Mage::getModel('review/review')->load($review)->delete(); /* Your operation */
  Mage::unregister('isSecureArea'); /* un set secure admin area*/

get customer data magento

$id = 3;
$customer = Mage::getModel('customer/customer')->load($id);

$customer->getFirstname();
$customer->getMiddlename();
$customer->getLastname();
$customer->getEmail();


Tuesday 5 March 2013

review collection magento

Category children magento

magento aditya: Magento get Category children id

get Category children id magento


<?php
$cat = Mage::getModel('catalog/category')->load(2);  /*load category*/
 $subcatcollection = $cat->getChildren();
$subcatrgories = explode(",",$subcatcollection);    /* it gives string , convert in array */
?>

get review collection magento

<?php

 $reviewcollection = Mage::getModel('review/review')->getCollection(); /*collection */
foreach($reviewcollection as $reviwe){
   print_r($reviwe);                   /*review*/
}
 ?>

Sunday 3 March 2013

marge category product collection magento


<?php
$merged_ids = array();
$categoryIdArray  =  array(2,6,5);

foreach(array_unique($categoryIdArray) as $categoryid)
{
$collection = Mage::getModel('catalog/category')->load($categoryid)->getProductCollection();
if($collection->getAllIds())
{
$temp = $collection->getAllIds();
for($k=0;$k<count($temp);$k++){
$merged_ids[] = $temp[$k];
}
unset($temp);
}
}


$merged_collection = Mage::getResourceModel('catalog/product_collection')
    ->addFieldToFilter('entity_id', $merged_ids)
    ->addAttributeToSelect('*');



?>

Saturday 2 March 2013

resize product image magento


$productId = 2;
$width = 200;
$height = 200;
$product = Mage::getModel('catalog/product')->load($productId);
$resizeimage = Mage::helper('catalog/image')->init($product, 'image')->resize($width, $height);

category product collection magento

$categoryId = 3;
$categoryProductCollection = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection();

Collection Filter magento

$modulecollection  = Mage::getModel("module/module")->getCollection()-addFilter($attributecode,$value);

add image to product magento

$product = Mage::getModel('catalog/product')->load($productid);
 $frontImage = $imagepath/$frontImagename;

$product->setMediaGallery (array('images'=>array (), 'values'=>array ()));
$product->addImageToMediaGallery($frontImage, array('image','small_image','thumbnail'),false,false);
$product->save();

upload file magento


$_FILES['image']['name'];
$uploader = new Varien_File_Uploader('image');
$uploader->setAllowedExtensions(array('jpg','png','gif'));
$uploader->setAllowRenameFiles(false);
$uploader->setFilesDispersion(false);
$destFile = $path.$_FILES['image']['name'];
$filename = $uploader->getNewFileName($destFile);
$uploader->save($path, $filename);