cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2 - What is the proper way to edit core file

Magento 2 - What is the proper way to edit core file

For my Paypal Payment issue,

I just want to update the following function,

 

app/code/Magento/Paypal/Model/Api/Nvp.php

protected function _applyStreetAndRegionWorkarounds(DataObject $address)
{
// merge street addresses into 1
if ($address->getData('street2') !== null) {
$address->setStreet(implode("\n", [$address->getData('street'), $address->getData('street2')]));
$address->unsetData('street2');
}
// attempt to fetch region_id from directory
if ($address->getCountryId() && $address->getRegion()) {
$regions = $this->_countryFactory->create()
->loadByCode($address->getCountryId())
->getRegionCollection()
->addRegionCodeOrNameFilter($address->getRegion())
->setPageSize(1);
if ($regions->count()) {
$regionItems = $regions->getItems();
$region = array_shift($regionItems);
$address->setRegionId($region->getId());
$address->setExportedKeys(array_merge($address->getExportedKeys(), ['region_id']));
}
}
}

 

Instead of

 protected function _applyStreetAndRegionWorkarounds(DataObject $address)
    {
        // merge street addresses into 1
        if ($address->getData('street2') !== null) {
            $address->setStreet(implode("\n", [$address->getData('street'), $address->getData('street2')]));
            $address->unsetData('street2');
        }
        // attempt to fetch region_id from directory
        if ($address->getCountryId() && $address->getRegion()) {
            $regions = $this->_countryFactory->create()->loadByCode(
                $address->getCountryId()
            )->getRegionCollection()->addRegionCodeOrNameFilter(
                $address->getRegion()
            )->setPageSize(
                1
            );
            $regionItems = $regions->getItems();
            $region = array_shift($regionItems);
            $address->setRegionId($region->getId());
            $address->setExportedKeys(array_merge($address->getExportedKeys(), ['region_id']));
        }
    }

Source: https://github.com/magento/magento2/issues/26698

What is the best way to edit core files?

My Error:

 

Fatal error: Uncaught Error: Call to a member function getId() on null in /var/www/html/test/app/code/Magento/Paypal/Model/Api/Nvp.php:1524 Stack trace: 
#0 /var/www/html/test/app/code/Magento/Paypal/Model/Api/Nvp.php(1493): Magento\Paypal\Model\Api\Nvp->_applyStreetAndRegionWorkarounds(Object(Magento\Framework\DataObject)) 
#1 /var/www/html/test/app/code/Magento/Paypal/Model/Api/Nvp.php(849): Magento\Paypal\Model\Api\Nvp->_exportAddresses(Array) 
#2 /var/www/html/test/app/code/Magento/Paypal/Model/Express/Checkout.php(621): Magento\Paypal\Model\Api\Nvp->callGetExpressCheckoutDetails() 
#3/var/www/html/test/app/code/Magento/Paypal/Controller/Express/AbstractExpress/ReturnAction.php(32): Magento\Paypal\Model\Express\Checkout->returnFromPaypal('*********') 
#4/var/www/html/test/generated/code/Magento/Paypal/Controller/Express/ReturnAction/Interceptor.php(24): Magento\Paypal\Controller\Express\AbstractExpress\ReturnAction->execute() 
#5 /var/www/html/test/lib/internal/Magento/Framework/App/Action/Ac in /var/www/html/test/app/code/Magento/Paypal/Model/Api/Nvp.php on line 15

Note: Is the following patch suitable for my error,

https://magento.com/tech-resources/download#tab-36 (PayPal Express Checkout issue with region patch for Magento 2.3.4)

 

 

 

 

5 REPLIES 5

Re: Magento 2 - What is the proper way to edit core file

Hi @Aveeva 

 

Kindly refer below example code for override model file of vendor:
[Vendor]\[Module]\etc\di.xml

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
        <preference for="Magento\ConfigurableProduct\Model\Customfile" type="[Vendor]\[Module]\Model\Rewrite\Customfile" />     </config>

Create class [Vendor]\[Module]\Model\Rewrite\Customfile

<?php
    namespace [Vendor]\[Module]\Model\Rewrite;
 
    use Magento\Catalog\Api\ProductRepositoryInterface;
    use Magento\Framework\Pricing\Helper\Data;
 
    class CustomFile extends \Magento\ConfigurableProduct\Model\Customfile
    {
        private $productRepository;
        private $priceHelper;
 
        public function __construct(
            ProductRepositoryInterface $productRepository,
            Data $priceHelper
        )
        {
            $this->productRepository = $productRepository;
            $this->priceHelper = $priceHelper;
 
        }
 
        protected function getAttributeOptionsData($attribute, $config)
        {
           /*Add your code and update functionality according to your requirement.*/
        }
    }

It may help you!
Problem Solved ? Please click on 'Kudos' & Accept as Solution!

Problem solved? Click Accept as Solution!

Re: Magento 2 - What is the proper way to edit core file

@Bhanu Periwal  First thank your your your help, i have few doubts in this,

 

1) My Nvp.php path is app/code/Magento/Paypal/Model/Api/Nvp.php not under vendor location, shall i proceed?

 

2) Shall i paste the entire code into the following function,

 protected function getAttributeOptionsData($attribute, $config)

or help pls?

 

 

Re: Magento 2 - What is the proper way to edit core file

@Bhanu Periwal My file app/code/Magento/Paypal/Model/Api/Nvp.php

 

Code : https://paste.ofcode.org/3TGVGN4kij4JEau5nVdqW8 

 

Where i can put and update the code?

Re: Magento 2 - What is the proper way to edit core file

@Bhanu Periwal After enabling the module, if i try to add a product to add-to-cart i am getting following error,
Invalid block type: Magento\Paypal\Block\Express\InContext\Minicart\SmartButton
How to solve the error?

Re: Magento 2 - What is the proper way to edit core file

@Aveeva ,

 

try https://mage2gen.com/

 

it has the following features and information you need::  

  • Api
  • Block
  • Cache
  • Category Attribute
  • Company Attribute (Magento Commerce)
  • Component
  • Configuration Type
  • Console Command
  • Controller
  • Crongroup
  • Cronjob
  • Customer Attribute
  • Customer(Section) Data
  • EAV Entity
  • EAV Attribute (custom)
  • GraphQl Endpoint
  • GraphQl Url Locator
  • Helper
  • Language
  • Model
  • Observer / Event
  • Payment Method
  • Plugin
  • Preference / Rewrite
  • Product Attribute
  • Product Type
  • Sales Attribute
  • Shipping Method
  • System / Config / Setting
  • Unit Test
  • Viewmodel
  • Widget

to override the block class you can use "Preference / Rewrite"

in this, you need to enter the class name which you want to override.

 

kindly Accept as a Solution! if this solves your issue and give Kudos!