Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
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
Comment #2
bojanz CreditAttribution: bojanz at Centarro commentedLooks 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?
Comment #3
IgnacioFarre CreditAttribution: IgnacioFarre as a volunteer commentedSame error, I see in "commerce_product__stores" table that the products have a store id.
Comment #4
vasikeI 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
Comment #5
IgnacioFarre CreditAttribution: IgnacioFarre as a volunteer commentedI 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 :(
Comment #6
vasikeThe 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.
Comment #7
terribeausejour CreditAttribution: terribeausejour commented@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.
Comment #8
bojanz CreditAttribution: bojanz at Centarro commented@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.
Comment #9
bojanz CreditAttribution: bojanz at Centarro commentedWe are already tracking the "prevent deletion of entities in use" issue.
The only thing we can do is improve the error message.
Comment #11
bojanz CreditAttribution: bojanz at Centarro commentedDone. The new error is "The given entity is not assigned to any store."
Comment #13
firfin CreditAttribution: firfin commentedStill having trouble with this error? Check #2945939: Remove the default_store config key, replace it with isDefault() flag for some possible solutions.