Tuesday 16 May 2017

How to Add Custom Field to Customer Address in magento



$installer = new Mage_Eav_Model_Entity_Setup('core_setup');

$installer->startSetup();

$installer->addAttribute('customer_address', 'specialdeliveryins', array(
    'type' => 'varchar',
    'input' => 'text',
    'label' => 'Special Delivery Instruction',
    'global' => 1,
    'visible' => 1,
    'required' => 0,
    'user_defined' => 1,
    'visible_on_front' => 1
));
Mage::getSingleton('eav/config')
    ->getAttribute('customer_address', 'specialdeliveryins')
    ->setData('used_in_forms', array('customer_register_address','customer_address_edit','adminhtml_customer_address'))
    ->save();
$installer->endSetup(); 

Thursday 11 May 2017

How to add a custom image field to a category in Magento?

Step:1. At first run this code  
 $installer = new Mage_Eav_Model_Entity_Setup('core_setup');
    $installer->startSetup();
    $attribute  = array(
    'type'          =>  'text',
    'label'         =>  'Banner Image',
    'input'         =>  'image',
    'global'        =>  Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible'       =>  true,
    'required'      =>  false,
    'backend'       => 'catalog/category_attribute_backend_image',
    'user_defined'  =>  true,
    'default'       =>  "",
    'group'         =>  "General Information"
    );
    $installer->addAttribute('catalog_category', 'banner_image', $attribute);
    $installer->endSetup();



Step 2:
Copy Category.php file from app/code/core/Mage/Catalog/Model/Category.php
to
 app/code/local/Mage/Catalog/Model/Category.php

Add this code line 480
 public function getBannerImageUrl()
    {
        $url = false;
        if ($image = $this->getBannerImage()) {
            $url = Mage::getBaseUrl('media').'catalog/category/'.$image;
        }
        return $url;
    }

Friday 5 May 2017

How to add custom property to magento Attributes?

Step 1: At first run this code.
<?php
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();
$installer->getConnection()->addColumn(
    $installer->getTable('catalog/eav_attribute'),
    'comment',
    array(
        'type'      => Varien_Db_Ddl_Table::TYPE_TEXT,
        'nullable'  => true,
        'comment'   => 'Comment'
    )
);
$installer->endSetup(); ?>


Step 2:
Then add this code.
/app/code/core/Mage/Adminhtml/Block/Catalog/Product/Attribute/Edit/Tab/Main.php

       $fieldset->addField('comment', 'textarea', array(
            'name' => 'comment',
            'label' => Mage::helper('catalog')->__('Comment'),
            'title' => Mage::helper('catalog')->__('Comment'),
            'note' => Mage::helper('catalog')->__('Comment'),
           
        ));

Thursday 4 May 2017

How to create a different product page structure for specific products in magento?



<reference name="product.info">
<action method="setTemplate">
<template>catalog/product/view-recipe.phtml</template>
</action>
</reference>
Step 1 :-  Admin => Catalog => Manage Product => Select this specific products => Left design section => Custom Layout Update (Add this above code)

Step 2 :- Then "view-recipe.phtml" upload the file this path
app/design/frontend/rwd/theme name/template/catalog/product/

Wednesday 3 May 2017

programmatically order create in magento

