0

I want to edit Magento 2 core function:

 /**
     * Build category URL path
     *
     * @param \Magento\Catalog\Api\Data\CategoryInterface|\Magento\Framework\Model\AbstractModel $category
     * @return string
     */
    public function getUrlPath($category)
    {
        if (in_array($category->getParentId(), [Category::ROOT_CATEGORY_ID, Category::TREE_ROOT_ID])) {
            return '';
        }
        $path = $category->getUrlPath();
        if ($path !== null && !$category->dataHasChangedFor('url_key') && !$category->dataHasChangedFor('parent_id')) {
            return $path;
        }
        $path = $category->getUrlKey();
        if ($path === false) {
            return $category->getUrlPath();
        }
        if ($this->isNeedToGenerateUrlPathForParent($category)) {
            $parentPath = $this->getUrlPath(
                $this->categoryRepository->get($category->getParentId(), $category->getStoreId())
            );
            $path = $parentPath === '' ? $path : $parentPath . '/' . $path;
        }
        return $path;
    }

I am new to Magento 2 and I want to edit the aforementioned code. The problem is that, after I edit the core file and later on when Magento executes an update, my edited codes will be reset.

I searched on Google and found out that we can create plugins to edit these core functions, But I am getting confused. I can't find simple tutorials or reference about how to do it.

There are some answers on Stackexchange, which says to Add override preference in di.xml, then create Create block,model and controller file in your module. I am not able to understand these procedures? Can anyone please explain what is block,model and controller? please How should I use it with the above function.

Kindly guide me with the same. Any help will be appreciated.

Thank You

0

1 Answer 1

0

You need custom module for that.Three main steps

  1. Make Custom Module.
  2. Extend core file using di.xml of your module
  3. Place your file in place and extend it with core file.

Follow this for Making Module in Magento 2 then using di.xml file of your module etc/di.xml file and place file in path given in type param of preference

<?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\Sales\Block\Adminhtml\Invoice" type="Package\ModuleName\Block\Adminhtml\Invoice" />  

</config>

You can create invoice.php file or (whatever is name of file you want to extend ) inside Package\ModuleName\Block\Adminhtml folder and changes core data.

Thanks.

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