Skip to content

Fix Url Rewrite not generated for stores that have different url key and adds the deprecated message to CategoryBasedProductRewriteGenerator class #36360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 21 commits into
base: 2.4-develop
Choose a base branch
from

Conversation

TuVanDev
Copy link
Member

@TuVanDev TuVanDev commented Oct 22, 2022

Description (*)

This pull request includes fixes for #36295 and adds the deprecated message to Magento\CatalogUrlRewrite\Model\CategoryBasedProductRewriteGenerator class due to it is no longer used anywhere after the change in commit c7d6324 (Magento 2.2.0) which \Magento\CatalogUrlRewrite\Model\CategoryProductUrlPathGenerator was introduced to replace Magento\CatalogUrlRewrite\Model\CategoryBasedProductRewriteGenerator.

  1. Fixes Url rewrite not generated for newly assigned category #36295

  2. Fix another related issue to Url rewrite not generated for newly assigned category #36295: URL Rewrite not generated for stores that have different URL key when updating category products/product in the store scope (changing product's categories under another store)

  3. Add the @deprecated tag to the Magento\CatalogUrlRewrite\Model\CategoryBasedProductRewriteGenerator class and indicate that the Magento\CatalogUrlRewrite\Model\CategoryProductUrlPathGenerator class should be used.

Assumption:

  • Magento 2 (affected to all versions) with 2 store views
  • Configurations -> Catalog -> Catalog tab -> Search Engine Optimization -> Generate "category/product" URL Rewrites = Yes
  • Product A used the default value of url key in the store view 1
  • Product A used a different url key (do not use default value) in the store view 2

Behavior:

Updating (add/remove) product categories for product A in one of the below scopes:

  • Global scope
  • Store view 1

Expected result: Url rewrite regenerate for both store view 1 and store view 2.

Actually result: Url rewrite only generates for store view 1 but does not regenerate for store view 2.

Related Pull Requests

Fixed Issues (if relevant)

  1. Fixes Url rewrite not generated for newly assigned category #36295

Manual testing scenarios (*)

  1. ...
  2. ...

Questions or comments

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • README.md files for modified modules are updated and included in the pull request if any README.md predefined sections require an update
  • All automated tests passed successfully (all builds are green)

@m2-assistant
Copy link

m2-assistant bot commented Oct 22, 2022

Hi @Viper9x. Thank you for your contribution
Here are some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names. Allowed build names are:

  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE,
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here

ℹ️ Run only required test builds during development. Run all test builds before sending your pull request for review.

For more details, review the Magento Contributor Guide documentation.

⚠️ According to the Magento Contribution requirements, all Pull Requests must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.

🕙 You can find the schedule on the Magento Community Calendar page.

📞 The triage of Pull Requests happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

✏️ Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

@m2-community-project m2-community-project bot added Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: pending review labels Oct 22, 2022
@TuVanDev
Copy link
Member Author

@magento give me test instance

@magento-deployment-service
Copy link

Hi @Viper9x. Thank you for your request. I'm working on Magento instance for you.

@magento-deployment-service
Copy link

@TuVanDev
Copy link
Member Author

@magento give me 2.4-develop instance

@magento-deployment-service
Copy link

Hi @Viper9x. Thank you for your request. I'm working on Magento instance for you.

@magento-deployment-service
Copy link

Copy link
Contributor

@Den4ik Den4ik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Viper9x. Thanks for you work.
Removing of the class is backward incomparable change and should be approved by architects. I believe that use of @deprecate message more useful for fast delivery.
You mentioned c7d6324 that replace object manager usage with injection to constructor of \Magento\CatalogUrlRewrite\Model\CategoryProductUrlPathGenerator. It's not \Magento\CatalogUrlRewrite\Model\CategoryBasedProductRewriteGenerator

…dundant since commit #c7d6324"

This reverts commit d84fded.
@TuVanDev
Copy link
Member Author

TuVanDev commented Oct 28, 2022

Hi @Den4ik.
I only say that Magento\CatalogUrlRewrite\Model\CategoryBasedProductRewriteGenerator is no longer used after the change in commit c7d6324 (Magento 2.2.0).
More explanation about that: commit c7d6324 changed from using Magento\CatalogUrlRewrite\Model\CategoryBasedProductRewriteGenerator to \Magento\CatalogUrlRewrite\Model\CategoryProductUrlPathGenerator, also replace object manager usage with injection to constructor. In Magento_CatalogUrlRewrite module, only Magento\CatalogUrlRewrite\Observer\UrlRewriteHandler class
uses Magento\CatalogUrlRewrite\Model\CategoryBasedProductRewriteGenerator, and after that commit, Magento\CatalogUrlRewrite\Model\CategoryBasedProductRewriteGenerator is not used anymore.

