0

I want to show/hide "Estimate Shipping costs and Tax" based on customer group.

I just do not write to css that hide block as it is bad practice. Is there any way we can disable component based on condition or remove block on xml based?

Thank you

1

Take a look, at Magento 2 remove "Estimate shipping costs and tax" from cart

If you can implement layout concept to the Block class then you get can add the condition.

checkout.cart.shipping is the layout name of the block class \Magento\Checkout\Block\Cart\Shipping and getJsLayout function/method is responsible for render of Estimate Shipping costs and Tax section.

If you will add able to <item name="componentDisabled" xsi:type="boolean">true</item> to the output of getJsLayout then you will get solutions.

And at block class, we can get the current user group code by \Magento\Framework\App\Http\Context::getValue

So, just need to add create after plugin on \Magento\Checkout\Block\Cart\Shipping::getJsLayout then put of customer grou checking then condition at here,

<?php

namespace StackExchange\Magento\Plugin\Magento\Checkout;

use Magento\Customer\Model\Context;

class Shipping {

    /**
     * @var \Magento\Framework\App\Http\Context
     */
    private $httpContext;

    public function __construct(
        \Magento\Framework\App\Http\Context $httpContext     
     ) {

         $this->httpContext = $httpContext;
    }
    public function afterGetJsLayout(
            \Magento\Checkout\Block\Cart\Shipping $subject,
            $result
    ) {

        $allowedCustomgroupIds  = [0,5,2];

        $currrentCustomGruopId = $this->httpContext->getValue(Context::CONTEXT_GROUP);

        $result = json_decode($result, true);

        //  if customer group DOES NOT match then donot show "Estimate Shipping costs and Tax"

        if (!in_array($currrentCustomGruopId , $allowedCustomgroupIds) && isset($result['components']['block-summary'])) {
            //Disable Estimate Shipping costs and Tax 
            $result['components']['block-summary']['config']['componentDisabled'] = true;
        }

        return json_encode($result, JSON_HEX_TAG);
    }

}

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.