This is my controller to add a specific product to the cart. It is working fine. Now I don't need to add in cart, but directly to place order using product id. My product id is 483. I need to place order this product and redirect to another page.


namespace Module\Name\Controller\Index;

class Places extends \Magento\Framework\App\Action\Action

protected $formKey;   
protected $cart;
protected $product;
protected $_response;
protected $customerRepository;
protected $addressRepository;

public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\Data\Form\FormKey $formKey,
\Magento\Checkout\Model\Cart $cart,
\Magento\Catalog\Model\Product $product,
 \Magento\Framework\App\ResponseInterface $response,
 \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository ,
 \Magento\Customer\Api\AddressRepositoryInterface $addressRepository,

array $data = []) {
    $this->formKey = $formKey;
    $this->cart = $cart;
    $this->product = $product;  
     $this->_response = $response;
     $this->customerRepository = $customerRepository;
     $this->addressRepository = $addressRepository;

public function execute()

  $productId =483;
  $params = array(
                'form_key' => $this->formKey->getFormKey(),
                'product' => $productId, //product Id
                'qty'   =>1 
               //quantity of product                
    //Load the product based on productID   
    $_product = $this->product->load($productId);       
    $this->cart->addProduct($_product, $params);



1 Answer 1


I think the best way to do this is through API as it falls in line with magento 2's idea of service contracts -

You can find how to do it in this answer - Magento 2 - Create order using REST API

  • this seems to look like entering variables manually?
    – Ramesh KR
    Jan 31, 2018 at 5:45
  • you can call API in php using curl and pass the parameters dynamically. Jan 31, 2018 at 7:03

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.