0

How I can change my configurable minimum sale quantity in the qty box dynamically when one option is selected, right now the product name, description, image is changed, I need to do the same with the minimum sale quantity in the qty box.

/app/code/Custom/CPdata/Block/ConfigurableProduct/Product/View/Type

 namespace Custom\CPdata\Block\ConfigurableProduct\Product\View\Type;

 use Magento\Framework\Json\EncoderInterface;
 use Magento\Framework\Json\DecoderInterface;

 class Configurable

{

protected $jsonEncoder;

protected $jsonDecoder;

protected $_productRepository;
protected $stockRegistry;


public function __construct(

        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,

           \Magento\Backend\Block\Template\Context $context,        
     Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,

        EncoderInterface $jsonEncoder,

        DecoderInterface $jsonDecoder


) {

    $this->jsonDecoder = $jsonDecoder;

    $this->jsonEncoder = $jsonEncoder;

    $this->_productRepository = $productRepository;
      $this->stockRegistry = $stockRegistry;


}



public function getProductById($id)

{

    return $this->_productRepository->getById($id);

}




public function aroundGetJsonConfig(

    \Magento\ConfigurableProduct\Block\Product\View\Type\Configurable $subject,

    \Closure $proceed

)

{

    $sname = [];

    $sdescription = [];

    $quantities = [];


    $config = $proceed();

    $config = $this->jsonDecoder->decode($config);



    foreach ($subject->getAllowProducts() as $prod) {

        $id = $prod->getId();

        $product = $this->getProductById($id);

        $sname[$id] = $product->getName();

        $sdescription[$id] = $product->getDescription();

           // $_productStock = $this->getStockItem($id);

        //$quantities[$id] = $_productStock->getMinSaleQty();


         $stockitem = $this->stockRegistry->getStockItem(
            $prod->getId(),
            $prod->getStore()->getWebsiteId()
        );
        $quantities[$prod->getId()] = $stockitem->getMinSaleQty();



          }



    $config['sname'] = $sname;

    $config['sdescription'] = $sdescription;
    $config['quantities'] = $quantities;

    return $this->jsonEncoder->encode($config);

    }

    }

/app/code/Custom/CPdata/view/frontend/web/js

      define([
     'jquery',
     'jquery/ui',
    'magento-swatch.renderer'
     ], function($){

     $.widget('custom.SwatchRenderer', $.mage.SwatchRenderer, { 

    /**
     * Event for swatch options
     *
     * @param {Object} $this
     * @param {Object} $widget
     * @private
     */
    _OnClick: function ($this, $widget) {
        var $parent = $this.parents('.' + $widget.options.classes.attributeClass),
            $wrapper = $this.parents('.' + $widget.options.classes.attributeOptionsWrapper),
            $label = $parent.find('.' + $widget.options.classes.attributeSelectedOptionLabelClass),
            attributeId = $parent.attr('attribute-id'),
            $input = $parent.find('.' + $widget.options.classes.attributeInput);

        if ($widget.inProductList) {
            $input = $widget.productForm.find(
                '.' + $widget.options.classes.attributeInput + '[name="super_attribute[' + attributeId + ']"]'
            );
        }

        if ($this.hasClass('disabled')) {
            return;
        }

        if ($this.hasClass('selected')) {
            $parent.removeAttr('option-selected').find('.selected').removeClass('selected');
            $input.val('');
            $label.text('');
            $this.attr('aria-checked', false);
        } else {
            $parent.attr('option-selected', $this.attr('option-id')).find('.selected').removeClass('selected');
            $label.text($this.attr('option-label'));
            $input.val($this.attr('option-id'));
            $input.attr('data-attr-name', this._getAttributeCodeById(attributeId));
            $this.addClass('selected');
            $widget._toggleCheckedAttributes($this, $wrapper);
        }

        $widget._Rebuild();

        // Custom Code starts
        var iname = $widget.options.jsonConfig.sname[this.getProduct()];
        var idescription = $widget.options.jsonConfig.sdescription[this.getProduct()];
         var iquantities = $widget.options.jsonConfig.quantities[this.getProduct()];

        if(idescription != ''){
            $('[data-role="content"]').find('.description .value').html(idescription);
        }
        if(iname != ''){
            $('[data-ui-id="page-title-wrapper"]').html(iname);
        }
         if(iquantities != ''){
            $( "input[type=number][name=qty].qty-default" ).val(iquantities);
        }

        // Custom code ends

        if ($widget.element.parents($widget.options.selectorProduct)
                .find(this.options.selectorProductPrice).is(':data(mage-priceBox)')
        ) {
            $widget._UpdatePrice();
        }

        $widget._loadMedia();
        $input.trigger('change');
    }

});

   return $.custom.SwatchRenderer;
    });
  • This code is working but because of that our category product is not showing. – Developer Account Jun 7 '18 at 7:41
  • The issue is now resolved. – Developer Account Jun 8 '18 at 5:54

Your Answer

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

Browse other questions tagged or ask your own question.