Skip to main content

get bundle product options magento 2

<?php    
    $id = 9;
    
    $_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    
    $product = $_objectManager->get('\Magento\Catalog\Model\Product')->load($id);
    
    $customOptions = $_objectManager->get('Magento\Catalog\Model\Product\Option')->getProductOptionCollection($product);
    
    $productTypeInstance = $_objectManager->get('Magento\ConfigurableProduct\Model\Product\Type\Configurable');
    
    $productAttributeOptions = $productTypeInstance->getConfigurableAttributesAsArray($product);
    
    $typeInstance = $_objectManager->get('Magento\GroupedProduct\Model\Product\Type\Grouped');
    
    $childs = $typeInstance->getAssociatedProducts($product);
    
    $store_id = \Magento\Framework\App\ObjectManager::getInstance()
    ->get(\Magento\Store\Model\StoreManagerInterface::class)
    ->getStore()
    ->getId();
    
    $options = $_objectManager->get('Magento\Bundle\Model\Option')
                        ->getResourceCollection()
                        ->setProductIdFilter($product->getId())
                        ->setPositionOrder();
         $options->joinValues($store_id);
        
    $typeInstance = $_objectManager->get('Magento\Bundle\Model\Product\Type');
    
    $selections = $typeInstance->getSelectionsCollection($typeInstance->getOptionsIds($product), $product);
    $x = 1;
    foreach ( $selections as $selection )
    {
        $y = $x++;
        $selectionArray = [];
            $selectionArray['selection_product_name'] = $selection->getName();
            $selectionArray['selection_product_price'] = $selection->getPrice();
            $selectionArray['selection_product_quantity'] = $selection->getSelectionQty();
            $selectionArray['selection_product_id'] = $selection->getProductId();
            $parentIds = $_objectManager->get('Magento\ConfigurableProduct\Model\Product\Type\Configurable')
            ->getParentIdsByChild($selection->getProductId());
            $parent_id = array_shift($parentIds);
            $selectionArray['parent_id'] = $parent_id;
            $productsArray[$selection->getOptionId()][$selection->getSelectionId()] = $selectionArray;
    }
    
    $store = $_objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore();
    
    foreach ( $productsArray as $key => $productsArrA )
    {
        foreach ( $productsArrA as $key => $productsrrA )
        {
            $productsArrayA[$key] = $productsrrA['selection_product_price'];
        }
    }
    
    $myJSON = json_encode($productsArrayA);
        
    $listBlock = $_objectManager->get('\Magento\Catalog\Block\Product\ListProduct');
    $addToCartUrl = $listBlock->getAddToCartUrl($product);

?>

