1

I have this code in my addtocart :

 <?php
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
    $productTypeInstance = $product->getTypeInstance();
    if ($product->getTypeId() == \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) {
        $usedProducts = $productTypeInstance->getUsedProducts($product);
        ?>
        <ul id="list-nbr-possible">
            <?php foreach ($usedProducts  as $child) { ?>
                <li class="myli" id="div<?php echo $child->getId()?>" style="display:none;list-style:none;color:green">
                    <?php
                    $productStockObj = $objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface')->getStockItem($child->getId());
                    $pièces=$child->getCustomAttributes("nbre_pieces_par_caisse");
                    $productData= $objectManager->get('Magento\Catalog\Model\Product')->load($child->getId());
                    $nb_pc= $productData->getData('nbre_pieces_par_caisse');
                    $msg  = "Nombre de pièces totales: <pan class='qty_selected'>1</pan> X <pan class='nbre_pieces'>$nb_pc</pan> égale à <pan class='nbre_pieces_tot'>$nb_pc</pan>";
                    echo $msg;

                    ?>
                </li>
            <?php } ?>
        </ul>
        <?php
    } else {
        $productStockObj = $objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface')->getStockItem($product->getId());
        echo "We have ".round($productStockObj->getData('qty'))." items in stock!";
    }
    ?>

    <p id="nbr-pieces-tot" style="color:green;display:none;"></p>
    <input id="nbre_pieces_par_caisse"  type="hidden" value="<?php echo  $nb_pc; ?>"/>

and this is my script :

<script type="text/javascript">
requirejs(['jquery','underscore'], function(jQuery,_){
    jQuery(window).load(function(){
        jQuery( ".product-options-wrapper div" ).click(function() {
            selpro();
        });
    });
    function selpro () {
        var selected_options = {};
        jQuery('div.swatch-attribute').each(function(k,v){
            var attribute_id    = jQuery(v).attr('attribute-id');
            var option_selected = jQuery(v).attr('option-selected');
            //console.log(attribute_id, option_selected);
            if(!attribute_id || !option_selected){ return;}
            selected_options[attribute_id] = option_selected;
        });

        var product_id_index = jQuery('[data-role=swatch-options]').data('mageSwatchRenderer').options.jsonConfig.index;
        var found_ids = [];
        //console.log(product_id_index);
        jQuery.each(product_id_index, function(product_id,attributes){
            //console.log(product_id);
            var productIsSelected = function(attributes, selected_options){
                return _.isEqual(attributes, selected_options);
            }
            if(productIsSelected(attributes, selected_options)){
                found_ids.push(product_id);
            }
        });

        console.log(found_ids);

        if (found_ids.length) {
            var selected_product_id = found_ids[0];
            jQuery('.myli').css('display','none');
            var qty_selected = jQuery("input#qty").val();
            var nbre_pieces = jQuery('#div'+selected_product_id+' .nbre_pieces').text();
            nbre_pieces = parseInt(nbre_pieces, 10);
            qty_selected = parseInt(qty_selected, 10);
            var nb_piece_tot = qty_selected * nbre_pieces;
            jQuery('#div'+selected_product_id+' .qty_selected').text(qty_selected);
            jQuery('#div'+selected_product_id+' .nbre_pieces_tot').text(nb_piece_tot);
            jQuery('#div'+selected_product_id).toggle();
        }
    }
});

i want to have all the attributes of options dropbox but here i find that if one is not aswatch the script does not work any help please

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.