0

I'm attempting to reorder via code using the follow code from: https://stackoverflow.com/a/7122181/2758139

$orderId= $YOUR_ORDER_NUMBER;
$personsOrder = Mage::getModel('sales/order')->load($orderId);
$order_model = Mage::getSingleton('adminhtml/sales_order_create');
$personsOrder->setReordered(true);
$order_model->initFromOrder($personsOrder);
$order_model->createOrder();

This code works great. However, I am trying to set a different payment method while trying to do this. The following is what I have:

$orderId= $YOUR_ORDER_NUMBER;
$personsOrder = Mage::getModel('sales/order')->load($orderId);
$order_model = Mage::getSingleton('adminhtml/sales_order_create');
$personsOrder->setReordered(true);
$order_model->initFromOrder($personsOrder);
/* Trying this */
$order_model->getQuote()->setPayment(array('method' => 'free'));
/* ---- */
$order_model->createOrder();

The new code trying to set the different payment method doesn't seem to be working. I've debugged and followed the code to where it validates the payment method in the core of Adminhtml/Model/Order/Create.php and where it get the method $method = $this->getQuote()->getPayment()->getMethodInstance(); but it is still the original payment method.

How do I set a new payment method when reordering via code?

1
  • I think this doesn't work because you want to modify the quote before creating the order. I've done some investigation, but it takes too much time for now to write you a ready-to-go solution. You should look at Mage_Sales_Controller_Abstract::reorderAction() and realize that it's not the cart you want to modify but a specific quote. Then you could use Vinai's example at pastebin.com/8cft4d8v to create the order. Convert an order address to a quote address using Mage::getModel('sales/convert_order')->addressToQuoteAddress($orderAddress). Hope this points you in the right direction. Commented Nov 8, 2013 at 9:56

2 Answers 2

1

After quite a bit of debugging the code and following how the admin side reorders, I found the solution. Instead of editing the quote, I could edit the original order before setReorder(true).

Below is my final code that works (which works for any active payment method).

Example for reordering on the Check Method.

$personsOrder = Mage::getModel('sales/order')->load($orderId);

/* Set different payment method */
$personsOrder->getPayment()->setMethod('checkmo');
$personsOrder->getPayment()->setMethodInstance(
    Mage::getModel('payment/method_checkmo')->setInfoInstance(
        $personsOrder->getPayment()->getMethodInstance()->getInfoInstance()));
/* /Set different payment method */

Mage::getSingleton('adminhtml/sales_order_create')->initFromOrder($personsOrder->setReordered(true))->createOrder();
0

I have create a extension for reorder please find the following code for it

