0

I need to add new Const in \Magento\Catalog\Model\Category core class how can i override core class and add Const in Magento 2.4const

2
  • Can you describe your requirement in more details. Jul 23, 2021 at 9:34
  • I want to add Constant on \Magento\Catalog\Model\Category, with overriding class Jul 26, 2021 at 4:03

2 Answers 2

0

You can extend a core class and replace that in di.xml. But it's a bad practice. It's better to use a plugin to change something

Plus you can add information to the class through di.xml

Please read more here: https://devdocs.magento.com/guides/v2.4/extension-dev-guide/build/di-xml-file.html

3
  • Can you able to give example of how to add new Const in \Magento\Catalog\Model\Category core class by overriding in m2 Jul 24, 2021 at 10:16
  • I am able to override core class and method but not able to define Constant in on overriding class Jul 24, 2021 at 10:37
  • It's just a regular PHP class. You can do anything you want. Could you please share an error message that you get when you try to define a const?
    – Donia Mark
    Jul 24, 2021 at 15:28
0
  1. create di.xml in to the following location

app/code/Vendor/Extension/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Catalog\Model\Category" type="Vendor\Extension\Model\Category" />
</config>
  1. create Category.php file on this location.

app/code/Vendor/Extension/Model/Category.php

namespace Vendor\Extension\Model;

class Category extends \Magento\Catalog\Model\Category
{
    const STATUS_PICKING = 'picking';

  public function __construct()
    {
        echo "Model Rewrite Working"; die();

    }
}
10
  • Thanks For reply! I want to add Constant on Rewrited model Jul 26, 2021 at 6:48
  • where and how you wan to added that? you can added that here on the code. Jul 26, 2021 at 7:14
  • See code i have edited i want like this and i need to access this const in another class Jul 26, 2021 at 8:32
  • where is your code? nothing added to question. Jul 26, 2021 at 9:39
  • I have edited your code Jul 26, 2021 at 10:45

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.