Magento – “Shipping address is not set” error when saving cart

addtocartcartmagento2

I have a simple code, where i add a simple product to the cart

$cart = $obj->get('\Magento\Checkout\Model\Cart');
$cart->addProduct(5, 3);
$cart->save();

but the last row, returns this error:
Fatal error: Uncaught Magento\Framework\Exception\StateException: Shipping address is not set

Why is necessary to add a shipping address to save the cart?

How i can add an address programmatically?

Best Answer

\Magento\Checkout\Model\Cart' class has been deprecated from100.1.0`.

Use \Magento\Quote\Model\Quoteinstead Please check https://github.com/magento/magento2/commit/e5479b4e7b87b8ad22dc1ca8e4c33556999da5f2

Related Topic