TransWikia.com

Override method in LayoutProcessor

Magento Asked by creativename on November 25, 2021

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

use MagentoFrameworkAppObjectManager;
use MagentoCheckoutBlockCheckoutLayoutProcessorInterface;
use MagentoCustomerModelAttributeMetadataDataProvider;
use MagentoUiComponentFormAttributeMapper;
use MagentoCheckoutBlockCheckoutAttributeMerger;
use MagentoCustomerModelOptions;

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="MagentoCheckoutBlockCheckoutLayoutProcessor" type="VendorModuleBlockCheckoutLayoutProcessor" />
</config>

2 Answers

  1. Create your own module if you do not have created it yet.
  2. Implement MagentoCheckoutBlockCheckoutLayoutProcessorInterface::process in your module through YourModule/etc/frontend/di.xml. Check implementation in MagentoCheckoutBlockCheckoutLayoutProcessor::process as a reference

  3. In your new implementation of LayoutProcessorInterface::process method get all payment methods by

    $paymentList = $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['payments-list']['children'] ?? null;

  4. Iterate through this payment list and modify layout configuration as needed.

  5. Return modified $jsLayout from your implementation of LayoutProcessorInterface::process method.

Answered by transversus on November 25, 2021

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

class LayoutProcessor extends MagentoCheckoutBlockCheckoutLayoutProcessor

Answered by Naveed Asim on November 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP