0

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?

Latheesan
  • 23,247
  • 32
  • 107
  • 201
  • How about creating a coupon on the fly and using it for the order? – Mahmoud Tantawy Feb 04 '16 at 12:44
  • Firstly, how can I create a coupon on the fly to give a fixed order level discount? and secondly, will it work with the method of order creation I am using (where i explictly set the subtotals and grand totals)? If it will, at which point do I set the auto-generated coupon on the order? is it before I save the order? Can you give me a better explanation / example if possible? – Latheesan Feb 04 '16 at 13:00
  • 1
    Here is how you can create a coupon http://stackoverflow.com/questions/15260397/programmatically-create-auto-generated-coupon-codes-in-magento – Mahmoud Tantawy Feb 04 '16 at 14:03
  • And of course you can set the coupon value to be either fixed value OR percentage, so that gives you the freedom. – Mahmoud Tantawy Feb 04 '16 at 14:03
  • I will give this a go and let you know if it worked. But I am still unsure how to inject the coupon onto an order that we are explictly setting subtotal/grandtotal onto. Maybe this will only work with `quote` based order creation? – Latheesan Feb 04 '16 at 14:55
  • I found some links: http://stackoverflow.com/questions/24416406/magento-apply-coupon-programmatically-but-its-printing-values , http://stackoverflow.com/questions/6381338/magento-setcouponcode-does-not-seem-to-apply-coupon-on-quote-model , http://magento.stackexchange.com/questions/7206/add-coupon-to-order-programmatically-with-modified-coupon-code – Mahmoud Tantawy Feb 04 '16 at 17:23

0 Answers0