<?php
    error_reporting(0);  
    require_once 'app/Mage.php';
    umask(0);
    Mage::app();  
   
    $row = 1;
    $line_flag = true;
   
    if (($handle = fopen("test.csv", "r")) !== FALSE)
    {
$flag=0;
$arr_row=array();

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
   if($line_flag)
   {
$line_flag = false;
continue;
   }
 
   $current_order_no=$data[1];
 
   $product_name=$data[4];
   $product_sku=$data[5];
   $product_price=$data[9];
 
   $customer_id=$data[12];
 
   $SKU = $product_sku;
   $productid = Mage::getModel('catalog/product')->getIdBySku(trim($SKU));

   $product = Mage::getModel('catalog/product');
   $product ->load($productid);
   $prod=$product->getData();
 
   $product_id = $product->getId();
 
   //if the product is not found in the db code start to add product
 
   if(count($prod)<=5)
   {
//echo "not ext";

$data=array("weight"=>"10","sku"=>$SKU,"name"=>$product_name,"description"=>"description","sdesc"=>"sdesc","price"=>$product_price,"cid"=>"2");

$newproduct = new Mage_Catalog_Model_Product();
$newproduct->setTypeId('simple');
$newproduct->setWeight($data['weight']);
$newproduct->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
$newproduct->setStatus(1);
$newproduct->setSku($data['sku']);
$newproduct->setTaxClassId(0);
$newproduct->setCategoryIds($data['cid']);

$newproduct->setWebsiteIDs(array(1));
$newproduct->setStoreIDs(array(1));
$newproduct->setStockData(array(
'is_in_stock' => 1,
'qty' => 199,
'manage_stock' => 1
));
$newproduct->setAttributeSetId(4);
$newproduct->setName($data['name']);
//$newproduct->setCategoryIds(array(3)); // array of categories it will relate to
$newproduct->setDescription($data['description']);
$newproduct->setShortDescription($data['sdesc']);
$newproduct->setPrice($data['price']);
$newproduct->save();

$product_id = $newproduct->getId();
   }
 
   $customer = Mage::getModel('customer/customer')->load($customer_id); // Customer Id  
   $customer_details=$customer->getData();
     
   $customerAddress = array();
   #loop to create the array
   foreach ($customer->getAddresses() as $address)
   {
      $customerAddress[] = $address->toArray();
   }
   #displaying the array
   //echo '<pre/>';print_r($customerAddress );exit;
 
 
   $firstname=$customer_details['firstname'];
   $lastname=$customer_details['lastname'];
   $company=$customer_details['lastname'];
   $email=$customer_details['email'];
 
   $street=$customerAddress[0]['street'];
   $city=$customerAddress[0]['city'];
   $region_id=$customerAddress[0]['region_id'];
   $region=$customerAddress[0]['region'];
   $postcode=$customerAddress[0]['postcode'];
   $country_id=$customerAddress[0]['country_id'];
   $telephone=$customerAddress[0]['telephone'];
   $fax=$customerAddress[0]['fax'];
 
   $customer_password='';
   $confirm_password='';
 
   $save_in_address_book='0';
   $use_for_shipping='1';
 
 
   $productId=$product_id;
   $store=Mage::app()->getStore();
 
   // Start New Sales Order Quote
   $quote=Mage::getModel('sales/quote')->setStoreId($store->getId());

 
   //Customer Billing Address Data

   $billingAddress = array(
'firstname' => $firstname,
'lastname' => $lastname,
'company' => 'Test',
'email' =>  $email,
'street' => $street,
'city' => $city,
'region_id' => $region_id,
'region' => $region,
'postcode' => $postcode,
'country_id' => $country_id,
'telephone' =>  $telephone,
'fax' => $fax,
'customer_password' => '',
'confirm_password' =>  '',
'save_in_address_book' => '0',
'use_for_shipping' => '1',
   );
 
 
    //Customer Shipping Address Data
       
   $shippingAddress = array(
'firstname' => $firstname,
'lastname' => $lastname,
'street' => $street,
'city' => $city,
'region' => $region,
'region_id' => $region_id,
'postcode' => $postcode,
'country_id' => $country_id,
'telephone' => $telephone,
   );
     
 
   //$quote      = Mage::getModel('sales/quote')->setStoreId($store->getId());
 
   $quote=Mage::getModel('sales/quote')->setStoreId(1);
 
 
   
   //Load Product and add to cart
   $product_cart=Mage::getModel('catalog/product')->load($productId);  
   $buyInfo=array('qty' => 1);  
   $quote->addProduct($product_cart, new Varien_Object($buyInfo));


   // Add Billing Address
   $quote->getBillingAddress()
   ->addData($billingAddress);

 
   //Add Shipping Address and set shipping method  
   $quote->getShippingAddress()
   ->addData($shippingAddress)      
   ->setCollectShippingRates(true)
   ->setShippingMethod('freeshipping_freeshipping')
   ->setCouponCode('test')
   ->collectTotals();
       $quote->setCouponCode('test');
   $quote->collectTotals()->save();

   /*

   //Set Customer group As Guest    
   $quote->setCheckoutMethod('guest')
->setCustomerId(null)
->setCustomerEmail($quote->getBillingAddress()->getEmail())
->setCustomerIsGuest(true)
->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID);
*/
 
$quote->setCustomerId($customer_id)
        ->setCustomerEmail($email)
        ->setCustomerIsGuest(false)
        ->setCustomerGroupId(1);


   //Set Payment method
 
   $quote->getPayment()->importData(array('method' => 'cashondelivery'));
   $quote->collectTotals()->save();


   //Save Order With All details




   $service = Mage::getModel('sales/service_quote', $quote);
   $service->submitAll();
   $increment_id = $service->getOrder()->getIncrementId();
 
 
 
   Mage::app()->getStore()->setConfig(Mage_Sales_Model_Order::XML_PATH_EMAIL_ENABLED, "1");
 
   //Send Order Mail
 
   $order_mail = new Mage_Sales_Model_Order();
   $order_mail->loadByIncrementId($increment_id);
   $order_mail->sendNewOrderEmail();
 
   //echo $increment_id;

   /*  For Invoice start */


   $order=Mage::getModel("sales/order")->loadByIncrementId($increment_id);
   //Mage::getModel("sales/order")->loadByIncrementId($incrementId);


   $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
   $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
   $invoice->register();
   $transaction = Mage::getModel('core/resource_transaction')
    ->addObject($invoice)
   ->addObject($invoice->getOrder());
   $transaction->save();
   if($order->canShip()):
$itemQty =  $order->getItemsCollection()->count();
$shipment = Mage::getModel('sales/service_order', $order)->prepareShipment($itemQty);
$shipment = new Mage_Sales_Model_Order_Shipment_Api();
$shipmentId = $shipment->create($increment_id);
endif;
   $order->setData('state',"complete");
   $order->setStatus("complete");
 
   //$history = $order->addStatusHistoryComment('Order marked as complete automatically.',false);
 
   $history = $order->addStatusHistoryComment('Order Number: '.$current_order_no);
 
 
   $history->setIsCustomerNotified(false);
   $order->save();
   $id=$order->getId();


/* --------  End --------------------------------------------------- */
 
 
}

fclose($handle);

    }
    else
    {
echo "no file found";
    }
?>