Skip to main content
3 of 3
Added More Information
zus
  • 83
  • 2
  • 18
  • 82

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? Module or Preference or anything else?

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)

zus
  • 83
  • 2
  • 18
  • 82