Magento 2.3.5: Add product to cart with custom options and price

Magento 2.3.5: Add product to cart with custom options and price

August 18, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2.3.5: Add product to cart with custom options and price

Magento 2.3.5: Add Product to Cart with Custom Options and Price

Hello developers! Today, let’s dive into a common issue faced by many Magento users - adding products to the cart programmatically with custom options and prices. While WooCommerce makes this process rather straightforward, Magento can be quite challenging due to its extensive documentation.

Understanding Your Current Setup

You’ve already made some progress by initializing the necessary components in your controller:

protected $_cart;
protected $_productFactory;

public function __construct(
    \Magento\Checkout\Model\Cart $cart,
    \Magento\Catalog\Model\ProductFactory $productFactory
) {
    $this->cart = $cart;
    this->productFactory = $productFactory;
}

However, you’re encountering several issues:

  1. The product is only visible in the cart after adding another product.
  2. The price isn’t being updated; it remains at the default value.
  3. The custom option isn’t visible anywhere - not even in the checkout or order details.

Resolving Price and Option Visibility

To resolve these issues, we need to understand how Magento handles options and pricing. By default, Magento stores product data separately from cart items. This means that when you add a product to the cart using the addProduct method, it doesn’t automatically inherit any custom attributes or options from the original product.

Updating Product Pricing

To update the price of your product in the cart, you can use the setCustomPrice method:

$product->setCustomPrice($custom_price);

This will set a new price for the specific instance of this product being added to the cart. Make sure to call this method before calling $this->cart->addProduct.

Adding Custom Options

To add custom options, you need to use the addOption method:

$options = [$option1, $option2, ...];
$this->cart->addOptions($options);

Each option should be an array containing the following keys:

  • product_sku: The SKU of the product.
  • name: The name of the custom option (e.g., “Size”).
  • price: The additional price for this option.

Saving Changes to the Cart

After adding your options, make sure to save the cart using $this->cart->save().

Conclusion: Implementing a Working Solution

While Magento’s documentation can be quite overwhelming at times, there are clear steps to add products with custom prices and options. The key is to understand how to set custom attributes on the specific instance of the product being added to the cart.

Remember that adding a product programmatically doesn’t automatically update its pricing or options unless you explicitly tell Magento to do so using the methods mentioned above.

For more detailed guides on working with Magento 2, I highly recommend checking out the official documentation at https://laravelcompany.com/magento-documentation. This resource provides a comprehensive overview of key concepts and best practices for developing custom extensions.

Magento 2 Checkout Catalog Orders

Our network