Magento2: When creating a new category via API, not all redirect entries are generated

Created on 27 May 2020  路  14Comments  路  Source: magento/magento2

Preconditions

  1. develop-2.4
  2. Create website austria_web, store austria_store, storeview "austria"

Steps to reproduce

  1. Call this script

    BEARER=`n98-magerun2 admin:token:create admin` # or get a bearer via API
    BASEURL="http://put-your-magento-base-url-here"
    echo '{"category":{"name":"Test","is_active":1,"parentId":2}}' > /tmp/datacreate.json
    
    curl -k -X POST $BASEURL/rest/V1/categories -H 'Content-Type: application/json' -H 
    "Authorization: Bearer $BEARER" -d @/tmp/datacreate.json > /tmp/result.json
    

Expected result (*)

  1. Marketing -> SEO -> URL rewrites should have two entries for the new Test category

Actual result (*)

  1. There is only a rewrite for the first store

Followup issue of #28197

Partial Workaround

Push the category again on store view level, and with the custom_attribute url_key set

EDIT

But this works only, if the url key is different. In case of Austria and German those are in almost every case the same (both use German) so there is no workaround


  • [x] Severity: S0 _- Affects critical data or functionality and leaves users without workaround._
ready for confirmation Reported on 2.4.0

Most helpful comment

Seems to me that creating a category should always create the url rewrites for all store views, whether "all" store code is passed or not. It seems Magento assume the url rewrite are created for all store views because when updating a store view with /rest/STORE_CODE/V1/categories/ with the same url key, no url rewrite is created.

In fact, updating a category should always check if the url rewrite is present and if not present create the url rewrite, whether the url key changed or not. Currently it seems the url rewrite part is skipped if the url key ain't modified.

All 14 comments

Hi @amenk. 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.4-develop instance - upcoming 2.4.x release

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

Please, add a comment to assign the issue: @magento I am working on this


I was able to reproduce it on 2.4-develop

Is this intended behavior or a bug?

I would say it's a bug because I get a empty URL when trying to get the category URL in a Block with the code:
{{widget type="Magento\Catalog\Block\Category\Widget\Link" template="category/widget/link/link_href.phtml" id_path="category/79"}}.

I get the category url for the first store view that have a different URL key and an entry in URL rewrite. But the exact same code doesn't works in the second store that have the exact same url key as the "All Store View" and that DOESN'T have an entry in URL rewrite.

I'm running Magento 2.3.4-p2, I haven't tried with 2.3.5 or 2.4-develop.

This may be a regression. I have 2 Magento installations running on 2 different servers. I've used pretty much the same script on both servers to create categories with the REST API. I've ran the script on the first server somewhere in 2019 on Magento 2.3.2 or 2.3.3. The categories url rewrites exists for both store views. On the second server, I've ran the script in March 2020 on Magento 2.3.4, the categories URL rewrites exists ONLY for the first store view. The first store view uses different URL key than "All Store View".

My workaround that fixed the issue is to append the store id to the url key. There is also an workaround suggestion, which I haven't tried, around the end of #16134.

@fgagne thanks for the confirmation. We already know about the URL regeneration script, but it's still really annoying as new categories are created in the PIM all the time, causing API pushes and URL problems.

Can you elaborate on your first workaround to append the store id?

@fgagne

Can you elaborate on your first workaround to append the store id?

do you mean pushing the same URL key with for example -2 appended to the second store? If so, yes this would work. But of course the visible URLs are then "not clean" anymore and have an unnecessary suffix

@amenk this is exactly what I've done.

But perhaps it could done the other way around?

  1. change the url key of the "All Store View" by appending something unique to the url key like "-0", by calling the REST API with no store code in the URL (or "all"?)
  2. for each store view, change the url key to the original value by calling the REST API with the store view code in the URL

Please keep us updated if you try it out.

@fgagne Okay, got it. That would work, but need changes in the API consumer. We'll try to identify the issue during the next days...

We try to make a failing test case.
Approach:

Extend or Copy \Magento\Catalog\Api\CategoryRepositoryTest::testCreate plus this fixture
dev/tests/integration/testsuite/Magento/Store/_files/core_second_third_fixturestore.php

@magento I am working on this

TL;DR when using /rest/V1/categories the first store is assumed; when using /rest/all/V1/categories instead it works

with @DanieliMi we found out that in In
https://github.com/magento/magento2/blob/a94aa0089a4385e21f33db4b684dc209efb08d51/app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGenerator.php#L103 the store ID is 1, when pushing via API. When using the Admin backend it's 0 (global) and all URL keys are generated.

Workarkound is to push to /rest/all/V1/categories

Failing test case is here: https://github.com/DanieliMi/magento2/commit/9f2fa5b9fed3a9ed317ac39adf7619839ba0f667

Now the big question is if the API should behave like this or not.

Seems to me that creating a category should always create the url rewrites for all store views, whether "all" store code is passed or not. It seems Magento assume the url rewrite are created for all store views because when updating a store view with /rest/STORE_CODE/V1/categories/ with the same url key, no url rewrite is created.

In fact, updating a category should always check if the url rewrite is present and if not present create the url rewrite, whether the url key changed or not. Currently it seems the url rewrite part is skipped if the url key ain't modified.

Wondering what happens if someone creates a Category in the backend, while a specific Store-View is selected in the drop down?

Was this page helpful?
0 / 5 - 0 ratings