Skip to main content
added 2861 characters in body
Source Link
Kristoffer
  • 1.1k
  • 6
  • 7

Good luck!You can change the app/code/StackExchange/PayPalPreference/Model/Api/Nvp.php like this and use storeManager to fetch website id to decide whether to run the code or not for particular websites:

<?php
declare(strict_types=1);

namespace StackExchange\PayPalPreference\Model\Api;

use Magento\Store\Model\StoreManagerInterface;

class Nvp extends \Magento\Paypal\Model\Api\Nvp
{
    /**
     * @var StoreManagerInterface
     */
    private StoreManagerInterface $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() == 2) {
            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']));
            }
        }
    }
}

Good luck!

You can change the app/code/StackExchange/PayPalPreference/Model/Api/Nvp.php like this and use storeManager to fetch website id to decide whether to run the code or not for particular websites:

<?php
declare(strict_types=1);

namespace StackExchange\PayPalPreference\Model\Api;

use Magento\Store\Model\StoreManagerInterface;

class Nvp extends \Magento\Paypal\Model\Api\Nvp
{
    /**
     * @var StoreManagerInterface
     */
    private StoreManagerInterface $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() == 2) {
            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']));
            }
        }
    }
}
Source Link
Kristoffer
  • 1.1k
  • 6
  • 7

In your di.xml create a preference to your own implementation

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

In your own implementation, extend the class you want to change and override only the method in question. You will have to create a constructor matching the parent constructor, it will look something like this:

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

<?php
declare(strict_types=1);

namespace StackExchange\PayPalPreference\Model\Api;

use Magento\Payment\Model\Method\Logger;

class Nvp extends \Magento\Paypal\Model\Api\Nvp
{
    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,
        array $data = []
    ) {
        parent::__construct($customerAddress, $logger, $customLogger, $localeResolver, $regionFactory, $countryFactory, $processableExceptionFactory, $frameworkExceptionFactory, $curlFactory, $data);
    }

    protected function _applyStreetAndRegionWorkarounds(DataObject $address)
    {
        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']));
            }
        }
    }
}

Good luck!