0

I want to disable the quantity box for specific custom products.

i followed this tutorial but it did not work.

in my custom module, i did the following steps.

Vendor/ModuleName/view/frontend/requirejs-config.js

var config = {
            'Magento_Checkout/template/minicart/item/default.html': '[Vendor]_[Module]/template/minicart/item/default.html'
        }

and then i :

Vendor/ModuleName/view/frontend/web/template/minicart/item/default.html

    <div class="product-item-details">
        <strong class="product-item-name">
            <!-- ko if: product_has_url -->
            <a data-bind="attr: {href: product_url}, html: product_name"></a>
            <!-- /ko -->
            <!-- ko ifnot: product_has_url -->
                <!-- ko text: product_name --><!-- /ko -->
            <!-- /ko -->
        </strong>

        <!-- ko if: options.length -->
        <div class="product options" data-mage-init='{"collapsible":{"openedState": "active", "saveState": false}}'>
            <span data-role="title" class="toggle"><!-- ko i18n: 'See Details' --><!-- /ko --></span>

            <div data-role="content" class="content">
                <strong class="subtitle"><!-- ko i18n: 'Options Details' --><!-- /ko --></strong>
                <dl class="product options list">
                    <!-- ko foreach: { data: options, as: 'option' } -->
                    <dt class="label"><!-- ko text: option.label --><!-- /ko --></dt>
                    <dd class="values">
                        <!-- ko if: Array.isArray(option.value) -->
                            <span data-bind="html: option.value.join('<br>')"></span>
                        <!-- /ko -->
                        <!-- ko ifnot: Array.isArray(option.value) -->
                            <span data-bind="html: option.value"></span>
                        <!-- /ko -->
                    </dd>
                    <!-- /ko -->
                </dl>
            </div>
        </div>
        <!-- /ko -->

        <div class="product-item-pricing">
            <!-- ko if: canApplyMsrp -->

            <div class="details-map">
                <span class="label" data-bind="i18n: 'Price'"></span>
                <span class="value" data-bind="i18n: 'See price before order confirmation.'"></span>
            </div>
            <!-- /ko -->
            <!-- ko ifnot: canApplyMsrp -->
            <!-- ko foreach: $parent.getRegion('priceSidebar') -->
                <!-- ko template: {name: getTemplate(), data: item.product_price, as: 'price'} --><!-- /ko -->
            <!-- /ko -->
            <!-- /ko -->

            <div class="details-qty qty">
                <label class="label" data-bind="i18n: 'Qty', attr: {
                       for: 'cart-item-'+item_id+'-qty'}"></label>
                <input data-bind="attr: {
                       id: 'cart-item-'+item_id+'-qty',
                       'data-cart-item': item_id,
                       'data-item-qty': qty,
                       'data-cart-item-id': product_sku
                       }, value: qty, enable: isCustomizedEnabledQtyBox"
                       type="number"
                       size="4"
                       class="item-qty cart-item-qty">
                <button data-bind="attr: {
                       id: 'update-cart-item-'+item_id,
                       'data-cart-item': item_id,
                       title: $t('Update')
                       }"
                        class="update-cart-item"
                        style="display: none">
                    <span data-bind="i18n: 'Update'"></span>
                </button>
            </div>
        </div>

        <div class="product actions">
            <!-- ko if: is_visible_in_site_visibility -->
            <div class="primary">
                <a data-bind="attr: {href: configure_url, title: $t('Edit item')}" class="action edit">
                    <span data-bind="i18n: 'Edit'"></span>
                </a>
            </div>
            <!-- /ko -->
            <div class="secondary">
                <a href="#" data-bind="attr: {'data-cart-item': item_id, title: $t('Remove item')}"
                   class="action delete">
                    <span data-bind="i18n: 'Remove'"></span>
                </a>
            </div>
        </div>
    </div>
</div>

then my di

Vendor/ModuleName/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\CustomerData\DefaultItem" type="[Vendor]\[Module]\Plugin\Magento\Checkout\CustomerData\DefaultItem" />
</config>

Vendor/ModuleName/Plugin/Magento/Checkout/CustomerData/DefaultItem.php

namespace [Vendor]\[Module]\Plugin\Magento\Checkout\CustomerData;

class DefaultItem extends \Magento\Checkout\CustomerData\DefaultItem
{
    protected function doGetItemData()
    {
        $result = parent::doGetItemData();
        $result['isCustomizedEnabledQtyBox'] = $this->isQtyBoxEnabled($this->item->getProduct()->getSku());
        return $result;
    }

    protected function isQtyBoxEnabled($sku)
    {
        $disabled = ['24-MB02'];
        if(!in_array($sku, $disabled))
            return 1;
        else
            return 0;
    }
}

This is my disabled virables:

 protected function isQtyBoxEnabled($sku)
    {
        $disabled = ['ChateauUndertheStars-1-DomeByLand-30','ChateauUndertheStars-1-DomeBySea-17'];
        if(!in_array($sku, $disabled))
            return 1;
        else
            return 0;
    }
2
  • What is the error you got in console? Nov 1, 2019 at 13:33
  • hi @Ranganathan. thanks for response. i am not getting any errors in the console
    – theSeeker
    Nov 1, 2019 at 14:21

1 Answer 1

0

Found an issue in this code, Please replace requirejs-config.js as below

var config = {
    map: {
        "*": {
            'Magento_Checkout/template/minicart/item/default.html': '[Vendor]_[Module]/template/minicart/item/default.html'
        }
    }
};

And Run below commands

php bin/magento setup:upgrade
php bin/magento setup:static-content-deploy -f 

This is worked in my Magento 2.3.2 env

14
  • hi. it did not work. no idea where i have gone wrong
    – theSeeker
    Nov 4, 2019 at 11:20
  • What is your sku that you restricted? Nov 4, 2019 at 12:18
  • hi. its ChateauUndertheStars-1-DomeByLand-30
    – theSeeker
    Nov 4, 2019 at 12:44
  • Did you added these skus in $disabled variable? Nov 4, 2019 at 13:07
  • hi. yes i did. i have amended my original question to how the disabled variables
    – theSeeker
    Nov 4, 2019 at 13:10

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.