Multistore and context

Created on 10 June 2020, almost 5 years ago
Updated 29 February 2024, about 1 year 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.

  • 🇬🇧United Kingdom chrisscrumping

    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?

  • 🇬🇧United Kingdom chrisscrumping

    Create a new patch with suggested fix

  • 🇬🇧United Kingdom chrisscrumping

    3.0 patch

  • 🇬🇧United Kingdom guy_schneerson

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

Production build 0.71.5 2024