Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design
Showing posts with label order collection. Show all posts
Showing posts with label order collection. Show all posts

How to Create CSV files in Magento

If you want to create downloadable csv of you ordered item's you can use following script.

$orders = Mage::getModel("sales/order")->getCollection();
// prepare CSV header
$csv = '';
$_columns = array(
     "Order Id",
     "Product Name",
     "Sku",
     "Price"
);
$data = array();
// prepare CSV header...
foreach ($_columns as $column) {
       $data[] = '"'.$column.'"';
}
$csv .= implode(',', $data)."\n";
foreach ($orders as $order) {
         $items = $order->getAllItems();
         foreach ($items as $item) {
               $loadProduct = Mage::getModel('catalog/product')->load($item->getProductId());
               //prepare csv contents
                $data = array();
                $data[] = $order->getId();
                $data[] = $loadProduct['name'];
                $data[] = $loadProduct['sku'];
                $data[] = $loadProduct['price'];

                $csv .= implode(',', $data)."\n";
               //now $csv varaible has csv data as string
    }
}
     
$this->_redirect('*/*/');
$this->_prepareDownloadResponse('file.csv', $csv, 'text/csv');

How to get order collection from customer email id magento


If you want to get order collection for particular user anywhere in magento using customer email ID you can simply get this using following simple code.

For that first you need to get current logged user email ID. So use following code for getting logged user email id:


     $customer = Mage::getSingleton('customer/session')->getCustomer();
     $email = $customer->getEmail();     
     $fullname = $customer->getName();
     $firstname = $customer->getFirstname();
     $lastname = $customer->getLastname();

Now you have customer email address in your $email variable. So you can easily get order collection using this email address as following: 

     $orderCollection = Mage::getModel(‘sales/order’)->getCollection();
     $orderCollection->addFieldToFilter(‘customer_email’, $email);

    foreach ($orderCollection as $_order)
    {
        echo $_order->getRealOrderId() ;
        echo $this->formatDate($_order->getCreatedAtStoreDate()) ;
        echo $_order->getShippingAddress() ? $this->escapeHtml($_order->getShippingAddress()->getName()) ;
        echo $_order->formatPrice($_order->getGrandTotal());
        echo $_order->getStatusLabel();
     }


 

Copyright @ 2017 HKBlog.