4

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>
  • 1
    Simply override the block. You can achieve this with help of plugin also. – Dinesh Yadav Jun 7 '17 at 12:19
  • processPaymentConfiguration is private method. So you don't modify even if you overwrite LayoutProcessor class. Create a plugin for LayoutProcessor::process, then you need to modify layout payment config array. – Sohel Rana Jun 7 '17 at 15:21
  • @SohelRana Why should I modify an array, this doesn't allow me to change a method. Am I missing something? – creativename Jun 8 '17 at 7:42
  • @DineshYadav I overrided the block, turns out I forgot to update my di.xml, but it still does not work. I've updated my question with my code. – creativename Jun 9 '17 at 9:26
0

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

class LayoutProcessor extends \Magento\Checkout\Block\Checkout\LayoutProcessor
0
  1. Create your own module if you do not have created it yet.
  2. Implement \Magento\Checkout\Block\Checkout\LayoutProcessorInterface::process in your module through YourModule/etc/frontend/di.xml. Check implementation in \Magento\Checkout\Block\Checkout\LayoutProcessor::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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.