1

I want to customize my bundle product page on magento2 store and want different "add to cart" buttons for all the simple products of that bundle product i.e. if there are 3 products available in a bundle product then there should be 3 "add to cart" buttons each with there respective qty field. Currently I have overided my bundle page "catalog_product_view_type_bundle.xml" with following code

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceContainer name="product.info.main">
        <block class="Magento\Catalog\Block\Product\View" name="customize.button" as="customize_button" 
               template="PHPCuong_Cart::catalog/product/view/customize.phtml" after="product.info.price" />
    </referenceContainer>

</body>

And in the customize.phtml I have used following code

    <?php 
$_product = $block->getProduct();
$__product = $block->getProduct();


    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
     $listBlock = $objectManager->get('\Magento\Catalog\Block\Product\ListProduct');
    $selectionCollection = $_product->getTypeInstance(true)
        ->getSelectionsCollection(
            $_product->getTypeInstance(true)->getOptionsIds($_product),
            $_product
        );
    foreach ($selectionCollection as $product) {
      $addToCartUrl =  $listBlock->getAddToCartUrl($product); ?>

      <?= $block->getChildHtml('', true) ?>

      <form data-role="tocart-form" action="<?php echo $addToCartUrl; ?>" method="post"> 

        <?php 
            echo $block->getBlockHtml('formkey');
            echo $product->getData('name');

        ?>
          <input type="hidden" name="product" value="<?php echo $product->getData('entity_id'); ?>">
          <input type="hidden" name="related_product" id="related-products-field" value="">
          <label class="label" for="qty"><span><?= /* @escapeNotVerified */ __('Qty') ?></span></label>
            <div class="control">
                <input type="number"
                       name="qty"
                       id="qty"
                       value="<?= /* @escapeNotVerified */ $block->getProductDefaultQty() * 1 ?>"
                       title="<?= /* @escapeNotVerified */ __('Qty') ?>"
                       class="input-text qty"
                       data-validate="<?= $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>"
                       />
            </div>
          <input type="hidden" name="selected_configurable_option" value="">
         <button type="submit" title="Add to Cart" id="product-addtocart-button" class="action tocart primary">

           <span>Add to Cart</span>
         </button>
          <?= $block->getChildHtml('', true) ?>
     </form>
<?php
    } ?>

<?php if ($block->isRedirectToCartEnabled()) : ?>
<script type="text/x-magento-init">
    {
        "#product_addtocart_form": {
            "Magento_Catalog/product/view/validation": {
                "radioCheckboxClosest": ".nested"
            }
        }
    }
</script>
<?php else : ?>
<script type="text/x-magento-init">
    {
        "#product_addtocart_form": {
            "Magento_Catalog/js/validate-product": {}
        }
    }
</script>
<?php endif; ?>

Now whenever I am adding any product in the cart then its adding but no data is showing in the mini cart. After checking the local storage in the browser I found that cart data is not creating properly.

Any help will be Appreciated.Thanks

2
  • Although if we go to the shopping cart page then the products show properly.
    – Prashant
    Nov 25, 2018 at 4:31
  • Hi @Prashant Did you get any solution for this, or any reference link. Please share. I want the same Add to cart for each product, instead of select option for bundle product. Jan 24, 2022 at 13:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.