Programmatically Create Admin Order with Pre-Populated Basket & Customer in Magento

adminhtmlquote

I'm working on a quote system where I want the admin to be able to activate it, which will then forward them to the create new order page in Adminhtml, sales_order_create.

I want this page to already have a customer assigned & products selected.

Edit:

I'd like to be able to pre-populate the quote for the Adminhtml page for creating an order.

Here's an example of creating a cart quote on the front-end:

$cart = Mage::getModel('checkout/cart');
$product = Mage::getModel('catalog/product')->load($productId);
if ($product){
    if ($product->isSaleable()){
        $cart->addProduct($product, array('qty' => $qty));
    }
}
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
$cart->save();

I want to do this, but in the backend (sales_order_place)

I'm looking at the code in app/code/core/Mage/Adminhtml/Model/Sales/Order/Create.php and it looks like adminhtml/session_quote would be the model I'd be aiming to use.

So the workflow would be:

  1. Set up quote object
  2. Call $this->redirect('*/sales_order_create');
  3. The admin will then be redirected to the create new order page with a customer already selected and the basket already populated with products

Would really appreciate some input.

Best Answer

This is very possible. Here is a very scaled back version of how to accomplish this task.

Explanation:

The adminhtml/session_quote object provides access to a Mage_Sales_Model_Quote object. This object is associated with the session, so the changes made to it are persisted. Do what you would like to the quote object. The items are loaded via $this->getQuote()->getAllVisibleItems(); (in Mage_Adminhtml_Block_Sales_Order_Create_Items).

Depending on how you are going to use this, you may need to clear the session:

if (is_object($this->_order)) {
    $this->_order->clearInstance();
    $this->_order->reset();
}
$this->_order = null;

$this->_orderCreator = null;
$this->_json = null;
$this->_orderData = null;
$this->_orderItems = null;
$this->_customer = null;

$this->_getSession()->unsetSpecificData();

Mage::unregister('rule_data');

Code:

protected $_orderCreator;
protected $_store;

public function indexAction()
{
    $this->_initSession();

    $quote = $this->_getSession()->getQuote();

    // TODO: REPLACE FOLLOWING CONSTANT WITH PRODUCT(S)
    $product = Mage::getModel('catalog/product')->load(16);
    $quote->addProduct($product, 1);
    $quote->save();

    $this->_redirect('*/sales_order_create');
}

/**
 * Retrieve order create model
 *
 * @return  Mage_Adminhtml_Model_Sales_Order_Create
 */
protected function _getCreateModel()
{
    if (!$this->_orderCreator) {
        $this->_orderCreator = Mage::getModel('adminhtml/sales_order_create');
    }

    return $this->_orderCreator;
}
/**
 * Retrieve session object
 *
 * @return Mage_Adminhtml_Model_Session_Quote
 */
protected function _getSession()
{
    $session = Mage::getSingleton('adminhtml/session_quote');

    return $session;
}

/**
 * Initialize order creation session data
 *
 * @param array $data
 * @return Mage_Adminhtml_Sales_Order_CreateController
 */
protected function _initSession()
{
    $this->_getSession()->clear();

    // TODO: SET CUSTOMER HERE
    $this->_getSession()->setCustomerId(1);

    $this->_getSession()->setStore($this->_getStore())
        ->setStoreId($this->_getStore()->getId());

    return $this;
}

protected function _getStore()
{
    if (!$this->_store) {
        // TODO: SET STORE HERE
        $this->_store = Mage::getModel('core/store')->load(1);
    }

    return $this->_store;
}
Related Topic