<?php class Cdotsys_ScheduleOrder_ScheduleorderController extends Mage_Core_Controller_Front_Action{
private $_storeId = '1';
private $_groupId = '1';
private $shipping_methord = 'flatrate_flatrate';
private $_sendConfirmation = '0';
private $orderData = array();
private $_sourceCustomer;

public function IndexAction() {

              $this->loadLayout();   
              $this->getLayout()->getBlock("head")->setTitle($this->__("Scheduleorder"));
                    $breadcrumbs = $this->getLayout()->getBlock("breadcrumbs");
              $breadcrumbs->addCrumb("home", array(
                        "label" => $this->__("Home Page"),
                        "title" => $this->__("Home Page"),
                        "link"  => Mage::getBaseUrl()
                   ));

              $breadcrumbs->addCrumb("scheduleorder", array(
                        "label" => $this->__("Scheduleorder"),
                        "title" => $this->__("Scheduleorder")
                   ));

        $scheduleorders = Mage::getModel('scheduleorder/scheduleorder')->getCollection();    
        foreach($scheduleorders as $scheduleorder){             
            $personsOrder = Mage::getModel('sales/order')->load($scheduleorder->getOrderId());              
            //$personsOrder->setReordered(true);
            $items = $personsOrder->getAllItems();  
            foreach($items as $item){                   
                $products[$item->getProductId()] = array('qty' => $item->getQtyOrdered());                  
            } 
            $this->shipping_methord = $personsOrder->getShippingMethod()
            $customer = Mage::getModel('customer/customer')->load($personsOrder->getCustomerId());
            $this->setOrderInfo($customer,$products);
            $this->create();
            $personsOrder->setPaymentMethod('cashondelivery');

            $order_model = Mage::getSingleton('adminhtml/sales_order_create');
            /*$order_model->setPaymentData('cashondelivery');
            $order_model->getQuote()->getPayment()->addData('cashondelivery');
            $order_model->setShipping('flatrate_flatrate');
            $order_model->getQuote()->getShipping()->addData('flatrate_flatrate');
            $order_model->getQuote()->setShipping(array('method' => 'flatrate_flatrate'));
            $order_model->getQuote()->setPayment(array('method' => 'cashondelivery'));

            $order_model->initFromOrder($personsOrder);             
            $order_model->createOrder();*/
        }

              $this->renderLayout(); 

    }

public function setOrderInfo(Mage_Customer_Model_Customer $sourceCustomer,$products){
        $this->_sourceCustomer = $sourceCustomer;
        //You can extract/refactor this if you have more than one product, etc.
        $Billingaddress = Mage::getModel('customer/address')->load($this->_sourceCustomer->getDefaultBilling());
        $Shippingaddress = Mage::getModel('customer/address')->load($this->_sourceCustomer->getDefaultShipping());
        $this->orderData = array(
        'session'       => array(
                            'customer_id'   => $this->_sourceCustomer->getId(),
                            'store_id'      => $this->_storeId,
                            ),
        'payment'       => array(
                            'method'    => 'checkmo',
                            ),  
        'add_products'  =>$products,
        'order' => array(
                        'currency' => 'USD',
                        'account' => array(
                                'group_id' => $this->_groupId,
                                'email' => $this->_sourceCustomer->getEmail()
                                ),
                        'billing_address' => array(
                                                'customer_address_id' => $this->_sourceCustomer->getDefaultBilling(),
                                                'prefix' => '',
                                                'firstname' => $this->_sourceCustomer->getFirstname(),
                                                'middlename' => '',
                                                'lastname' => $this->_sourceCustomer->getLastname(),
                                                'suffix' => '',
                                                'company' => '',
                                                'street' => array($Billingaddress->getStreet(),''),
                                                'city' => $Billingaddress->getCity(),
                                                'country_id' => $Billingaddress->getCountryId(),
                                                'region' => '',
                                                'region_id' => $Billingaddress->getRegionId(),
                                                'postcode' => $Billingaddress->getPostcode(),
                                                'telephone' => $Billingaddress->getTelephone(),
                                                'fax' => '',
                                                ),
                        'shipping_address' => array(
                                                'customer_address_id' => $this->_sourceCustomer->getDefaultShipping(),
                                                'prefix' => '',
                                                'firstname' => $this->_sourceCustomer->getFirstname(),
                                                'middlename' => '',
                                                'lastname' => $this->_sourceCustomer->getLastname(),
                                                'suffix' => '',
                                                'company' => '',
                                                'street' => array($Shippingaddress->getStreet(),''),
                                                'city' => $Shippingaddress->getCity(),
                                                'country_id' => $Shippingaddress->getCountryId(),
                                                'region' => '',
                                                'region_id' => $Shippingaddress->getRegionId(),
                                                'postcode' => $Shippingaddress->getPostcode(),
                                                'telephone' => $Shippingaddress->getTelephone(),
                                                'fax' => '',
                                                ),
                                                'shipping_method' => $shipping_methord,
                                                'comment' => array(
                                                'customer_note' => 'This order has been created by scheduler order script.',
                                                ),
                            'send_confirmation' => $this->_sendConfirmation
        ),
        );
}   

protected function _getOrderCreateModel()
{
    return Mage::getSingleton('adminhtml/sales_order_create');
}
/**
* Retrieve session object
*
* @return Mage_Adminhtml_Model_Session_Quote
*/
protected function _getSession()
{
    return Mage::getSingleton('adminhtml/session_quote');
}
/**
* Initialize order creation session data
*
* @param array $data
* @return Mage_Adminhtml_Sales_Order_CreateController
*/
protected function _initSession($data)
{
/* Get/identify customer */
    if (!empty($data['customer_id'])) {
        $this->_getSession()->setCustomerId((int) $data['customer_id']);
    }
/* Get/identify store */
    if (!empty($data['store_id'])) {
        $this->_getSession()->setStoreId((int) $data['store_id']);
    }
  return $this;
}

public function create(){
    $orderData = $this->orderData;

    if (!empty($orderData)) {
            $this->_initSession($orderData['session']);
        try {
                $this->_processQuote($orderData);
                if (!empty($orderData['payment'])) {
                    $this->_getOrderCreateModel()->setPaymentData($orderData['payment']);
                    $this->_getOrderCreateModel()->getQuote()->getPayment()->addData($orderData['payment']);
                }

                Mage::app()->getStore()->setConfig(Mage_Sales_Model_Order::XML_PATH_EMAIL_ENABLED, "0");
                $_order = $this->_getOrderCreateModel()->importPostData($orderData['order'])->createOrder();
                $this->_getSession()->clear();
                Mage::unregister('rule_data');
                return $_order;
        }
        catch (Exception $e){
            Mage::log("Order save error...");
        }
    }
    return null;
}

protected function _processQuote($data = array()){
    /* Saving order data */
    if (!empty($data['order'])) {
        $this->_getOrderCreateModel()->importPostData($data['order']);
    }
    $this->_getOrderCreateModel()->getBillingAddress();
    $this->_getOrderCreateModel()->setShippingAsBilling(true);
    /* Just like adding products from Magento admin grid */
    if (!empty($data['add_products'])) {
        $this->_getOrderCreateModel()->addProducts($data['add_products']);
    }
    /* Collect shipping rates */
    $this->_getOrderCreateModel()->collectShippingRates();
    /* Add payment data */
    if (!empty($data['payment'])) {
        $this->_getOrderCreateModel()->getQuote()->getPayment()->addData($data['payment']);
    }
    $this->_getOrderCreateModel()->initRuleData()->saveQuote();

    if (!empty($data['payment'])) {
        $this->_getOrderCreateModel()->getQuote()->getPayment()->addData($data['payment']);
    }
    return $this;
}

}

Your Answer

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

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