1

I have create order programmatically in magento. so i want to sent mail add the time of order creation but it is not working. I used following code for send order email programmatically at the time of order generate --

**Mage::app()->getStore()->setConfig(Mage_Sales_Model_Order::XML_PATH_EMAIL_ENABLED, "1");**

I have used magento 1.6.1.0 version. here is the code of order creation programmatically --

$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);

    // 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/Shipping 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('freeshipping_freeshipping');
    /*->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('free')
    ->setAction('yes')
    ->setPo_number(' – ')
    ->setOrderStatus('processing');
    $order->setPayment($orderPayment);

    // let say, we have 2 products
    //check that your products exists
    //need to add code for configurable products if any
    $subTotal = 0;

    $products = getFreeProducts($point);

    foreach ($products as $product) {
    $_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $product['sku']); 
    $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().' (Martini Free Product)')
    ->setSku($_product->getSku())
    ->setPrice($_product->getPrice())
    ->setBasePrice($_product->getPrice())
    ->setOriginalPrice($_product->getPrice())
    ->setRowTotal($rowTotal)
    ->setBaseRowTotal($rowTotal);

    //$subTotal += $rowTotal;
    $subTotal = 0;
    $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();

please suggest and help me for send order email programmatically.

3 Answers 3

1

Try adding

$order->sendNewOrderEmail();

After

$transaction->addObject($order);
$transaction->addCommitCallback(array($order, 'place'));
$transaction->addCommitCallback(array($order, 'save'));
$transaction->save();
0

Try this code

$email = Mage::getSingleton('customer/session')->getCustomer()->getEmail();

$orders = Mage::getModel('sales/order')->getCollection()->getLastItem()->getIncrementId();



      $order_mail = new Mage_Sales_Model_Order();
      $incrementId = $orders;
      $order_mail->loadByIncrementId($incrementId);

      try
      {
        $order_mail->sendNewOrderEmail();
      } 

      catch (Exception $ex)
      {
      } 
0

Hi you have all things in correct format as as you have enabled this : Mage::app()->getStore()->setConfig(Mage_Sales_Model_Order::XML_PATH_EMAIL_ENABLED, "1");

You need to checkwhether order mail was sent or not if so then you can use below code

if(!$order ->getEmailSent()){
$order ->sendEmail(true);
$order ->setEmailSent(true);
}
3
  • I got this issue while compile this code : a:5:{i:0;s:73:"Invalid method Mage_Sales_Model_Order::sendEmail(Array ( [0] => 1 ) )";
    – Prasanth P
    Dec 5, 2014 at 11:25
  • what is your magento version? and check app/code/core/Mage/Sales/Model/Order.php
    – Anil Gupta
    Apr 23, 2015 at 12:37
  • Magento version 1.8, I checked Sales/Model/Order.php, nothing wrong in the code.
    – Prasanth P
    Apr 24, 2015 at 21:43

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.