Magento2: URL rewrites are not being generated when Single-Store Mode is enabled

Created on 21 Oct 2019  路  15Comments  路  Source: magento/magento2

Preconditions (*)

  1. Magento (Open Source) 2.3.3
  2. PHP 7.1.32
  3. MySQL 5.7.15

Steps to reproduce

  1. Ensure Single-Store Mode is disabled
  2. Change the store code and store view code so they are not default (unsure if this step is needed)
  3. Add a new product
  4. Edit product and in drop down change to your store view.
  5. Change product title
  6. Confirm all changes are reflected on the frontend.
  7. Enable Single-Store mode
  8. Change product title of previously created product, note how this doesn't change on frontend.
  9. Add a new simple product and specify a url_key
  10. Check the url_rewrite database table and search the request_path column for the url_key you entered.

Expected result

New URL key should be in the database table

Actual result

Nothing is updated in the database

Once I disable the Single-Store mode and repeat the steps. It works perfectly and the url_rewrite table is updated as soon as I save.

There seems to be an issue if you enable Single-Store mode after previously using multi-store mode and override values on a store level.

Format is valid

Most helpful comment

Can confirm @victortodoran 's comment

public function getStoreIds()
    {
        if (!$this->hasStoreIds()) {
            $storeIds = [];
            if ($websiteIds = $this->getWebsiteIds()) {
                if ($this->_storeManager->isSingleStoreMode()) {
                    $websiteIds = array_keys($websiteIds);
                }
                foreach ($websiteIds as $websiteId) {
                    $websiteStores = $this->_storeManager->getWebsite($websiteId)->getStoreIds();
                    // phpcs:ignore Magento2.Performance.ForeachArrayMerge
                    $storeIds = array_merge($storeIds, $websiteStores);
                }
            }
            $this->setStoreIds($storeIds);
        }
        return $this->getData('store_ids');
    }

When creating a new product with single-store mode enabled, $this->getWebsiteIds() returns an array [ 0 => "1" ], containing the main store as expected
Calling array_keys() makes this [ 0 => 0 ], the global scope, I don't know why Magento would do this

Magento\CatalogUrlRewrite\Model\ProductScopeRewriteGenerator::generateForGlobalScope doesn't generate rewrites this way, since the only store_id it gains is the global scope:

public function generateForGlobalScope($productCategories, Product $product, $rootCategoryId = null)
    {
        $productId = $product->getEntityId();
        $mergeDataProvider = clone $this->mergeDataProviderPrototype;

        foreach ($product->getStoreIds() as $id) {
            if (!$this->isGlobalScope($id) &&
                !$this->storeViewService->doesEntityHaveOverriddenUrlKeyForStore(
                    $id,
                    $productId,
                    Product::ENTITY
                )) {
                $mergeDataProvider->merge(
                    $this->generateForSpecificStoreView($id, $productCategories, $product, $rootCategoryId)
                );
            }
        }

        return $mergeDataProvider->getData();
    }

Commenting out the lines if ($this->_storeManager->isSingleStoreMode()) { $websiteIds = array_keys($websiteIds); } results in url-rewrites being properly generated for new products

All 15 comments

Hi @paul-blundell. Thank you for your report.
To help us process this issue please make sure that you provided the following information:

  • [ ] Summary of the issue
  • [ ] Information on your environment
  • [ ] Steps to reproduce
  • [ ] Expected and actual results

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento give me 2.3-develop instance - upcoming 2.3.x release

For more details, please, review the Magento Contributor Assistant documentation.

@paul-blundell do you confirm that you were able to reproduce the issue on vanilla Magento instance following steps to reproduce?

  • [ ] yes
  • [ ] no

@magento give me 2.3-develop instance

Hi @paul-blundell. Thank you for your request. I'm working on Magento 2.3-develop instance for you

