Skip to content

Deprecated getStoreMethods() throws exception - possible solution #93

@stas-abra

Description

@stas-abra

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;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions