Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecated getStoreMethods() throws exception - possible solution #93

Open
stas-abra opened this issue Aug 26, 2024 · 1 comment
Open

Comments

@stas-abra
Copy link

Greetings,

In class AddCheckoutBillingAddress there is an issue with getStoreMethods() on line 142 that is already in your todo list
( // @todo Visit working around deprecated code)

I encountered this issue today and by using the PaymentMethodListInterface with the store id from StoreManagerInterface it
worked as expected.

Here is the complete class after my code adjustments:

<?php

namespace AddressFinder\AddressFinder\Observer\FormConfig\Frontend;

use AddressFinder\AddressFinder\Model\FormConfigProvider;
use AddressFinder\AddressFinder\Model\StateMappingProvider;
use AddressFinder\AddressFinder\Observer\FormConfig\Base;
use Exception;
use Magento\Framework\Data\Collection;
use Magento\Framework\DataObject;
use Magento\Framework\Exception\LocalizedException;
use Magento\Payment\Helper\Data as PaymentHelper;
use Magento\Payment\Api\PaymentMethodListInterface;
use Magento\Store\Model\StoreManagerInterface;

class AddCheckoutBillingAddress extends Base
{
    public const FORM_ID = 'frontend.checkout.billing.address';

    /** @var PaymentHelper */
    private $paymentHelper;

    protected $paymentMethodInterface;

    protected StoreManagerInterface $storeManager;

    /**
     * Creates a new "Add Checkout Billing Address" observer.
     */
    public function __construct(
        FormConfigProvider $configProvider,
        StateMappingProvider $stateMappingProvider,
        PaymentHelper $paymentHelper,
        PaymentMethodListInterface $paymentMethodInterface,
        StoreManagerInterface $storeManager
    ) {
        parent::__construct($configProvider, $stateMappingProvider);

        $this->paymentHelper = $paymentHelper;
        $this->paymentMethodInterface = $paymentMethodInterface;
        $this->storeManager = $storeManager;
    }

    /**
     * @inheritDoc
     *
     * @throws Exception
     */
    protected function addForm(Collection $forms): void
    {
        foreach ($this->getActivePaymentMethodCodes() as $code) {
            $forms->addItem(new DataObject([
                'id' => sprintf('%s.%s', self::FORM_ID, $code),
                'label' => sprintf('Checkout Billing Address (%s)', $code),
                'layoutSelectors' => [
                    'li#payment',
                    sprintf('div[name="billingAddress%s.street.0"]', $code),
                ],
                'countryIdentifier' => sprintf(
                    'div[name="billingAddress%s.country_id"] select[name=country_id]',
                    $code
                ),
                'searchIdentifier' => sprintf(
                    'div[name="billingAddress%s.street.0"] input[name="street[0]"]',
                    $code
                ),
                'nz' => [
                    'countryValue' => 'NZ',
                    'elements' => [
                        'address1' => sprintf(
                            'div[name="billingAddress%s.street.0"] input[name="street[0]"]',
                            $code
                        ),
                        'address2' => sprintf(
                            'div[name="billingAddress%s.street.1"] input[name="street[1]"]',
                            $code
                        ),
                        'suburb' => sprintf(
                            'div[name="billingAddress%s.street.2"] input[name="street[2]"]',
                            $code
                        ),
                        'city' => sprintf(
                            'div[name="billingAddress%s.city"] input[name=city]',
                            $code
                        ),
                        'region' => sprintf(
                            'div[name="billingAddress%s.region"] input[name=region]',
                            $code
                        ),
                        'postcode' => sprintf(
                            'div[name="billingAddress%s.postcode"] input[name=postcode]',
                            $code
                        ),
                    ],
                    'regionMappings' => null,
                ],
                'au' => [
                    'countryValue' => 'AU',
                    'elements' => [
                        'address1' => sprintf(
                            'div[name="billingAddress%s.street.0"] input[name="street[0]"]',
                            $code
                        ),
                        'address2' => sprintf(
                            'div[name="billingAddress%s.street.1"] input[name="street[1]"]',
                            $code
                        ),
                        'suburb' => sprintf(
                            'div[name="billingAddress%s.city"] input[name=city]',
                            $code
                        ),
                        'state' => $this->getStateMappings('AU')
                            ? sprintf(
                                'div[name="billingAddress%s.region_id"] select[name=region_id]',
                                $code
                            )
                            : sprintf(
                                'div[name="billingAddress%s.region"] input[name=region]',
                                $code
                            ),
                        'postcode' => sprintf(
                            'div[name="billingAddress%s.postcode"] input[name=postcode]',
                            $code
                        ),
                    ],
                    'stateMappings' => $this->getStateMappings('AU'),
                ],
            ]));
        }
    }

    /**
     * Gets active payment method codes.
     *
     * @return string[]
     */
    private function getActivePaymentMethodCodes(): array
    {
        $codes = [];

        // @todo Visit working around deprecated code
//        foreach ($this->paymentHelper->getStoreMethods() as $method) {
//            try {
//                $codes[] = $method->getCode();
//            } catch (LocalizedException $e) {
//            }
//        }

        foreach ($this->paymentMethodInterface->getActiveList($this->storeManager->getStore()->getId()) as $method) {
            try {
                $codes[] = $method->getCode();
            } catch (LocalizedException $e) {
            }
        }

        $codes[] = 'shared';

        return $codes;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
@stas-abra and others