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.

Command icon 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

vbouchet created an issue. See original summary.

guy_schneerson’s picture

I 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.

guy_schneerson’s picture

Status: Active » Postponed (maintainer needs more info)

Hi 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.

guy_schneerson’s picture

Status: Postponed (maintainer needs more info) » Fixed

Assumed fixed as no activity. Please reopen if issue not resolved.

Status: Fixed » Closed (fixed)

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

ktpm made their first commit to this issue’s fork.

ktpm’s picture

Hi 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.

chrisscrumping’s picture

I 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

/** @var \Drupal\commerce_store\CurrentStore $currentStore */
    $this->currentStore = \Drupal::service('commerce_store.current_store');
    $store = $this->currentStore->getStore();

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?

chrisscrumping’s picture

StatusFileSize
new3.19 KB

Create a new patch with suggested fix

chrisscrumping’s picture

StatusFileSize
new3.19 KB

3.0 patch

guy_schneerson’s picture

Status: Closed (fixed) » Needs work

OK, it looks like it needs some work :) will have a look