I have to add the custom option price when product is adding add to cart using controller

1 Answer 1


If you have a product with custom options like this -

enter image description here

and you want to add it to cart with the option Zip Type = Metal, use the following code:

namespace MyVendor\MyModule;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Catalog\Api\ProductRepositoryInterface;

class AddToCart
    private $cart;
    private $productRepository;
    private $storeManager;

    public function __construct(
        \Magento\Checkout\Model\Cart $cart,
        ProductRepositoryInterface $productRepository,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        $this->cart = $cart;
        $this->productRepository = $productRepository;
        $this->storeManager = $storeManager;

    protected function _initProduct($productId)
        $storeId = $this->storeManager->getStore()->getId();
        try {
            return $this->productRepository->getById($productId, false, $storeId);
        } catch (NoSuchEntityException $e) {
            return false;

    public function addToCart()
        $product = $this->_initProduct("8");
        $this->cart->addProduct($product, ['options' => [1 => 2]] );

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.