Apply Coupon code to programmatically create reorder

I want to apply previous order coupon code for second recurring cycle. My code is as below which is running for second recurring cycle.

       $order = Mage::getModel('sales/order');

        $billingInfo = $this->getSubscription()->getBillingAddressInfo();
        $billingAddress = Mage::getModel('sales/order_address')->setData($billingInfo)->setId(null);

        $shippingInfo = $this->getSubscription()->getShippingAddressInfo();
        $shippingAddress = Mage::getModel('sales/order_address')->setData($shippingInfo)->setId(null);

        $payment = Mage::getModel('sales/order_payment')->setMethod($this->getSubscription()->getMethodCode());

        $transferDataKeys = array(

        $orderInfo = $this->getSubscription()->getOrderInfo();
        foreach ($transferDataKeys as $key) {
            if (isset($orderInfo[$key])) {
                $order->setData($key, $orderInfo[$key]);
            elseif (isset($shippingInfo[$key])) {
                $order->setData($key, $shippingInfo[$key]);

        $order->setStoreId($this->getSubscription()->getStoreId())->setState(Mage_Sales_Model_Order::STATE_NEW, true)->setBaseToOrderRate(
                $this->getSubscription()->getInfoValue('order_info', 'base_to_order_rate'))->setStoreToOrderRate(
                $this->getSubscription()->getInfoValue('order_info', 'store_to_order_rate'))->setOrderCurrencyCode(
                $this->getSubscription()->getInfoValue('order_info', 'order_currency_code'))->setBaseSubtotal($billingAmount)->setSubtotal(
                $this->getSubscription()->getInfoValue('order_info', 'items_qty'))->setBillingAddress($billingAddress)->setShippingAddress(

        foreach ($items as $item) {

        $transaction = Mage::getModel('core/resource_transaction');
        $transaction->addCommitCallback(array($order, 'place'));
        $transaction->addCommitCallback(array($order, 'save'));

        $orderCreateModel = Mage::getModel('adminhtml/sales_order_create');
        $quote = $orderCreateModel->getQuote();

        Mage::dispatchEvent('checkout_submit_all_after', array('order' => $order, 'quote' => $quote));

Also I have found coupon code in function initFromOrder() of Model 'adminhtml/sales_order_create'.But in collect total my coupon code set null in below function of "Mage_Sales_Model_Quote".

protected function _validateCouponCode()
    $code = $this->_getData('coupon_code');
    if (strlen($code)) {
        $addressHasCoupon = false;
        $addresses = $this->getAllAddresses();
        if (count($addresses)>0) {
            foreach ($addresses as $address) {
                if ($address->hasCouponCode()) {
                    $addressHasCoupon = true;
        //$address->hasCouponCode() my coupon code not working here 
            if (!$addressHasCoupon) {
    return $this;

And also i have try to apply coupon code from "sales_convert_order_to_quote" event. Below is my observer code

public function orderToQuote(Varien_Event_Observer $observer)
                Mage::log('orderToQuote data:',null,'mylog.log');
                $quote         =  $observer->getEvent()->getQuote();
                $quoteid       =  $quote->getId();      

                $cartItems = $quote->getAllItems();
                foreach ($cartItems as $item) {
                    $productId = $item->getProductId();
                    Mage::log('orderToQuote product id data:'.$productId,null,'mylog.log');
                    if ($productId  == 195) {
                            $couponCode = "testdata";                           

I have found product id in log from observer but not successful to applying coupon code.any help would be greatly appreciated.


Please add your code as below

    ->setState(Mage_Sales_Model_Order::STATE_NEW, true)
    ->setBaseToOrderRate($this->getSubscription()->getInfoValue('order_info', 'base_to_order_rate'))
    ->setStoreToOrderRate($this->getSubscription()->getInfoValue('order_info', 'store_to_order_rate'))
    ->setOrderCurrencyCode($this->getSubscription()->getInfoValue('order_info', 'order_currency_code'))
    ->setTotalQtyOrdered($this->getSubscription()->getInfoValue('order_info', 'items_qty'))
    ->setCouponCode('set your coupon code here')

Similar questions

How to reload checkout page after applying the coupon code or removing the coupon code magento 2?
I am facing an issue related page refresh on checkout page and it is solving after refreshing page when discount applied or removed.
How to use multiple coupon code in Magento or set up free shipping for specific customer without coupon code?
Is there anyone who could let me know how to set up a free shipping/delivery for certain customer without coupon code? The reason why without coupon code is because I'm running a specific offer at the same time and Magento is not allow to apply multiple coupon codes. I also know that there are a few extension available to use multiple coupons but I...
Validate and apply coupon when create an order programmatically
I create an order programmatically like this: how can i apply coupon code in here and validate the coupon code?
Magento Coupon codes although deleted already, doesn't allow to create coupon with same code again
I am not sure if it's some kind of core-logic(unknown to me) or a bug maybe, but even though I have already deleted a coupon-code rule from "Shopping Cart Price Rules", when I search in the "salesrule_coupon" table the deleted coupon code, it still shows up. What happens due to above scenario is that if I try to recreate the coupon-rule with same c...
Customize "apply coupon" action when a particular coupon is applied and then re-update shipping in cart & checkout
I am running magento ver. in my site. I want to customize shipping rate when a particular coupon is applied. How can I do that when "Apply Coupon" button is clicked, if a given coupon is the one, that is applied ? I never had much encounter with code in price rule section so I am not aware of which controller is executed when "Apply Coupon"...
Create coupon code programmatically (in Magento 2.x)
I'm trying to port over tens of thousands of coupons from a custom e-commerce website into Magento 2.0.8 and I'm having a hard time getting it to work. These are simple coupons either fixed $ or % off of the carts subtotal. I have been unable to get it to save from the command line. I have a command called coupons:migrate that I'm running this code...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.