Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top