1

This below code adds product to cart and redirects to checkout page but can't place order. It shows shipping address is not set. I hope will get help.

namespace My\Module\Controller\Index;



class Mod extends \Magento\Framework\App\Action\Action
{



protected $formKey;   
protected $cart;
protected $product;
 protected $_response;

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,
array $data = []) {
    $this->formKey = $formKey;
    $this->cart = $cart;
    $this->product = $product;  
     $this->_response = $response;
    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://mysiteof/checkout/#payment"); exit; 

 }


}

Your Answer

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

Browse other questions tagged or ask your own question.