- Add a product to the store without a price.
- Visit that that product page, click "Add to cart"

Unexpected Error:
TypeError: Argument 1 passed to Drupal\commerce_order\Entity\OrderItem::setUnitPrice() must be an instance of Drupal\commerce_price\Price, null given, called in /Applications/MAMP/htdocs/d8/web/modules/contrib/commerce/modules/cart/src/Form/AddToCartForm.php on line 217 in /Applications/MAMP/htdocs/d8/web/modules/contrib/commerce/modules/order/src/Entity/OrderItem.php on line 12

Comments

Thomas Cys created an issue. See original summary.

bojanz’s picture

We need to make that price field required, it seems.

Thomas Cys’s picture

Copied from ProductionVariation class baseFieldDefinitions method:
// The price is not required because it's not guaranteed to be used
// for storage (there might be a price per currency, role, country, etc).

I accidentally forgot to add to price and ran into this issue but are there use cases where you don't want to add a price to a product? If there are the price should be 0 not null imo.

Thomas Cys’s picture

Thomas Cys’s picture

Status: Active » Needs review
luizsgpetri’s picture

I had the same issue and creating products with price fix it. Thanks for sharing this issue, it saved me some time to figure out it was my fault =p

  • bojanz committed 7635d4c on 8.x-2.x authored by Thomas Cys
    Issue #2877911 by Thomas Cys, bojanz: Add to cart fails when adding a...
bojanz’s picture

Status: Needs review » Fixed

Committed, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.