0

I want to disable a UI component conditionally in the cart sidebar. I could do it as follows:

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="minicart">
            <arguments>
                <argument name="jsLayout" xsi:type="array">
                    <item name="components" xsi:type="array">
                        <item name="minicart_content" xsi:type="array">
                            <item name="children" xsi:type="array">
                                <item name="item.renderer" xsi:type="array">
                                    <item name="children" xsi:type="array">
                                        <item name="checkout.cart.item.price.sidebar" xsi:type="array">
                                            <item name="config" xsi:type="array">
                                                <item name="componentDisabled" xsi:type="boolean">true</item>
                                            </item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                        </item>
                    </item>
                </argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

But that isn't condittionally.

1

The solution is using a JsLayoutProvider (or you can also add a layouthandle and disable components in the layout xml). Here you can adjust the parsed layout xml files as an multidim array.

First you need a class which implements the Magento\Customer\CustomerData\JsLayoutDataProviderInterface class. In this class you need a getData method which returns the array of layout you want to adjust.

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Vendor\Module\CustomerData;

use Magento\Customer\CustomerData\JsLayoutDataProviderInterface;
use Vendor\Module\Helper\Data;

/**
 * Checkout totals js layout data provider
 */
class HidePricesJsLayoutDataProvider implements JsLayoutDataProviderInterface
{

    /**
     * {@inheritdoc}
     */
    public function getData()
    {
        $hidePrices = $this->hidepricesHelper->getHidePrices();
        return [
            'components' => [
                'minicart_content' => [
                    'children' => [
                        'item.renderer' => [
                            'children' => [
                                'checkout.cart.item.price.sidebar' => [
                                    'config' => [
                                        'componentDisabled' => true
                                    ],
                                ],
                            ],
                        ],
                    ],
                ],
            ],
        ];
    }
}

Then you need to declare a virtualtype in your frontend/di.xml as follows:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Customer\CustomerData\JsLayoutDataProviderPoolInterface">
        <arguments>
            <argument name="jsLayoutDataProviders" xsi:type="array">
                <item name="hide_prices" xsi:type="object">Vendor\Module\CustomerData\HidePricesJsLayoutDataProvider</item>
            </argument>
        </arguments>
    </type>
</config>

Now your JsLayoutDataProvider will be added & merged to the layout xml data in the constructor of Magento\Checkout\Block\Cart\Sidebar

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Customer\Model\Session $customerSession,
    \Magento\Checkout\Model\Session $checkoutSession,
    \Magento\Catalog\Helper\Image $imageHelper,
    \Magento\Customer\CustomerData\JsLayoutDataProviderPoolInterface $jsLayoutDataProvider,
    array $data = [],
    \Magento\Framework\Serialize\Serializer\Json $serializer = null
) {
    if (isset($data['jsLayout'])) {
        $jsLayoutData=$jsLayoutDataProvider->getData();
        $this->jsLayout = $this->array_merge_recursive_distinct($jsLayoutData, $data['jsLayout']);
        unset($data['jsLayout']);
    } else {
        $this->jsLayout = $jsLayoutDataProvider->getData();
    }

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.