I am wondering, is it possible for me to automatically create/export a csv file for each order that is placed on magento (1.7 or 1.9)
I would need them to contain all the required info for dispatching an order, products, selected attributes, customer details etc. And i would need the files named in a format similar to 'order_number_timestamp.csv'
Perhaps it is achievable with some php and a cron script?
Thanks
Leeboyce,you can do this using Magento Event Observer functionality
.
When Trigger an event which will be run function ,which will fire a function on Order place
Create an extension will send mail...with csv
step: create config.xml under app/code/local/Amit/Sendorderdata/etc/
and
Code of config.xml are
<?xml version="1.0" encoding="utf-8"?>
<config>
<modules>
<Amit_Sendorderdata>
<version>0.1.4</version>
</Bh_Sendorderdata>
</modules>
<global>
<models>
<sendorderdata>
<class>Amit_Sendorderdata_Model</class>
</sendorderdata>
</models>
</global>
<frontend>
<events>
<checkout_submit_all_after>
<observers>
<sendorderdata_observer_checkout_type_onepage_save_order_after>
<type>singleton</type>
<class>sendorderdata/observer</class>
<method>checkoutTypeOnepageSaveOrderAfter</method>
</sendorderdata_observer_checkout_type_onepage_save_order_after>
</observers>
</checkout_submit_all_after>
</events>
</frontend>
</config>
Step2: Create Observer.php app/code/local/Amit/Sendorderdata/Model/
In this file have a function checkoutTypeOnepageSaveOrderAfter which send mail
...
<?php
class Amit_Sendorderdata_Model_Observer
{
public function checkoutTypeOnepageSaveOrderAfter($observer)
{
$order = $observer->getEvent()->getOrder();
if (!$order) {
$orders = $observer->getEvent()->getOrders();
$order = array_shift($orders);
}
/* Here you will write yours code for create $order will give all detail of order */
}
How to create csv in magento here
Know more about Event and observer
http://inchoo.net/category/ecommerce/magento/events-observers/
http://www.pierrefay.com/event-observers-magento-tutorial-howto-105
http://www.nicksays.co.uk/magento-events-cheat-sheet-1-7/
You can do this using Magento Event Observer functionality as stated below:
Step1: go to config.xml under app/code/local/Allin/CustomModule/etc/ and add the following code in config.xml
config.xml
......
<frontend>
<events>
<sales_order_save_after>
<observers>
<generate_csv_save_order_after>
<type>singleton</type>
<class>custommodule/observer</class>
<method>customCSV</method>
</generate_csv_save_order_after>
</observers>
</sales_order_save_after>
</events>
</frontend>
Step2: Create Observer.php at app/code/local/Allin/CustomModule/Model/Observer.php
<?php
class Allin_CustomModule_Model_Observer
{
public function customCSV($observer)
{
$id = $observer->getEvent()->getOrder()->getId();
$order = Mage::getModel('sales/order')->load($id);
$orderIncrementId = $order->getIncrementId();
$billingAddress = $order->getBillingAddress()->getData();
$billingAdd=array(
'Billing Name' => $billingAddress['firstname']."
".$billingAddress['lastname'],
'Billing Company' => $billingAddress['company'],
'Billing Street' => $billingAddress['street'],
'Billing Zip' => $billingAddress['postcode'],
'Billing City' => $billingAddress['city'],
'Billing State' => $billingAddress['region'],
'Billing Country' => $billingAddress['country_id'],
'Billing Phone' => $billingAddress['telephone'],
);
$orderData = Mage::getResourceModel('sales/order_collection')
->addFieldToSelect(array('entity_id'=>'entity_id',
'Order Number'=>'increment_id',
'Order Date'=>'created_at',
'Order Status'=>'status',
'Order Purchased From'=>'store_name',
'Order Shipping Method'=>'shipping_method',
'Order Subtotal'=>'subtotal',
'Order Tax'=>'tax_amount',
'Order Shipping'=>'shipping_amount',
'Order Discount'=>'discount_amount',
'Order Grand Total'=>'grand_total',
'Order Base Grand Total'=>'base_grand_total',
'Order Paid'=>'total_paid',
'Order Refunded'=>'total_refunded',
'Order Due'=>'total_due',
'Total Qty Items Ordered'=>'total_qty_ordered',
'Customer Email'=>'customer_email',
'Stock Health Customer ID'=>'customer_id',
'Stock Health Customer ID'=>'customer_id',
))->addExpressionFieldToSelect(
'Customer Name',
'CONCAT({{customer_firstname}}, \' \',
{{customer_lastname}})',
array('customer_firstname' =>
'main_table.customer_firstname', 'customer_lastname' =>
'main_table.customer_lastname')
)->addExpressionFieldToSelect(
'Shipping Name',
'CONCAT({{firstname}}, \' \', {{lastname}})',
array('firstname' => 'sales_flat_order_address.firstname',
'lastname' => 'sales_flat_order_address.lastname')
)
->join(array('sales_flat_order_address' => 'sales/order_address'),
"main_table.entity_id = sales_flat_order_address.parent_id AND
sales_flat_order_address.address_type != 'billing'
AND main_table.increment_id='$orderIncrementId' ",
array(
'Shipping Company' => 'company',
'Shipping Street' => 'street',
'Shipping Zip' => 'postcode',
'Shipping City' => 'city',
'Shipping State' => 'region',
'Shipping Country' => 'country_id',
'Shipping Phone Number' => 'telephone',
))->join(array('sales_flat_order_payment' =>
'sales/order_payment'), 'main_table.entity_id =
sales_flat_order_payment.parent_id',
array(
'Order Payment Method' => 'method',
'Credit Card Type' => 'cc_type'
))->join(array('sales_flat_order_item' => 'sales/order_item'),
'main_table.entity_id = sales_flat_order_item.order_id',
array(
'Order Item Increment' => 'item_id',
'Item Name' => 'name',
'sku' => 'sku',
'Item Options' => 'product_options',
'Item Original Price' => 'original_price',
'Item Price' => 'price',
'Item Qty Ordered' => 'qty_ordered',
'Item Qty invoiced' => 'qty_invoiced',
'Item Qty shipped' => 'qty_shipped',
'Item Qty Canceled' => 'qty_canceled',
'Item Qty Refunded' => 'qty_refunded',
'Item Tax' => 'tax_amount',
'Item Discount' => 'discount_amount',
'Item Total' => 'row_total_incl_tax',
));
$headings=$orderData->getData();
foreach($headings as $k => $v)
{
array_shift($v);
$heading[]= array_merge($v,$billingAdd);
}
$filename = 'csvorder/'.date('dmY-His').$orderIncrementId.".csv";
foreach($heading[0] as $key => $value ) {
$labelArray .= $key.",";
}
$labelArray = substr($labelArray, 0, -1);
$valueArray = substr($valueArray, 0, -1);
$data[] = explode(",", $labelArray);
foreach($heading as $key => $value ) {
$data[] = $value;
}
if(count($data) > 0 ) {
$fp = fopen($filename, 'w');
foreach ($data as $product) {
fputcsv($fp, $product);
}
fclose($fp);
}
}
}
?>