Thursday, 14 February 2013

How to create csv file using magento

Creating csv files in magento is very easy and flexiable

         $fileName = "example.csv";
         $file_path = Mage::getBaseDir('media').DS.$fileName; // path of the file

         $mage_csv = new Varien_Convert_Parser_Csv();

          try {
         $mage_csv->saveData($file_path, $rows); //note $rows will be two dimensional array
         $mage_csv->setData($rows);
         $result = $mage_csv->unparse();
      
              } catch (Exception $e) {
         echo $e->getMessage();exit;
         }

         header("Content-type: application/csv");
         header("Content-Disposition: attachment; filename=$fileName");
         header("Pragma: no-cache");
         header("Expires: 0");
         echo $result->getData();
         exit;

Example
      
     $fileName = "example.csv";
         $file_path = Mage::getBaseDir('media').DS.$fileName; // path of the file

         $mage_csv = new Varien_Convert_Parser_Csv();
         $rows = array();
        $_data['n'] = 'name';
         $_data['a'] = 'address';
         $_data['g'] = 'gender';
         $rows[]=$_data;

           $data['n'] = 'Ram';
         $data['a'] = 'Hyd';
         $data['g'] = 'Male';
        $rows []=$data;
          try {
         $mage_csv->saveData($file_path, $rows); //note $rows will be two dimensional array
         $mage_csv->setData($rows);
         $result = $mage_csv->unparse();
      
              } catch (Exception $e) {
         echo $e->getMessage();exit;
         }

         header("Content-type: application/csv");
         header("Content-Disposition: attachment; filename=$fileName");
         header("Pragma: no-cache");
         header("Expires: 0");
         echo $result->getData();
         exit;

Wednesday, 13 February 2013

How to remove currency symbol from price?

We can remove symbol from price admin

System > Manage Currency > Symbols

Monday, 11 February 2013

To create a order programmatically in magento


Below is the php code to create an order in magento. It requires a valid customer account with shipping and billing address setup.
$id=1; // get Customer Id
$customer = Mage::getModel('customer/customer')->load($id);
$transaction = Mage::getModel('core/resource_transaction');
$storeId = $customer->getStoreId();
$reservedOrderId = Mage::getSingleton('eav/config')->getEntityType('order')->fetchNewIncrementId($storeId);
$order = Mage::getModel('sales/order')
->setIncrementId($reservedOrderId)
->setStoreId($storeId)
->setQuoteId(0)
->setGlobal_currency_code('USD')
->setBase_currency_code('USD')
->setStore_currency_code('USD')
->setOrder_currency_code('USD');
//Set your store currency USD or any other
// set Customer data
$order->setCustomer_email($customer->getEmail())
->setCustomerFirstname($customer->getFirstname())
->setCustomerLastname($customer->getLastname())
->setCustomerGroupId($customer->getGroupId())
->setCustomer_is_guest(0)
->setCustomer($customer);
// set Billing Address
$billing = $customer->getDefaultBillingAddress();
$billingAddress = Mage::getModel('sales/order_address')
->setStoreId($storeId)
->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_BILLING)
->setCustomerId($customer->getId())
->setCustomerAddressId($customer->getDefaultBilling())
->setCustomer_address_id($billing->getEntityId())
->setPrefix($billing->getPrefix())
->setFirstname($billing->getFirstname())
->setMiddlename($billing->getMiddlename())
->setLastname($billing->getLastname())
->setSuffix($billing->getSuffix())
->setCompany($billing->getCompany())
->setStreet($billing->getStreet())
->setCity($billing->getCity())
->setCountry_id($billing->getCountryId())
->setRegion($billing->getRegion())
->setRegion_id($billing->getRegionId())
->setPostcode($billing->getPostcode())
->setTelephone($billing->getTelephone())
->setFax($billing->getFax());
$order->setBillingAddress($billingAddress);
$shipping = $customer->getDefaultShippingAddress();
$shippingAddress = Mage::getModel('sales/order_address')
->setStoreId($storeId)
->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING)
->setCustomerId($customer->getId())
->setCustomerAddressId($customer->getDefaultShipping())
->setCustomer_address_id($shipping->getEntityId())
->setPrefix($shipping->getPrefix())
->setFirstname($shipping->getFirstname())
->setMiddlename($shipping->getMiddlename())
->setLastname($shipping->getLastname())
->setSuffix($shipping->getSuffix())
->setCompany($shipping->getCompany())
->setStreet($shipping->getStreet())
->setCity($shipping->getCity())
->setCountry_id($shipping->getCountryId())
->setRegion($shipping->getRegion())
->setRegion_id($shipping->getRegionId())
->setPostcode($shipping->getPostcode())
->setTelephone($shipping->getTelephone())
->setFax($shipping->getFax());
$order->setShippingAddress($shippingAddress)
->setShipping_method('flatrate_flatrate');
/*->setShippingDescription($this->getCarrierName('flatrate'));*/
/*some error i am getting here need to solve further*/
//you can set your payment method name here as per your need
$orderPayment = Mage::getModel('sales/order_payment')
->setStoreId($storeId)
->setCustomerPaymentId(0)
->setMethod('purchaseorder')
->setPo_number(' – ');
$order->setPayment($orderPayment);
// let say, we have 1 product
//check that your products exists
//need to add code for configurable products if any
$subTotal = 0;
$products = array(
    '1' => array(
    'qty' => 2
    )
);
foreach ($products as $productId=>$product) {
$_product = Mage::getModel('catalog/product')->load($productId);
$rowTotal = $_product->getPrice() * $product['qty'];
$orderItem = Mage::getModel('sales/order_item')
->setStoreId($storeId)
->setQuoteItemId(0)
->setQuoteParentItemId(NULL)
->setProductId($productId)
->setProductType($_product->getTypeId())
->setQtyBackordered(NULL)
->setTotalQtyOrdered($product['rqty'])
->setQtyOrdered($product['qty'])
->setName($_product->getName())
->setSku($_product->getSku())
->setPrice($_product->getPrice())
->setBasePrice($_product->getPrice())
->setOriginalPrice($_product->getPrice())
->setRowTotal($rowTotal)
->setBaseRowTotal($rowTotal);
$subTotal += $rowTotal;
$order->addItem($orderItem);
}
$order->setSubtotal($subTotal)
->setBaseSubtotal($subTotal)
->setGrandTotal($subTotal)
->setBaseGrandTotal($subTotal);
$transaction->addObject($order);
$transaction->addCommitCallback(array($order, 'place'));
$transaction->addCommitCallback(array($order, 'save'));
$transaction->save();

