0

Method inside InstallData.php/UpgradeData.php to remove suffix from product and catalog pages:

public function removeSuffix()
{
    $this->_config->saveConfig('catalog/seo/product_url_suffix', null, 'default', 0);
    $this->_config->saveConfig('catalog/seo/category_url_suffix', null, 'default', 0);
}

As you can see in screenshots below, in backend the changes have been made and the core_config_data table has the fields that it would have if it was done manually in backend, but in url_rewrite table paths don't get updated.

Product and category links still show up as e.g. http://test.dev/gear/bags.html with the suffix.

I have done sudo bin/magento indexer:reindex and also tried flushing cache.


The suffix fields in backend are changed after running script:

enter image description here


The core_config_data table has the fields that would normally be there if it was done manually:

enter image description here


The url_rewrite table paths are not being changed:

enter image description here

1 Answer 1

1

UPDATE:

This setup script will set the product and category suffix values to an empty string and will update the url_rewrite table accordingly (assuming the suffix values were not empty to begin with).

<?php

namespace Vendor\Module\Setup;

use Magento\Framework\App\State as AppState;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\InstallDataInterface;

class InstallData implements InstallDataInterface
{
    protected $configFactory;

    protected $appState;

    public function __construct(
        \Magento\Config\Model\Config\Factory $configFactory,
        AppState $appState
    ){
        $this->configFactory = $configFactory;
        $this->appState = $appState;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $this->appState->setAreaCode('adminhtml');

        $config = $this->configFactory->create();
        $config->setDataByPath('catalog/seo/product_url_suffix', '');
        $config->save();

        $config = $this->configFactory->create();
        $config->setDataByPath('catalog/seo/category_url_suffix', '');
        $config->save();

        $setup->endSetup();
    }
}

Original answer:

There is an afterSave method that runs when saving those values in the admin. You could try saving the suffix values in the admin in the System Config section of the admin and see if that removes the suffix from the url_rewrite table.

Also note that in Magento 2 there is no longer a url_rewrite index process, so this may have something to do with the index not making any difference.

6
  • Yes, if I manually change them in backend it works, but that's not what I want. I want it to work in the setup script.
    – rits
    Commented Feb 10, 2017 at 6:35
  • Well, the afterSave method is a public function, so you may be able to call afterSave on the Magento/Catalog/Model/System/Config/Backend/Catalog/Url/Rewrite/Suffix class. You will need to play with changing the value because afterSave checks if the value has changed before calling updateSuffixForUrlRewrites.
    – user50408
    Commented Feb 10, 2017 at 6:53
  • url_rewrite table updates request_path if a product/category gets saved. Is there a way I could loop through every product and category and trigger save on them?
    – rits
    Commented Feb 10, 2017 at 10:41
  • 1
    @Rudolph, I have updated my answer. Please give that a try.
    – user50408
    Commented Feb 12, 2017 at 6:46
  • 1
    @Rudolph, The setAreaCode was needed in order to get the correct Backend Model to load, since the afterSave method only runs in the admin. This post does a pretty good job capturing the basics of areas in Magento, and contains links to the Magento documentation on areas.
    – user50408
    Commented Feb 13, 2017 at 1:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.