Do you think adding * @deprecated 101.0.0 Because \Magento\CatalogUrlRewrite\Model\CategoryProductUrlPathGenerator was introduced. to Magento\CatalogUrlRewrite\Model\CategoryBasedProductRewriteGenerator class's docBlock is fine?

Thanks for your support.

Magento\CatalogUrlRewrite\Model\CategoryBasedProductRewriteGenerator is no longer used after the change in commit c7d6324
@TuVanDev
Copy link
Member Author

TuVanDev commented Nov 4, 2022

Hi @Den4ik I've added the deprecated message to Magento\CatalogUrlRewrite\Model\CategoryBasedProductRewriteGenerator class due to it is no longer used after the change in commit c7d6324
Please check, thanks.

@hostep
Copy link
Contributor

hostep commented Nov 10, 2022

@Viper9x: it looks like a commit made it in the 2.4-develop branch recently by the core team: e46f719

I have the feeling it might solve the same issue you are trying to solve, could you investigate that?
If it does solve the same issue, this PR can be closed.
If it doesn't solve the same issue, you'll have to update your PR so that no merge conflicts exist anymore.

Thanks! 🙂

@TuVanDev
Copy link
Member Author

@hostep: Thanks for let me know about the update related to my PR. I've investigated and tested for sure.

The commit e46f719 resolve related issue but not the issue I'm trying to solve.

Detail:

  • The commit e46f719: fix URL Rewrite not generated for stores that have different url key when updating category products/ product in global scope, it does not fix when updating category products/ product in store scope
  • My commits in this PR: fix URL Rewrite not generated for stores that have different url key when updating category products/ product in both global and store scope.

I have been updated my PR with the lastest changes from 2.4-develop branch, so my PR won't be conflict with other.

Thanks Pieter Hoste

@TuVanDev TuVanDev changed the title Fix url rewrite not generated for stores that have different url key and removing redundant class Fix Url Rewrite not generated for stores that have different url key and adds the deprecated message to CategoryBasedProductRewriteGenerator class Nov 12, 2022
@hostep
Copy link
Contributor

hostep commented Nov 12, 2022

Thanks @Viper9x for the investigation and extra feedback!

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@Den4ik
Copy link
Contributor

Den4ik commented Nov 12, 2022

@Viper9x Thanks for update. Could you please check failed Unit and Static tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@engcom-Delta
Copy link
Contributor

@magento run Integration Tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@engcom-Delta
Copy link
Contributor

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@engcom-Delta
Copy link
Contributor

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

2 similar comments
@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@engcom-Delta
Copy link
Contributor

@magento run WebAPI Tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@engcom-Delta engcom-Delta removed their assignment Mar 29, 2023
@cptX
Copy link

cptX commented Apr 2, 2023

I have magento 2.4.5 and magento 2.4.6. When I update the url key of a category that has subcategories the url key is not updated in the frontend, although it is saved in the category settings. Also in url rewrites the new url is not visible at all. Could this be related to the issue you try to solve here?

@TuVanDev
Copy link
Member Author

TuVanDev commented Apr 2, 2023

@cptX I don't have time to check that issue, could you apply the patch for this pull request to see if it resolves your issue?
Here is the patch for this pull request:

