2

I would like to be able to redirect to a url when the user clicks the category.

enter image description here

I have this menu, with the News category. This category loads a CMS Block:

enter image description here

And only having a script which redirects me to an url:

<script type="application/javascript">
window.location.href = "http://web/blog/news/";
</script>

I have also used:

<meta http-equiv="refresh" content="0; URL='http://web/blog/news/'" />

Basically it redirects fine, but in between it loads the CMS Block, which has the javascript:

enter image description here

And after it does properly redirect to the wanted url.

The issue here is that it shows the CMS Block in between, can I avoid that somehow? Or is there a better way to do it?

The main point here is to be able to create a categories link to this webpage, and so far, creating a block and adding it to the category is what I have comed up to. If necessary I can open de code and do it programatically, but it's not what I'm seeking for due to the clients would like to add more and they are not developers.

2
  • You want custom redirection when an user click on this cateory link it goes to a cms page?
    – Amit Bera
    Commented Sep 27, 2018 at 7:39
  • Yes basically yes, but as I said, if is possible to do it through backend that would be much better than programatically. Thanks :D
    – Raül
    Commented Sep 27, 2018 at 7:41

2 Answers 2

3

You don't have to build an extension for this. You can do the following:

  1. Create new category in Products -> Categories and set a meta title inside Search Engine Optimization, for example: "News".
  2. Go to Marketing -> SEO and sales -> URL Rewrite and change the target path to the desired one. For example: "blog/news". NOTE: If your not allowed to change the target path, delete them and create it again with the correct target path or change it on Database, inside url_rewrite table.

After that, reindex.

2

For this requirement, you have to build an extension

  • which extension will create a category an Attribute and where your clients add respect cms page URL. How to create a category attribute follow Magento devdoc.
  • Then using catalog_controller_category_init_after event, you will direct to that custom attribute field value

File: Namespace/Module/Setup/InstallData.php

<?php
namespace {Namespace}\{Module}\Setup;\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;

class InstallData  implements \Magento\Framework\Setup\InstallDataInterface{

   protected $eavSetupFactory;
   public function __construct(EavSetupFactory $eavSetupFactory) {
        $this->eavSetupFactory = $eavSetupFactory;
    }
    public function install(\Magento\Framework\Setup\ModuleDataSetupInterface $setup, 
            \Magento\Framework\Setup\ModuleContextInterface $context) {

        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(\Magento\Catalog\Model\Category::ENTITY, 'cms_url', [
            'type'     => 'varchar',
            'label'    => 'Your Category Attribute Name',
            'input'    => 'text',
            'visible'  => true,
            'required' => false,
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'group'    => 'General Information',
        ]);
    }


}

Observer Class

<?php

namespace {Namespace}\{Module}\Observer;

class CategoryinitafterRedirection implements \Magento\Framework\Event\ObserverInterface {

    /**
     * @var \Magento\Framework\App\ActionFlag
     */
    protected $actionFlag;

    /**
     * @var \Magento\Framework\App\Response\RedirectInterface
     */
    protected $redirect;

    public function __construct(
        \Magento\Framework\App\ActionFlag $actionFlag,
        \Magento\Framework\App\Response\RedirectInterface $redirect           
    )
    {
        $this->redirect = $redirect;
        $this->actionFlag = $actionFlag;
    }
    public function execute(\Magento\Framework\Event\Observer $observer): void {
        $category = $observer->getEvent()->getCategory();
        $controller_action = $observer->getEvent()->getData('controller_action');
        if ($category && $category->getCmsUrl()) {
            if($category->getCmsUrl() != null){
                $this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
                $this->redirect->redirect($controller_action->getResponse(), $category->getCmsUrl());               
            }
        }

}

}

}

Namespace/Module/view/adminhtml/ui_component/category_form.xml

<?xml version="1.0"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="display_settings">
        <field name="cms_url">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="label" xsi:type="string" translate="true">CMS URL</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

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.