cancel
Showing results for 
Search instead for 
Did you mean: 

What is the error in this Preference?

What is the error in this Preference?

Due to PayPal Error, i am updating the following function in app/code/Magento/Paypal/Model/Api/Nvp.php, not edit the entire code just update a single function.  Function Name :  function _applyStreetAndRegionWorkarounds()

 

My Preference:

 

app/code/Gta/PayPalPreference/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Gta_PayPalPreference',
    __DIR__
);

app/code/Gta/PayPalPreference/etc/module.xml

<?xml version="1.0"?>
	<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    	<module name="Gta_PayPalPreference" schema_version="0.0.1" setup_version="0.0.1"></module>
</config>

app/code/Gta/PayPalPreference/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\Paypal\Model\Api\Nvp"
     type="Gta\PayPalPreference\Model\Api\Nvp"
 />
</config>

app/code/Gta/PayPalPreference/Model/Api/Nvp.php

<?php
declare(strict_types=1);

namespace Gta\PayPalPreference\Model\Api;

use Magento\Store\Model\StoreManagerInterface;

class Nvp extends \Magento\Paypal\Model\Api\Nvp
{
    /**
     * @var StoreManagerInterface
     */
    private $storeManager;

    public function __construct(
        \Magento\Customer\Helper\Address $customerAddress,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Payment\Model\Method\Logger $customLogger,
        \Magento\Framework\Locale\ResolverInterface $localeResolver,
        \Magento\Directory\Model\RegionFactory $regionFactory,
        \Magento\Directory\Model\CountryFactory $countryFactory,
        \Magento\Paypal\Model\Api\ProcessableExceptionFactory $processableExceptionFactory,
        \Magento\Framework\Exception\LocalizedExceptionFactory $frameworkExceptionFactory,
        \Magento\Framework\HTTP\Adapter\CurlFactory $curlFactory,
        StoreManagerInterface $storeManager,
        array $data = []
    ) {
        parent::__construct($customerAddress, $logger, $customLogger, $localeResolver, $regionFactory, $countryFactory, $processableExceptionFactory, $frameworkExceptionFactory, $curlFactory, $data);

        $this->storeManager = $storeManager;
    }

    protected function _applyStreetAndRegionWorkarounds(DataObject $address)
    {
        // e.g. if you do not want to run the code on website id = 2
        if ($this->storeManager->getWebsite()->getId() == 1) {
            parent::_applyStreetAndRegionWorkarounds($address);
            return;
        }

        if ($address->getData('street2') !== null) {
            $address->setStreet(implode("\n", [$address->getData('street'), $address->getData('street2')]));
            $address->unsetData('street2');
        }
        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']));
            }
        }
    }
}

I am getting the following error during the compile command,

 

Warning: Declaration of Gta\PayPalPreference\Model\Api\Nvp::_applyStreetAndRegionWorkarounds(Gta\PayPalPreference\Model\Api\DataObject $address) should be compatib
le with Magento\Paypal\Model\Api\Nvp::_applyStreetAndRegionWorkarounds(Magento\Framework\DataObject $address) in /var/www/html/test/app/code/Gta/PayPalPreferenc
e/Model/Api/Nvp.php on line 59

 

How to solve this error?

 

 

 

 

3 REPLIES 3

Re: What is the error in this Preference?

@Aveeva 

you need to add

use Magento\Framework\DataObject;

in your Gta\PayPalPreference\Model\Api\Nvp class file. 

 

kindly Accept as Solution if this works for you and give Kudos Smiley Happy 
 

Re: What is the error in this Preference?

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

Re: What is the error in this Preference?

@Aveeva ,

kindly check if you have overridden SmartButton class somewhere in your module or any 3rd party module, if YES check by removing/disabling that. 

or try disabling PayPal express shortcut button from the product page for a quick fix.