I was researching on how to create orders programatically and I came across this awesome post: https://blog.amasty.com/creating-magento-order-programmatically/
The main code that creates the order looks like this:
public function createOrder($products)
{
if (!($this->_customer instanceof Mage_Customer_Model_Customer)){
$this->setCustomer(self::CUSTOMER_RANDOM);
}
$transaction = Mage::getModel('core/resource_transaction');
$this->_storeId = $this->_customer->getStoreId();
$reservedOrderId = Mage::getSingleton('eav/config')
->getEntityType('order')
->fetchNewIncrementId($this->_storeId);
$currencyCode = Mage::app()->getBaseCurrencyCode();
$this->_order = Mage::getModel('sales/order')
->setIncrementId($reservedOrderId)
->setStoreId($this->_storeId)
->setQuoteId(0)
->setGlobalCurrencyCode($currencyCode)
->setBaseCurrencyCode($currencyCode)
->setStoreCurrencyCode($currencyCode)
->setOrderCurrencyCode($currencyCode);
$this->_order->setCustomerEmail($this->_customer->getEmail())
->setCustomerFirstname($this->_customer->getFirstname())
->setCustomerLastname($this->_customer->getLastname())
->setCustomerGroupId($this->_customer->getGroupId())
->setCustomerIsGuest(0)
->setCustomer($this->_customer);
$billing = $this->_customer->getDefaultBillingAddress();
$billingAddress = Mage::getModel('sales/order_address')
->setStoreId($this->_storeId)
->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_BILLING)
->setCustomerId($this->_customer->getId())
->setCustomerAddressId($this->_customer->getDefaultBilling())
->setCustomerAddress_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())
->setVatId($billing->getVatId());
$this->_order->setBillingAddress($billingAddress);
$shipping = $this->_customer->getDefaultShippingAddress();
$shippingAddress = Mage::getModel('sales/order_address')
->setStoreId($this->_storeId)
->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING)
->setCustomerId($this->_customer->getId())
->setCustomerAddressId($this->_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())
->setVatId($billing->getVatId());
$this->_order->setShippingAddress($shippingAddress)
->setShippingMethod($this->_shippingMethod);
$orderPayment = Mage::getModel('sales/order_payment')
->setStoreId($this->_storeId)
->setCustomerPaymentId(0)
->setMethod($this->_paymentMethod)
->setPoNumber(' – ');
$this->_order->setPayment($orderPayment);
$this->_addProducts($products);
$this->_order->setSubtotal($this->_subTotal)
->setBaseSubtotal($this->_subTotal)
->setGrandTotal($this->_subTotal)
->setBaseGrandTotal($this->_subTotal);
$transaction->addObject($this->_order);
$transaction->addCommitCallback(array($this->_order, 'place'));
$transaction->addCommitCallback(array($this->_order, 'save'));
$transaction->save();
}
I used it like this:
$orderGenerator = new OrderGenerator();
$orderGenerator->setCustomer(Mage::getModel('customer/customer')->load(1234));
$orderGenerator->createOrder(array(
array(
'product' => 4174,
'qty' => 2
),
array(
'product' => 631,
'qty' => 7
)
));
This works, an order is created for the specified customer and products.
In the above example, the order total was £25.00
(for e.g.).
If I wanted to give the order an overall discount of say £5.00
, I could adjust the $this->_subTotal
before its used to set the values.
However, this only works partially. It displays correctly the discounted amount of the order (£20.00
). But when I go to invocie it, it's the full amount (£25.00
).
How can I programatically create an order with a fixed order level discount? or is this not possible? Do I need to approach this using the quote
mechanism?