Multistore and context

Created on 10 June 2020, over 4 years ago
Updated 29 February 2024, 7 months ago

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.

🐛 Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

🇫🇷France vbouchet

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • First commit to issue fork.
  • Merge request !21Issue #3150560 → (Open) created by ktpm
  • 🇩🇪Germany ktpm

    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.

Production build 0.71.5 2024