#StackBounty: Magento Order report

Bounty: 50

I got mail every week order report below the code

<?php
include 'app/Mage.php';
ini_set('display_errors', 1);
Mage::app();
    $ordershippeddays = 7; // number of days you want
    $fromDate = gmdate("Y-m-d H:i:s", gmmktime(0, 0, 0, gmdate("m"), gmdate("d") - $ordershippeddays, gmdate("Y")));
    $toDate = gmdate("Y-m-d H:i:s", gmmktime(23, 59, 59, gmdate("m"), gmdate("d"), gmdate("Y")));

     $ordercollection = Mage::getResourceModel('sales/order_item_collection')
    ->addAttributeToSelect('order_id')
    ->addAttributeToSelect('created_at')
    ->addAttributeToFilter('created_at', array('from' => $fromDate, 'to' => $toDate))
    ->load();
    $orderCount = count($ordercollection);
    Mage::log($orderCount,null,'custom.log');
    $tableHeaderHtml = '<table cellspacing="0" cellpadding="0" border="0" width="650" style="border:1px solid #eaeaea"><thead><tr>';
    $tableHeaderHtml .= '<th align="center" bgcolor="#EAEAEA" style="font-size:13px;padding:3px 9px;">Order Id</th><th align="center" bgcolor="#EAEAEA" style="font-size:13px;padding:3px 9px;">Name</th>';
    $tableHeaderHtml .= '<th align="center" bgcolor="#EAEAEA" style="font-size:13px;padding:3px 9px;">Email</th><th align="center" bgcolor="#EAEAEA" style="font-size:13px;padding:3px 9px;">Payment Method</th>';
    $tableHeaderHtml .= '<th align="center" bgcolor="#EAEAEA" style="font-size:13px;padding:3px 9px;">Order Status</th><th align="center" bgcolor="#EAEAEA" style="font-size:13px;padding:3px 9px;">Product Name</th>';
    $tableHeaderHtml .= '<th align="center" bgcolor="#EAEAEA" style="font-size:13px;padding:3px 9px;">Billing Address</th><th align="center" bgcolor="#EAEAEA" style="font-size:13px;padding:3px 9px;">Shipping Address</th>';
    $tableHeaderHtml .= '</tr></thead><tbody bgcolor="#F6F6F6">';
    $processedTemplate .="";
    foreach ($ordercollection as $orderkey => $ordervalue) {
    $eid = $ordervalue->getOrderId();   
    $order = Mage::getModel("sales/order")->load($eid);
    Mage::log("Testing success...........");
    $orderInfo = $order->getIncrementId().','.$order->getPayment()->getMethodInstance()->getTitle().','.$order->getStatusLabel().','.number_format ($order->getGrandTotal(), 2, '.' , $thousands_sep = '');
    $userDetails = $order->getCustomerName().','.$order->getCustomerEmail();            
    $billingAddress = $order->getBillingAddress();
    $shipping_address = $order->getShippingAddress();
    Mage::log($billingAddressFull = $billingAddress->getStreetFull().','.$billingAddress->getRegion().','.$billingAddress->getPostcode().','.$billingAddress->getCountry().', Mob: '.$billingAddress->getTelephone());
    Mage::log($shipping_addressFull = $shipping_address->getStreetFull().','.$shipping_address->getRegion().','.$shipping_address->getPostcode().','.$shipping_address->getCountry().', Mob: '.$shipping_address->getTelephone());

    foreach ($order->getAllVisibleItems() as $item) {
        $productDetails = $item->getName().','.$item->getSku().', Qty: '.(int)$item->getQtyOrdered();
        $productName = $item->getName();

    $tableContentHtml = '<tr>';
    $tableContentHtml .= '<td align="left" valign="top" style="font-size:11px;padding:3px 9px;border-bottom:1px dotted #cccccc;">' . $order->getIncrementId() . '</td><td align="center" valign="top" style="font-size:11px;padding:3px 9px;border-bottom:1px dotted #cccccc;">' . $order->getCustomerName() . '</td>';
    $tableContentHtml .= '<td align="left" valign="top" style="font-size:11px;padding:3px 9px;border-bottom:1px dotted #cccccc;">' . $order->getCustomerEmail() . '</td><td align="center" valign="top" style="font-size:11px;padding:3px 9px;border-bottom:1px dotted #cccccc;">' . $order->getPayment()->getMethodInstance()->getTitle() . '</td>';
    $tableContentHtml .= '<td align="left" valign="top" style="font-size:11px;padding:3px 9px;border-bottom:1px dotted #cccccc;">' . $order->getStatusLabel() . '</td><td align="center" valign="top" style="font-size:11px;padding:3px 9px;border-bottom:1px dotted #cccccc;">' . $productName . '</td>';
    $tableContentHtml .= '<td align="left" valign="top" style="font-size:11px;padding:3px 9px;border-bottom:1px dotted #cccccc;">' . $billingAddressFull . '</td><td align="center" valign="top" style="font-size:11px;padding:3px 9px;border-bottom:1px dotted #cccccc;">' . $shipping_addressFull . '</td>';
    $tableContentHtml .= '<tr>'; 
    Mage::log($productName,null,'custom.log');
    $template_id = 'order_update_email';
      $email_to = 'my@gmail.com';
      $customer_name   = "Test";
      $email_template  = Mage::getModel('core/email_template')->loadDefault($template_id);
      $email_template_variables = array(
          'custom_variable' => $fromDate,
          'custom_variable1' => $toDate,
          'custom_variable3' =>$orderCount,
          'custom_variable4'=>$orderInfo,
          'custom_variable5'=>$tableHeaderHtml,
          'custom_variable6'=>$tableContentHtml
      );


      $processedTemplate .= $email_template->getProcessedTemplate($email_template_variables);
      // echo $processedTemplate;
      // exit();
     // $sender_name = 'Admin';
     // $sender_email = Mage::getStoreConfig('trans_email/ident_general/email');
     // $email_template->setSenderName($sender_name);
     // $email_template->setSenderEmail($sender_email); 
    //  $email_template->send($email_to, $customer_name, $email_template_variables);


    }
    }

    $mail = Mage::getModel('core/email');
    $mail->setToName('My Name');
    $mail->setToEmail('mymail@gmail');
    $mail->setBody($processedTemplate);
    $mail->setSubject('Test');
    $mail->setFromEmail('mymail@gmail');
    $mail->setFromName("Msg to Show on Subject");
    $mail->setType('html');// YOu can use Html or text as Mail format
    $mail->send();

?>

I added this in my root file then i will run. I din’t get any mail.

Also i add and check this

$processedTemplate .= $email_template->getProcessedTemplate($email_template_variables);
           echo $processedTemplate;
           exit();

I got only one order information in that mail. Also that order have 2 peoduct but it showing only one.

How i fix this?


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.