I am not 100% confident reporting this issue because I am really new in Drupal Commerce. I am trying to make the multi-store (and stock per store) feature to work. I created 2 stores. My product is assigned to the 2 stores. I have a custom storeResolver which seems to work (based on the domain). I followed the README.md and created the 2 fields for store_locations. I created a stock transaction to add stock to one store only. However, when I visit the product page, I get the add-to-basket button for both the domains. I wanted to create a Views to see the stock in numeric value. This Views is very basic (see screenshot) and simply display the variation title and stock. When accessing the page, I get "Exception: The given entity can't be purchased from the current store. in Drupal\commerce_stock\StockServiceManager->getContextDetails() (line 85 of modules/contrib/commerce_stock/src/ContextCreatorTrait.php)."
In getContextDetails, I placed some debugging code. I confirm that $stores contains the 2 stores (which is normal given my product is available in both store). The $store variable is also ok (storeResolver worked).
However, it looks the in_array() is not working as expected. I am wondering how performant it is to do in_array() on objects. For testing, I simply did something like:
foreach ($stores as $store) {
$ids[] = $store->id();
}
$currentStore = \Drupal::service('commerce_store.current_store');
$store = $currentStore->getStore();
if (!in_array($store->id(), $ids)) {
// Indicates that the site listings are not filtered properly.
throw new \Exception("The given entity can't be purchased from the current store.");
}
The error disappears. I am quite sure we can have it better with array_filter() for example but would like first to understand why in_array() is not working in my case.
| Comment | File | Size | Author |
|---|---|---|---|
| #12 | commerce-stock-3150560-12-3.0.patch | 3.19 KB | chrisscrumping |
| #11 | commerce_stock-3150560-11.patch | 3.19 KB | chrisscrumping |
Issue fork commerce_stock-3150560
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
guy_schneerson commentedI could not recreate the issue for me the in_array() works although I think that comparing IDs is safer and probably better performance do will make the change.
However, visiting the product page uses another function commerce_stock_enforcement_get_context() and that does the the IDs but in a forloop. The two should be combined however this function has code to use if the current store is not found. I suggest you debug this function to find why it is not working as you expect.
Comment #4
guy_schneerson commentedHi vbouchet,
I committed the change you suggested. Can you please debug the commerce_stock_enforcement_get_context() in commerce_stock_enforcement.module and let me know the results.
Comment #5
guy_schneerson commentedAssumed fixed as no activity. Please reopen if issue not resolved.
Comment #9
ktpm commentedHi guy_schneerson,
I think commerce_stock_enforcement_get_context() should be moved to the context creator trait as it (presumably) always returns a store where the current implementation does not.
This can result in an error page if we assume to have stores A, B and C, where A is the default store, a product has selected B and C, and then you try to edit a product variation.
Using the SelectStoreTrait saves a few lines of code but requires a try/catch. Please check my approach in the MR.
Comment #10
chrisscrumping commentedI have been running into exactly the issue described by ktpm, should we re open this ticket?
My main issue is; once I create a product, if I try moving it to other stores eg create product in store A and later move it to Store B and C (note it has to be > 1 store) I can no longer edit the product variations.
There is an issue with the patch/diff as the SelectStoreTrait requires $this->currentStore so we need to set this first.
I have added the below to the top of the getContextDetails
This seems to fix the issue I was having in that I can edit the product variations. My hunch is this may have other implications for stock and order management because its just selecting the first store in the array, although I can't see what else we can do?
Comment #11
chrisscrumping commentedCreate a new patch with suggested fix
Comment #12
chrisscrumping commented3.0 patch
Comment #13
guy_schneerson commentedOK, it looks like it needs some work :) will have a look