We are currently finalising our newest Shopware Store Plugin: Order via batch to cart. It is already in use on one of our customers stores and didn’t work at all. It showed all products, but the products with variants showed only a random variant. We looked into it, but couldn’t really understand what the problem is. Even worse, we couldn’t reproduce the problem. Neither on stage, nor locally.
Database?
We have the problem, that we don’t have an automatic sync from production to stage and local yet, therefore I thought: Most likely it is something in the database. And thankfully the shop is not yet live, so I didn’t need to bother with GDPR and copied the database to local. But it didn’t change the behaviour. Locally all ran fine with the database.
Not the database content? Version? PHP?
But if it is not the database and not the code, what it is then? There are only a few options which makes sense, before we start looking into stuff like networking, operating system, etc.:
- PHP Version
- Database Version
- Infrastructure (redis, elasticsearch, …)
- Services which only run on production (ERP, CRM, …)
The PHP versions don’t match 100% because ddev only allows to define the minor version, but I’m confident enough into the PHP language, project and development, that this is enough. Version 8.2 is the same, so ?
Database version does match as well, both are running on MySQL 8.
Redis or ElasticSearch?
Ok, what about the infrastructure? *check notes* On production we are running Redis for Session and Caching and ElasticSearch. We have all this in our local setups, but it seems I deactivated it for some reason. So turning it on showed the same behaviour as on production
FINALLY! ? It is broken!
ElasticSearch it is
Ok, but what changes if I run elastic search? Time to dig deeper.
Use Elastic if possible
If one digs deeper, we find \Shopware\Core\Content\Product\SalesChannel\Listing\ProductListingLoader::load
and see in the code:
$origin->addState(Criteria::STATE_ELASTICSEARCH_AWARE);
So each time you load a category page (or any other kind of product list) Shopware tries to use Elastic if possible. This happened in our case as well. Unfortunately not everything we do with the database is possible in Elastic, especially because the data is not indexed and available.
TL;DR The fix
So because Shopware is adding this State to our Criteria, the fix is easy: Remove it again ?
$origin->removeState(Criteria::STATE_ELASTICSEARCH_AWARE);
And finally it works as expected. With and without ElasticSearch. One could argue, wether this is a good fix and I would say: Yes it is. It works.
Although, the better fix might be to change the indexer, make the data inside of ElasticSearch available and use it if available.
One thought on “Find the one (important) difference”