Hi @paul-blundell, here is your Magento instance.
Admin access: https://i-25190-2-3-develop.instances.magento-community.engineering/admin
Login: admin Password: 123123q
Instance will be terminated in up to 3 hours.

I have been able to reproduce on the fresh instance of the develop branch. See my updated description for steps on how to reproduce.

I believe there is an underlying issue with Single-Store mode and others have reported similar issues: https://github.com/magento/magento2/issues/5929

Hi @engcom-Delta. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

  • [ ] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.
  • [ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • [ ] 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • [ ] 4. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!

  • [ ] 5. Add label Issue: Confirmed once verification is complete.

  • [ ] 6. Make sure that automatic system confirms that report has been added to the backlog.

Hi @paul-blundell thank you for your report. Unfortunately, I am not able to reproduce issue by steps you described on clean 2.3-develop.
#25190issue

If you'd like to update the issue, please reopen it.

@engcom-Delta Did you do step 4? With Single-Store mode set to No, did you change the scope to the store view, then edit & save the product. This is the critical step to reproducing this.

@paul-blundell I did step 4. But I didn't experience issue with generating URL keys. Tested with Enable Single-Store Mode=No and Enable Single-Store Mode=Yes.
I noticed another issue: Product Name cannot be changed in Single-Store Mode if it was edited in store view level( Default Store View in my case). For this case you could create separate issue.

I have same problem on magento 2.3.3 release.

Additionally when I changed auto generated url key, its add new record for url key

This is happening to me in 2.3.3 release as well. Quite unfortunate this bug with single store mode has gone on for years without resolution.

\Magento\Catalog\Model\Product::getStoreIds() this is the method were things go south.
More specifically the following:
if ($this->_storeManager->isSingleStoreMode()) { $websiteIds = array_keys($websiteIds); }

It still happens on 2.2.3

Can confirm @victortodoran 's comment

public function getStoreIds()
    {
        if (!$this->hasStoreIds()) {
            $storeIds = [];
            if ($websiteIds = $this->getWebsiteIds()) {
                if ($this->_storeManager->isSingleStoreMode()) {
                    $websiteIds = array_keys($websiteIds);
                }
                foreach ($websiteIds as $websiteId) {
                    $websiteStores = $this->_storeManager->getWebsite($websiteId)->getStoreIds();
                    // phpcs:ignore Magento2.Performance.ForeachArrayMerge
                    $storeIds = array_merge($storeIds, $websiteStores);
                }
            }
            $this->setStoreIds($storeIds);
        }
        return $this->getData('store_ids');
    }

When creating a new product with single-store mode enabled, $this->getWebsiteIds() returns an array [ 0 => "1" ], containing the main store as expected
Calling array_keys() makes this [ 0 => 0 ], the global scope, I don't know why Magento would do this

Magento\CatalogUrlRewrite\Model\ProductScopeRewriteGenerator::generateForGlobalScope doesn't generate rewrites this way, since the only store_id it gains is the global scope:

public function generateForGlobalScope($productCategories, Product $product, $rootCategoryId = null)
    {
        $productId = $product->getEntityId();
        $mergeDataProvider = clone $this->mergeDataProviderPrototype;

        foreach ($product->getStoreIds() as $id) {
            if (!$this->isGlobalScope($id) &&
                !$this->storeViewService->doesEntityHaveOverriddenUrlKeyForStore(
                    $id,
                    $productId,
                    Product::ENTITY
                )) {
                $mergeDataProvider->merge(
                    $this->generateForSpecificStoreView($id, $productCategories, $product, $rootCategoryId)
                );
            }
        }

        return $mergeDataProvider->getData();
    }

Commenting out the lines if ($this->_storeManager->isSingleStoreMode()) { $websiteIds = array_keys($websiteIds); } results in url-rewrites being properly generated for new products

I have experienced same issue in Magento 2.3.3 commenting the above line as mentioned by @MatthijsBreed resolved the issue for me. Thanks

Was this page helpful?
0 / 5 - 0 ratings