How to overwrite vendor files and add custom code into the default function.

How to overwrite vendor files and add custom code into the default function.

1.) first add dependency into Tridhyatech/RemoveParentUrl/etc/di.xml

2.) add below code into Tridhyatech/RemoveParentUrl/Model/CategoryUrlPathGenerator.php

namespace Tridhyatech\Products\Model;

use Magento\Catalog\Model\Category;

class CategoryUrlPathGenerator extends \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator

{

public function getUrlPath($category, $parentCategory = null)

{

Custom code;

}

}

3.) Things you should take care of while over writing vendor files.

  • you have to define all the private functions or private variables in the custom file if your custom function is using it , if you don’t define those variables then it will not work.
  • if you are overwriting an existing function or method then you have to copy the whole function and paste it into a custom file and then add your code into it , because it will only call the function which is present in your custom file.
  • You have to extend the file you are overwriting into a custom file , no need to extend the super parent only extend the parent file.
  • If you are calling a constructor into your custom file then you should have to define all variables and assign it to the parent function as shown below.

public function __construct(

\Magento\Framework\App\Action\Context $context,

\Magento\Customer\Model\Session $customerSession,

CustomerRepositoryInterface $customerRepository,

AccountManagementInterface $accountManagement

)

{

parent::__construct( $context, $customerSession,$customerRepository, $accountManagement );

}



Copyright © 2024 Tridhya Tech Limited, Inc. All rights reserved.