diff --git a/vendor/magento/module-catalog-url-rewrite/Model/CategoryBasedProductRewriteGenerator.php b/vendor/magento/module-catalog-url-rewrite/Model/CategoryBasedProductRewriteGenerator.php
index 105ca290..8e599f91 100644
--- a/vendor/magento/module-catalog-url-rewrite/Model/CategoryBasedProductRewriteGenerator.php
+++ b/vendor/magento/module-catalog-url-rewrite/Model/CategoryBasedProductRewriteGenerator.php
@@ -11,8 +11,9 @@ use Magento\Catalog\Model\Product\Visibility;

 /**
  * Class ProductUrlRewriteGenerator
- * @package Magento\CatalogUrlRewrite\Model
  * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
+ * @deprecated 101.0.0 please use \Magento\CatalogUrlRewrite\Model\CategoryProductUrlPathGenerator instead.
+ * @see https://github.com/magento/magento2/commit/c7d6324fe84766ddd9ab9710f19b5923fcbac09a
  */
 class CategoryBasedProductRewriteGenerator
 {
diff --git a/vendor/magento/module-catalog-url-rewrite/Model/ProductUrlRewriteGenerator.php b/vendor/magento/module-catalog-url-rewrite/Model/ProductUrlRewriteGenerator.php
index f5e6ae9a..d1785773 100644
--- a/vendor/magento/module-catalog-url-rewrite/Model/ProductUrlRewriteGenerator.php
+++ b/vendor/magento/module-catalog-url-rewrite/Model/ProductUrlRewriteGenerator.php
@@ -6,16 +6,14 @@
 namespace Magento\CatalogUrlRewrite\Model;

 use Magento\Catalog\Model\Product;
+use Magento\Catalog\Model\Product\Visibility;
 use Magento\CatalogUrlRewrite\Model\Product\CanonicalUrlRewriteGenerator;
 use Magento\CatalogUrlRewrite\Model\Product\CategoriesUrlRewriteGenerator;
 use Magento\CatalogUrlRewrite\Model\Product\CurrentUrlRewritesRegenerator;
 use Magento\CatalogUrlRewrite\Service\V1\StoreViewService;
 use Magento\Framework\App\ObjectManager;
-use Magento\Catalog\Model\Product\Visibility;

 /**
- * Class ProductUrlRewriteGenerator
- * @package Magento\CatalogUrlRewrite\Model
  * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  */
 class ProductUrlRewriteGenerator
@@ -23,10 +21,11 @@ class ProductUrlRewriteGenerator
     /**
      * Entity type code
      */
-    const ENTITY_TYPE = 'product';
+    public const ENTITY_TYPE = 'product';

     /**
      * @deprecated 100.1.0
+     * @see https://github.com/magento/magento2/commit/6729b6e01368248abc33300208eb292c95050203
      * @var \Magento\CatalogUrlRewrite\Service\V1\StoreViewService
      */
     protected $storeViewService;
@@ -34,41 +33,48 @@ class ProductUrlRewriteGenerator
     /**
      * @var \Magento\Catalog\Model\Product
      * @deprecated 100.1.0
+     * @see https://github.com/magento/magento2/commit/6729b6e01368248abc33300208eb292c95050203
      */
     protected $product;

     /**
      * @deprecated 100.1.0
+     * @see https://github.com/magento/magento2/commit/6729b6e01368248abc33300208eb292c95050203
      * @var \Magento\CatalogUrlRewrite\Model\Product\CurrentUrlRewritesRegenerator
      */
     protected $currentUrlRewritesRegenerator;

     /**
      * @deprecated 100.1.0
+     * @see https://github.com/magento/magento2/commit/6729b6e01368248abc33300208eb292c95050203
      * @var \Magento\CatalogUrlRewrite\Model\Product\CategoriesUrlRewriteGenerator
      */
     protected $categoriesUrlRewriteGenerator;

     /**
      * @deprecated 100.1.0
+     * @see https://github.com/magento/magento2/commit/6729b6e01368248abc33300208eb292c95050203
      * @var \Magento\CatalogUrlRewrite\Model\Product\CanonicalUrlRewriteGenerator
      */
     protected $canonicalUrlRewriteGenerator;

     /**
      * @deprecated 100.1.0
+     * @see https://github.com/magento/magento2/commit/6729b6e01368248abc33300208eb292c95050203
      * @var \Magento\CatalogUrlRewrite\Model\ObjectRegistryFactory
      */
     protected $objectRegistryFactory;

     /**
      * @deprecated 100.1.0
+     * @see https://github.com/magento/magento2/commit/6729b6e01368248abc33300208eb292c95050203
      * @var \Magento\CatalogUrlRewrite\Model\ObjectRegistry
      */
     protected $productCategories;

     /**
      * @deprecated 100.1.0
+     * @see https://github.com/magento/magento2/commit/6729b6e01368248abc33300208eb292c95050203
      * @var \Magento\Store\Model\StoreManagerInterface
      */
     protected $storeManager;
@@ -106,13 +112,14 @@ class ProductUrlRewriteGenerator
      * Retrieve Delegator for generation rewrites in different scopes
      *
      * @deprecated 100.1.4
+     * @see https://github.com/magento/magento2/commit/b2ce2a37d921b5ad88fc38663fc0ff3dd6c582d1
      * @return ProductScopeRewriteGenerator|mixed
      */
     private function getProductScopeRewriteGenerator()
     {
         if (!$this->productScopeRewriteGenerator) {
             $this->productScopeRewriteGenerator = ObjectManager::getInstance()
-            ->get(ProductScopeRewriteGenerator::class);
+                ->get(ProductScopeRewriteGenerator::class);
         }

         return $this->productScopeRewriteGenerator;
@@ -131,23 +138,19 @@ class ProductUrlRewriteGenerator
             return [];
         }

-        $storeId = $product->getStoreId();
-
         $productCategories = $product->getCategoryCollection()
             ->addAttributeToSelect('url_key')
             ->addAttributeToSelect('url_path');

-        $urls = $this->isGlobalScope($storeId)
-            ? $this->generateForGlobalScope($productCategories, $product, $rootCategoryId)
-            : $this->generateForSpecificStoreView($storeId, $productCategories, $product, $rootCategoryId);
-
-        return $urls;
+        // Generate url rewrites for all store views to ensure store views with different url-key are generated as well.
+        return $this->generateForGlobalScope($productCategories, $product, $rootCategoryId);
     }

     /**
      * Check is global scope
      *
      * @deprecated 100.1.4
+     * @see https://github.com/magento/magento2/commit/b2ce2a37d921b5ad88fc38663fc0ff3dd6c582d1
      * @param int|null $storeId
      * @return bool
      */
@@ -160,6 +163,7 @@ class ProductUrlRewriteGenerator
      * Generate list of urls for global scope
      *
      * @deprecated 100.1.4
+     * @see https://github.com/magento/magento2/commit/b2ce2a37d921b5ad88fc38663fc0ff3dd6c582d1
      * @param \Magento\Framework\Data\Collection $productCategories
      * @param \Magento\Catalog\Model\Product|null $product
      * @param int|null $rootCategoryId
@@ -178,6 +182,7 @@ class ProductUrlRewriteGenerator
      * Generate list of urls for specific store view
      *
      * @deprecated 100.1.4
+     * @see https://github.com/magento/magento2/commit/b2ce2a37d921b5ad88fc38663fc0ff3dd6c582d1
      * @param int $storeId
      * @param \Magento\Framework\Data\Collection $productCategories
      * @param Product|null $product
@@ -195,7 +200,10 @@ class ProductUrlRewriteGenerator
     }

     /**
+     * Checking the category is generating correctly
+     *
      * @deprecated 100.1.4
+     * @see https://github.com/magento/magento2/commit/b2ce2a37d921b5ad88fc38663fc0ff3dd6c582d1
      * @param \Magento\Catalog\Model\Category $category
      * @param int $storeId
      * @return bool

@cptX
Copy link

cptX commented Apr 4, 2023

@Viper9x I have the setting "Use Categories Path for Product URLs" to "No". My problem is only with the category url rewrites. Will this patch help this? I see that the above modifications affect product url rewrites not categories.
If I understood correctly the necessary files to be changed for functioning are CategoryBasedProductRewriteGenerator.php and ProductUrlRewriteGenerator.php, correct?

@cptX
Copy link

cptX commented Apr 4, 2023

@Viper9x Unfortunately this patch didn't solve my problem. I'm still unable to access my second store view categories. Rewrite URL is visible in the configuration (it is indeed generated) but not working. Browser says "The page isn’t redirecting properly". As I said I have the setting "Use Categories Path for Product URLs" to "No".
I'm feeling that the issue is much deeper than what this patch is doing...

UPDATE: my issue was caused by the module https://github.com/alex-79/magento2-hide-default-store-code-from-url
After disabling the module all the urls rewrites of the second store view worked.

@engcom-Dash
Copy link
Contributor

Hello @TuVanDev ,

Thanks for the contributions!

Please resolve the conflict so we can proceed with this PR.

Thanks.

@engcom-Dash
Copy link
Contributor

Closing this PR since it has not been updated in a while. Please feel free to reopen if needed.

@TuVanDev
Copy link
Member Author

TuVanDev commented Nov 8, 2024

Hey @engcom-Dash,
Could you please reopen this ticket?

@Den4ik Den4ik reopened this Nov 11, 2024
Copy link

m2-assistant bot commented Nov 11, 2024

Hi @TuVanDev. Thank you for your contribution!
Here are some useful tips on how you can test your changes using Magento test environment.
❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names.

Allowed build names are:
  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here
ℹ️ Run only required test builds during development. Run all test builds before sending your pull request for review.


For more details, review the Code Contributions documentation.
Join Magento Community Engineering Slack and ask your questions in #github channel.

@Den4ik
Copy link
Contributor

Den4ik commented Nov 11, 2024

@TuVanDev PR reopened. Please resolve conflict.

@TuVanDev
Copy link
Member Author

@Den4ik @engcom-Dash I have resolved the merge conflict. Please review the changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: needs update
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Url rewrite not generated for newly assigned category
7 participants