1

我已经在 magento 中以编程方式创建订单。所以我想发送邮件添加订单创建时间,但它不起作用。在生成订单时,我使用以下代码以编程方式发送订单电子邮件——

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

我使用了 magento 1.6.1.0 版本。这是以编程方式创建订单的代码-

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

请建议并帮助我以编程方式发送订单电子邮件。

4

3 回答 3

1

尝试添加

$order->sendNewOrderEmail();

$transaction->addObject($order);
$transaction->addCommitCallback(array($order, 'place'));
$transaction->addCommitCallback(array($order, 'save'));
$transaction->save();
于 2013-06-24T13:44:21.943 回答
0

嗨,您已经启用了正确格式的所有内容:Mage::app()->getStore()->setConfig(Mage_Sales_Model_Order::XML_PATH_EMAIL_ENABLED, "1");

您需要检查是否发送了订单邮件,如果是,则可以使用以下代码

if(!$order ->getEmailSent()){
$order ->sendEmail(true);
$order ->setEmailSent(true);
}
于 2013-06-24T13:48:37.897 回答
0

试试这个代码

$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)
      {
      } 
于 2013-06-24T13:44:29.523 回答