Magento – How to add particular product into cart programmatically at admin side

addtocartadminprogrammatically

Many a times you have heard or seen about how to add particular product into shopping cart programmatically at frontend side. But I want to do the same at admin side.

Suppose, I have product 'SLR Camera Tripod'and I have one button say PayNow in my admin area.

I also have pre-defined customer so there is no need to attempt other beginning steps of admin side order creation process.

When I click on this PayNow button Product 'SLR Camera Tripod' should be added into cart automatically and I should directly redirect on order placement page at admin side.

Is there any snippet available for this? Hope you understood my concern and can guide me well.

Best Answer

Assuming that these item are simple products

Take a look at Programmatically create admin order with pre-populated basket & customer

Create a module with a admin controller

protected function _getSession()
{
    $session = Mage::getSingleton('adminhtml/session_quote');

    return $session;
}

public function addAction(){
    $product_id = 1; // get product id from add to cart button

    //$this->_initSession();
    $session = $this->_getSession();
    $session->clear();

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

    $product = Mage::getModel('catalog/product')->load($product_id);
    $quote->addProduct($product, 1);
    $quote->save();



    $this->_getSession()->setStore($this->_getStore())
    ->setStoreId($this->_getStore()->getId());
    $this->_redirect('*/sales_order_create');
}
Related Topic