How to call custom blocks that are created already in phtml in magento?


<?php
echo$this->getLayout()->createBlock('catalog/product_view_attributes')->setTemplate('catalog/product/view/attributes.phtml')->toHtml();
?>

How to upgrade magento 1.4 1.1 to 1.7.0.2

How to upgrade magento 1.4 1.1 to 1.7.0.2 1. Change permissions on lib/pear folder to writable (recursively): command: chmod -R 777 lib/PEAR 2.Execute this command to prepare Magento for upgrade: command: ./pear mage-setup Most likely you will see this result after command execution: Channel “connect.magentocommerce.com/core” is already initialized Upgrade from Magento 1.4.1.x to Magento 1.4.2.0 This step is necessary even if you upgrade your Magento store to 1.5 or 1.6 versions.

3.Execute Magento upgrade command: command: ./pear upgrade -f magento-core/Mage_All_Latest-stable Output of the command will show you what core packages were upgraded: channel://connect.magentocommerce.com/core/Mage_Centinel-1.4.2.0 upgrade ok: channel://connect.magentocommerce.com/core/Interface_Frontend_Base_Default-1.4.2.0 upgrade ok: channel://connect.magentocommerce.com/core/Phoenix_Moneybookers-1.2.3 upgrade ok: channel://connect.magentocommerce.com/core/Find_Feed-1.0.7 upgrade ok: channel://connect.magentocommerce.com/core/Interface_Adminhtml_Default-1.4.2.0 4.When this part of the upgrade will be complete, enter these commands: command: .chmod 550 ./mage command: ./mage mage-setup . Running initial setup...

Success

Successfully added: http://connect20.magentocommerce.com/community

It means that Magento connect 2.0 channel was added to the channels list successfully. 5.Next, enter this command: command: ./mage sync The result will be: Successfully added: community/Lib_Js_Mage-1.4.2.0

Successfully added: community/Lib_Js_Calendar-1.51.1

Successfully added: community/Mage_Downloader-1.5.0.0

Successfully added: community/Lib_Js_TinyMCE-3.3.7.0

Successfully added: community/Lib_Js_Ext-1.4.2.0

Successfully added: community/Lib_Google_Checkout-1.4.2.0

Successfully added: community/Interface_Install_Default-1.4.2.0

Successfully added: community/Lib_Js_Prototype-1.6.0.3.3

Successfully added: community/Find_Feed-1.0.7

Successfully added: community/Lib_LinLibertineFont-2.8.14.0

Successfully added: community/Lib_Varien-1.4.2.0

Successfully added: community/Mage_Centinel-1.4.2.0

Successfully added: community/Lib_Phpseclib-1.4.2.0

Successfully added: community/Mage_All_Latest-1.4.2.1

Successfully added: community/Mage_Locale_en_US-1.4.2.0

Successfully added: community/Lib_PEAR-1.4.0.0

Successfully added: community/Lib_Mage-1.4.2.0

Successfully added: community/Mage_Pear_Helpers-1.0.18800


6.Upgrade from Magento 1.4.2.0 to Magento 1.5.10 or Magento 1.6.0.0 Now you can upgrade your store to version 1.5 or 1.6. Before proceeding with this part of Magento upgrade, it is very important to see to what version Magento upgrade scripts will upgrade your store. Enter this command to check this:

command: ./mage list-upgrades Updates for community:

Lib_Js_Mage: 1.4.2.0 => 1.7.0.1
Lib_Js_Calendar: 1.51.1 => 1.51.1.1
Mage_Downloader: 1.5.0.0 => 1.7.0.1
Lib_Js_TinyMCE: 3.3.7.0 => 3.4.7.0
Lib_Js_Ext: 1.4.2.0 => 1.7.0.0
Lib_Google_Checkout: 1.4.2.0 => 1.5.0.0
Interface_Install_Default: 1.4.2.0 => 1.7.0.0
Lib_Js_Prototype: 1.6.0.3.3 => 1.7.0.0.3
Find_Feed: 1.0.7 => 1.1.1
Lib_LinLibertineFont: 2.8.14.0 => 2.8.14.1
Lib_Varien: 1.4.2.0 => 1.7.0.0
Mage_Centinel: 1.4.2.0 => 1.7.0.0

Lib_Phpseclib: 1.4.2.0 => 1.5.0.0

Mage_All_Latest: 1.4.2.1 => 1.7.0.2

Mage_Locale_en_US: 1.4.2.0 => 1.7.0.1

Lib_Mage: 1.4.2.0 => 1.7.0.1
7.Enter this command to change the upgrade channel to stable: command: ./mage config-set preferred_state stable Result : Success 8.After channel selection you can upgrade your Magento to 1.5.1.0 (or to Magento 1.6.0.0) using this command: command: ./mage upgrade-all --force 9.Clear the Cache and give permissions to magento folders. 10.Replace old .htaccess file insted of new file.



Important notes: 1.When upgrade magento version 1.4.1.1 to 1.7.0.2 that time DB issues occured: Tables List: a.wishlist_item b.Catalog_Category_Flat_Store_30 c.tax_order_aggregated_updated d.sales_order_tax_item Solution: First of all take backup of above tables, After delete those tables issue is resolved. For alternative backup of above data re-import the backup tables. (This tables are not mandatory, just keeping it backup) NOTE: if create any table issue rise in upgrade time take bakup table and delete it. 2.“Integrity constraint violation: 1062 Duplicate entry “
Solution:
This error can be solved by disabling the unique indexes check.
In the app/etc/config.xml, change this

SET NAMES utf8 to this SET NAMES utf8; SET FOREIGN_KEY_CHECKS=0; SET UNIQUE_CHECKS=0;

3.Error: Fatal error: Call to a member function getAllOptions() on a non-object in /var/www/html/magento/app/code/core/Mage/Core/Model/Cache.php on line 455
Solution: Uncomment line 198 in app/mage.php:

//self::$_appRoot = null;

which is called by app/code/core/Mage/Core/Model/Config/Options.php at line 54:

$appRoot= Mage::getRoot();
4.Admin Side not disply
Solution:
app/code/core/Mage/Admin/Model/Session.php (Commented Line No:93)

How to setup Magento ecommerce on Cpanel Server without SSH access(Fast way)

Here is source

Original Post:
http://www.mtomto.com/13/guide-how-to-setup-magento-ecommerce-on-cpanel-server-fast-way/
I would advise to check the original guide posted on magentohub as we always update the guide from time to time when necessary with new updates/changes.
========================================================================
First of all, make sure your webhost meet the [url=http://www.mtomto.com/8/magento-installation-requirement/[/url] before you install magento.

Two reasons why you should use this guide:

1. Your account do not come with SSH access and upload files via FTP is taking so long( it took me > 20minutes just to upload entire magento files via FTP)
2. OR you do not know how to install magento using SSH.
This guide is for you.
----------------------------------------------------------------------------------------
Here is a quick way to upload all magento files using cpanel file manager:
Upload magento
1. Upload your magento-x.x.x.zip file to public_html/ via FTP. This gonna take sometime since the zip file alone is about 15mb big.
2. Upload finish.
Login to Cpanel > File manager
3. Navigate to the directory where you have just upload the zip file.
4. Right click the zip file and choose “Extract” . It should extract a “magento” folder.
5. Enter into “magento” folder , “Select all” . Right click on one of the selected file. Choose “move”
Move all files outside of “magento” folder.
6. Delete “magento” folder and the zip file.
Start installation
1. Create a mysql database & mysql user.
2. Import the sample data to your mysql database. (You can do so at Cpanel > mysql > phpmyadmin)
(You can get the .sql sample data from http://www.magentocommerce.com/download/noregister )
3. Start the installation wizard
http://www.yourdomain.com/install.php
Note: My site is hosted on a suexec/suphp environment. Therefore, i do not have chmod 777 to make certain directories writable. For other who are no on suexec/suphp ennvironment, you may require to make the file magento/var/.htaccess, the directories magento/app/etc, magento/var, and all the directories under magento/media are writable by the web server.