1

I added a product programmatically in cart and redirected to checkout page. But Can't place Order.

class Wallet 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;
    parent::__construct($context);
}

public function execute()
 { 

  $productId =482;
  $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);
    $this->cart->save();
    //$this->_response->setRedirect('magento')->sendResponse();
    header("Location: https://test.rkhomeappliances.co.in/checkout/#payment"); exit; 

    //https://test.payu.in/_payment_options
    //https://test.rkhomeappliances.co.in/checkout/#payment
 }


}

enter image description here

2

As I can see in your code you have just added product to cart and getting redirect to checkout page. But it seems you have product with weight it means it must have shipping address.

If you notice to see your check URL that have used: https://test.rkhomeappliances.co.in/checkout/#payment is redirecting you directly on payment method step and skipping shipping address and method selection steps that's why you are getting this error.

Just remove #payment from redirect URL and you will be redirected to step 1 (shipping address and method selection form which is step 1 of checkout) and when you fill shipping address and select method you will be redirected to step 2 (payment method selection form).

Note: even if you are already logged in, you must have to goto step1 to select your address and move forward because it does assign shipping address and method to checkout.

| improve this answer | |
  • 1
    Welcome @ramesh KR if this solution worked for you please mark this solution accepted thanks again. – Kumar M Jan 22 '18 at 9:52
  • Now I added a digital product and added to cart. I need to place an order directly from the controller. Can you pls help me. I can find solution – Ramesh KR Feb 2 '18 at 6:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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