Get All simple product from a Configurable Product in Magento Product View

后端 未结 6 1547
轮回少年
轮回少年 2021-01-31 09:55

How can I get all the simple products associated with a configurable product? I found how to do the opposite (get a product configurable from a simple product) but that\'s not w

相关标签:
6条回答
  • 2021-01-31 10:03

    A configurable product can have multiple other products associated to it.

    Here is the code to fetch all the children products that are associated with a configurable product.

    Here goes the code :)

    /**
     * Load product by product id
     */
    $product = Mage::getModel('catalog/product')->load(YOUR_PRODUCT_ID);
    
    /**
     * Get child products id and such (only ids)
     */
    $childIds = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($product->getId());
    
    /**
     * Get children products (all associated children products data)
     */
    $childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$product);
    

    Source: http://blog.chapagain.com.np/magento-how-to-get-all-associated-children-product-of-a-configurable-product/

    0 讨论(0)
  • 2021-01-31 10:07

    write the below simplest code in block and just call it in your template file to get the products associated products :

        $productId = (int)$this->getRequest()->getParam('id');
        $objectManager = \Magento\Framework\App\objectManager::getInstance();
        $product = $objectManager->create("\Magento\Catalog\Model\Product")->load($productId);
        $collection = $product->getTypeInstance()->getAssociatedProducts($product);
        $this->setCollection($collection);
    

    now, in template write the below code to print the names :

       $collection = $block->getCollection(); 
       foreach ($collection as $key => $value) 
       {
          echo $value->getName();
          echo "<br>";
       }
    
    0 讨论(0)
  • 2021-01-31 10:09

    Use this below code

    Code to get the the full product information (where 3 is the configurable product Id)

    $product = Mage::getModel('catalog/product')->load(3); 
    $childProducts = Mage::getModel('catalog/product_type_configurable')
                        ->getUsedProducts(null,$product);
    
    foreach($childProducts as $child) {
        print_r($child->getName());  // You can use any of the magic get functions on this object to get the value
    }
    

    Another code to get the Children Product Ids

    $childProducts = Mage::getModel('catalog/product_type_configurable')
                        ->getChildrenIds(3);
    

    Hope this helps!!

    0 讨论(0)
  • 2021-01-31 10:15

    For anyone looking to do this, and display the results I will share what I did to finish it off

    Add to the script segment of: app/design/frontend/default/[your_theme]/template/catalog/product/view/type/options/configurable.phtml

    id = {};
    <?php 
    foreach ($_product->getTypeInstance(true)->getUsedProducts ( null, $_product) as $simple) {
        $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($simple)->getQty();
        echo "  id[" . $simple->getId() . "] = $stock;\n\r";
    }
    ?>
    
        spConfig.getIdOfSelectedProduct = function () {
            var existingProducts = new Object();
            for (var i = this.settings.length - 1; i >= 0; i--) {
                var selected = this.settings[i].options[this.settings[i].selectedIndex];
                if (selected.config) {
                    for (var iproducts = 0; iproducts < selected.config.products.length; iproducts++) {
                        var usedAsKey = selected.config.products[iproducts] + "";
                        if (existingProducts[usedAsKey] == undefined) {
                            existingProducts[usedAsKey] = 1;
                        } else {
                            existingProducts[usedAsKey] = existingProducts[usedAsKey] + 1;
                        }
                    }
                }
            }
            for (var keyValue in existingProducts) {
                for (var keyValueInner in existingProducts) {
                    if (Number(existingProducts[keyValueInner]) < Number(existingProducts[keyValue])) {
                        delete existingProducts[keyValueInner];
                    }
                }
            }
            var sizeOfExistingProducts = 0;
            var currentSimpleProductId = "";
            for (var keyValue in existingProducts) {
                currentSimpleProductId = keyValue;
                sizeOfExistingProducts = sizeOfExistingProducts + 1
            }
            if (sizeOfExistingProducts == 1) {
               var qtyLeft = id[currentSimpleProductId];
               if(qtyLeft >= 1) {
                   jQuery('.availability-only').html('Only ' + qtyLeft + ' available.');
                   jQuery('p.out-of-stock').removeClass('out-of-stock').addClass('in-stock');
                   jQuery('p.in-stock > span').html('In stock');
               } else {
                   jQuery('.availability-only').html('Sorry, there are none available in this size.');
                   jQuery('p.in-stock').removeClass('in-stock').addClass('out-of-stock');
                   jQuery('p.out-of-stock > span').html('Out of stock');
               }
            }
        }
    

    in the select of the same page add:

     onchange = "spConfig.getIdOfSelectedProduct()"
    

    Feel free to edit what the statement prints, but this should get you there. It also accounts for 0 stock on hand, changing it to Out of stock in css and text

    0 讨论(0)
  • 2021-01-31 10:17

    I got it. Thanks for the replies.

    <?php if($_product->getTypeId() == "configurable"): ?>
        <?php $_configurable = $_product->getTypeInstance()->getUsedProductIds(); ?>
        <?php foreach ($_configurable as $_config): ?>
            <?php $_simpleproduct = Mage::getModel('catalog/product')->load($_config); ?>
            <?php //Magic php with a $_simpleproduct. ?>
        <?php endforeach; ?>
    <?php endif; ?>
    
    0 讨论(0)
  • 2021-01-31 10:19

    use the following script in

    app/design/frontend/default/[your theme]/template/catalog/product/view/type/options/configurable.phtml

    Inside the script:

    spConfig.getIdOfSelectedProduct = function () {
        var existingProducts = new Object();
        for (var i = this.settings.length - 1; i >= 0; i--) {
            var selected = this.settings[i].options[this.settings[i].selectedIndex];
            if (selected.config) {
                for (var iproducts = 0; iproducts < selected.config.products.length; iproducts++) {
                    var usedAsKey = selected.config.products[iproducts] + "";
                    if (existingProducts[usedAsKey] == undefined) {
                        existingProducts[usedAsKey] = 1;
                    } else {
                        existingProducts[usedAsKey] = existingProducts[usedAsKey] + 1;
                    }
                }
            }
        }
        for (var keyValue in existingProducts) {
            for (var keyValueInner in existingProducts) {
                if (Number(existingProducts[keyValueInner]) < Number(existingProducts[keyValue])) {
                    delete existingProducts[keyValueInner];
                }
            }
        }
        var sizeOfExistingProducts = 0;
        var currentSimpleProductId = "";
        for (var keyValue in existingProducts) {
            currentSimpleProductId = keyValue;
            sizeOfExistingProducts = sizeOfExistingProducts + 1
        }
        if (sizeOfExistingProducts == 1) {
            alert("Selected product is: " + currentSimpleProductId)
        }
    }
    

    Now add onchange event to your dropdown in same page :

    onchange = "spConfig.getIdOfSelectedProduct()"
    

    Full description

    0 讨论(0)
提交回复
热议问题