Magento – Override method in LayoutProcessor

checkoutmagento2overridespayment-methods

I want to make changes(PHP code) to the method processPaymentConfiguration, but I don't know how I can override the method so my changes have a higher priority. I can alter the LayoutProcessor file but it's never good to apply changes to a core file.

So my question; is this possible and what do I need to do to achieve this?

Vendor/Module/Block/Checkout/LayoutProcessor.php

<?php
namespace Vendor\Module\Block\Checkout;

use Magento\Framework\App\ObjectManager;
use Magento\Checkout\Block\Checkout\LayoutProcessorInterface;
use Magento\Customer\Model\AttributeMetadataDataProvider;
use Magento\Ui\Component\Form\AttributeMapper;
use Magento\Checkout\Block\Checkout\AttributeMerger;
use Magento\Customer\Model\Options;

class LayoutProcessor implements LayoutProcessorInterface
{
    private $attributeMetadataDataProvider;
    protected $attributeMapper;
    protected $merger;
    private $options;

    public function __construct(
        AttributeMetadataDataProvider $attributeMetadataDataProvider,
        AttributeMapper $attributeMapper,
        AttributeMerger $merger
    ) {
        $this->attributeMetadataDataProvider = $attributeMetadataDataProvider;
        $this->attributeMapper               = $attributeMapper;
        $this->merger                        = $merger;
    }

    private function getOptions()
    {
        //same code as Magento/Checkout/Block/LayoutProcessor::getOptions()
    }


    private function getAddressAttributes()
    {
        //same code as Magento/Checkout/Block/LayoutProcessor::getAddressAttributes()
    }

    private function convertElementsToSelect($elements, $attributesToConvert)
    {
        //same code as Magento/Checkout/Block/LayoutProcessor::convertElementsToSelect()
    }

    public function process($jsLayout)
    {
        //same code as Magento/Checkout/Block/LayoutProcessor::process()
    }

    private function processPaymentConfiguration(array &$configuration, array $elements)
    {
        /*
        code from Magento/Checkout/Block/LayoutProcessor::processPaymentConfiguration()
        with a couple changes.
        It works when I apply the changes in code file (vendor/magento/magento-checkout/...)
        */
    }
}

When I var_dump processPaymentConfigurationfrom the core and my custom code, I get the same results. So it looks like the code executes and returns the right thing but nothing is happening.

Vendor/Module/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\Checkout\Block\Checkout\LayoutProcessor" type="Vendor\Module\Block\Checkout\LayoutProcessor" />
</config>

Best Answer

you have to "extend" the class not "implement" interface..

class LayoutProcessor extends \Magento\Checkout\Block\Checkout\LayoutProcessor
Related Topic