Magento 2 Override by preference not working

Magento 2 Override by preference not working

July 13, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2 Override by preference not working

Debugging Magento 2 Overrides: A Comprehensive Guide

Are you having trouble overriding a Magento 2 model using the preference method? You’re not alone.

In your case, you’ve correctly set up your di.xml file with the preference declaration:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator" type="Rltsquare\Customrewrite\Model\CatalogUrlRewrite\ProductUrlPathGenerator" />
</config>

And you’ve created the override model file:

<?php

namespace Rltsquare\Customrewrite\Model\CatalogUrlRewrite;

use Magento\Store\Model\Store;
use Magento\Catalog\Model\Product;

class ProductUrlPathGenerator extends \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator
{

    protected function prepareProductUrlKey(Product $product)
    {
        $urlKey = $product->getUrlKey();
        return 'products/' . $product->formatUrlKey($urlKey === '' || $urlKey === null ? $product->getName() : $urlKey);
    }

}

However, the override seems to be failing. Let’s troubleshoot this step-by-step.

Step 1: Check Your Namespace and Class Name

Ensure that the namespace declared in your di.xml file matches exactly with the one used in your model file. The type attribute should start with Rltsquare (your vendor name) followed by the module name (Customrewrite), then the class name.

Step 2: Clear Your Cache and Recompile

After making changes to your preference override, you must clear the Magento cache and recompile your code. You can do this via the command line using the following commands:

php bin/magento cache:clean
php bin/magento setup:di:compile

These commands will delete all cached files and compile your dependency injection configuration again.

Step 3: Verify Your Override

After running these commands, clear your browser cache and visit any page that should trigger the overridden model. In your case, this would likely be a product listing or detail page.

If the override is working correctly, you should see the expected changes in the URL structure for products.

Step 4: Troubleshooting Common Issues

  1. Incorrect Filename: Ensure that the filename of your model matches exactly with the one declared in the preference type attribute. The filename must end with ProductUrlPathGenerator.php.

  2. Use of Backslashes: Be careful with backslashes (\) used for namespaces and class inheritance. Always use forward slashes (/) for directory paths.

  3. Dependency Injection Configuration: Make sure your di.xml file is located in the correct path according to your module structure. For a custom module, it should be placed under app/code/{Vendor}/{Module}/etc/di.xml.

Conclusion: Mastering Magento 2 Overrides

Overriding Magento core models using the preference method can be powerful but requires careful attention to details.

Remember these key points:

  • Always keep your vendor and module names consistent.
  • Use forward slashes for directory paths.
  • Clear cache and recompile after making changes.
  • Verify your overrides by testing relevant pages.

If you’re still facing issues, consider seeking assistance from the Magento community or contacting a professional developer. For comprehensive guides on Magento development and troubleshooting, visit Laravel Company.

Magento 2 Catalog Extensions Configuration

Our network