With Drupal Commerce 2.x on Drupal 8.x, some products fail Add to Cart with the following error, while other products of the same type are successfully added to the cart.

Exception: The given entity can't be purchased from the current store. in Drupal\commerce_cart\Form\AddToCartForm->selectStore() (line 239 of /srv/bindings/6187247a214a47e9921dcdbac898249e/code/modules/commerce/modules/cart/src/Form/AddToCartForm.php).

Comments

terribeausejour created an issue. See original summary.

bojanz’s picture

Looks like you have certain products created without a store, which is a required field.

Load those products in Devel, confirm that their stores reference field is empty, then try to see how that happened: were they created via an API? Did someone modify the product type and accidentally removed the stores field from the form display?

IgnacioFarre’s picture

Same error, I see in "commerce_product__stores" table that the products have a store id.

vasike’s picture

Priority: Normal » Critical

I could reproduce this by deleting the store.

It seems similar with other issue about entities that are deleted : #2774885: Custom currency can be deleted when it is the stores default currency

We need some protection as we're a click away from breaking a commerce system.

p.s. Changed the status for attention

IgnacioFarre’s picture

I detected same problem, when click the add to cart button, drupal add other product with the same atributte, but I don't get the "Exception"

I have a store enable.
It's through a view whith Format:Unformatted list -> Show:Product

The view create a bad the list.
I check with xdebug and the $form_state in buildForm -> AddToCartForm.php come bad. It's change values.
Sorry for my english :(

vasike’s picture

The only way i get this is by deleting the Store:
- it could the "single" store built in the commerce system
or
- the "other" store which the product is assigned to.

terribeausejour’s picture

@bojanz You were (mostly) correct -- I didn't have products created without a store, but I did have the store field disabled on the form (since I have only one store, it seemed to make sense not to present that field). So thank you for the clue "Did someone modify the product type and accidentally removed the stores field from the form display?". Although it was not accidental, your comment lead me to the solution. Thankfully, I was able to work around the issue by re-enabling the store in the product forms -- and -- it was also necessary to open each product and then save it. With those 2 steps, products now add to cart successfully.

bojanz’s picture

@terribeausejour
The stores field hides itself when there's only 1 available field, there's no need for you to do that yourself, it just crashes the system.

bojanz’s picture

Title: Drupal Commerce Add to Cart Error » Provide a more precise error message when a malformed product references no stores
Category: Bug report » Task
Priority: Critical » Normal
Issue tags: -Commerce (duplicate), -add to cart, -store

We are already tracking the "prevent deletion of entities in use" issue.

The only thing we can do is improve the error message.

  • bojanz committed 5f7d6ff on 8.x-2.x
    Issue #2865233: Provide a more precise error message when a malformed...
bojanz’s picture

Status: Active » Fixed

Done. The new error is "The given entity is not assigned to any store."

Status: Fixed » Closed (fixed)

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

firfin’s picture

Still having trouble with this error? Check #2945939: Remove the default_store config key, replace it with isDefault() flag for some possible solutions.