<!--form data-role="tocart-form" action="<?php echo $addToCartUrl; ?>" method="post">
<?php //echo $block->getBlockHtml('formkey')?>
<button type="submit"
title="Add to Cart"
class="action tocart primary">
<span>Add to Cart</span>
</button>
</form-->



    <div class="section-outfit mt-120">
      <div class="container">
        <div class="row align-items-center">
          <div class="col-md-6">
             <div class="owl-carousel owl-theme outfit-slider">
                <div class="item">
                  <div class="outfit-img">
                    <img src="<?php echo $this->getViewFileUrl('images/outfit.png'); ?>" class="img-fluid" alt="outfit">
                  </div>
                </div>
                <div class="item">
                  <div class="outfit-img">
                    <img src="<?php echo $this->getViewFileUrl('images/outfit.png'); ?>" class="img-fluid" alt="outfit">
                  </div>
                </div>
                <div class="item">
                  <div class="outfit-img">
                    <img src="<?php echo $this->getViewFileUrl('images/outfit.png'); ?>" class="img-fluid" alt="outfit">
                  </div>
                </div>
              </div>
          </div>
          <div class="col-md-6">
            <div class="outfit-box">
              <div class="pb-4 d-flex align-items-center title-bx">
                <h2 class="sec-tilte m-0">Dein Matchdayoutfit</h2>
                <p class="ml-auto">HERBST 2021</p>
              </div>
              <form data-product-sku="Bundle product 1" action="http://magento2.p585981.webspaceconfig.de/checkout/cart/add/product/9/" method="post" id="product_addtocart_form" novalidate="novalidate">
                <?php echo $block->getBlockHtml('formkey')?>
                <input type="hidden" name="product" value="9">
                    <ul>
                        <?php
                            foreach ( $productsArray as $key => $productsArr )
                            {
                                /* echo '<pre>';
                                print_r ( $productsArr );
                                echo '</pre>'; */
                            ?>
                                <li>
                                    <div class="outfit-product">
                                        <div class="outfit-product-inner">
                                            <select name="bundle_option[<?php echo $key;?>]">
                                                <?php
                                                    foreach ( $productsArr as $key => $productsrr )
                                                    {
                                                        $parentss = $productsrr['parent_id'];
                                                ?>
                                                     <option value="<?php echo $key; ?>"><?php echo $productsrr['selection_product_name']; ?></option>
                                                <?php } ?>
                                            </select>
                                            <?php
                                                $product_parent = $_objectManager->create('Magento\Catalog\Model\Product')->load($parentss);
                                            ?>
                                            
                                            <div class="fitproduct-desc">
                                                <h6><?php echo $product_parent->getName();?></h6>
                                                <p><?php echo $product_parent->getShortDescription();?></p>
                                            </div>
                                        </div>
                                        <div class="fitproduct-img">
                                            <img src="<?php echo $imageUrl = $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product' . $product_parent->getImage(); ?>">
                                        </div>
                                        
                                    </div>
                                </li>
                            <?php     
                            }
                        ?>
                    </ul>
                        <div class="homeprice-sec mt-4">
                        <p><span class="del-price"><n>124</n>,00 €</span> <m>115</m>,00 €</p>
                        <input type="hidden" name="bundle_option_qty[3]" value="1" aria-required="true">
                        <input type="hidden" name="bundle_option_qty[4]" value="1" aria-required="true">
                        <input type="hidden" name="bundle_option_qty[5]" value="1" aria-required="true">
                        <input type="hidden" name="qty" id="qty" min="0" value="1" title="Qty" class="input-text qty">
                        <button type="submit" title="Add to Cart" class="action primary tocart" id="product-addtocart-button">
                                <span><img src="<?php echo $this->getViewFileUrl('images/bag-icon.png'); ?>">IN DEN WACKERKORB</span>
                        </button>
                    </div>
              </form>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <script>
        require(['jquery', 'jquery/ui'], function($){
            jQuery(document).ready( function() {
                    var data= '<?php echo $myJSON; ?>';
                    arr = $.parseJSON(data);
                
                    var strt = [];
                    $( ".outfit-product-inner select" ).each(function() {
                        strt.push( parseInt(arr[ $(this).val() ]) );
                    });
                    var strttotal = 0;
                    for (var i = 0; i < strt.length; i++) {
                        strttotal += strt[i] << 0;
                    }
                    $("m").html(strttotal);
                    $("n").html(strttotal+224);
                
                
                
                var all = [];
                $(document).on("change",".outfit-product-inner select",function() {
                    all = [];
                    $( ".outfit-product-inner select" ).each(function() {
                        all.push( parseInt(arr[ $(this).val() ]) );
                    });
                    var total = 0;
                    for (var i = 0; i < all.length; i++) {
                        total += all[i] << 0;
                    }
                    $("m").html(total);
                    $("n").html(total+224);
                });
                
            });
        });
    </script>

Comments

Popular posts from this blog

Magento 2: Category list for custom magento system configuration section ( Backend )

In system.xml file field for multi select of category is like: NOTE: Use Select for Single item and multiselect for multiple in - <field id = "bannerlist" translate = "label" type = " multiselect " <group id = "bannerblock_setting" translate = "label" type = "text" delault = "1" sortOrder = "3" showInDefault = "1" showInWebsite = "1" showInStore = "1" > <label> Setting </label> <field id = "bannerlist" translate = "label" type = "multiselect" sortOrder = "10" showInDefault = "1" showInWebsite = "1" showInStore = "1" > <label> Select Category </label> <!-- <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>--> <source_model> Ipragmatech\Bannerblock\Model\Config\Source\C...

Magento2: How to install custom theme in magento 2

magento2 | _ app | _ design | _ frontend | _ Custom | _theme | _Magento_Theme | _templates | _root . phtml - Copy of Luma | _registration . php | _theme . xml     Your path for registration.php is app\design\frontend\Custom\theme\registration.php   <? php \Magento\Framework\Component\ComponentRegistrar :: register ( \Magento\Framework\Component\ComponentRegistrar :: THEME , 'frontend/Custom/theme' , __DIR__ );       app\design\frontend\Custom\theme\theme . xml   <theme xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation = "urn:magento:framework:Config/etc/theme.xsd" > <title> Custom Theme </title> <parent> Magento/luma </parent> <media> <preview_image> media/preview.jpg </preview_image> </media...