首页 文章

从Magento产品视图中的可配置产品中获取所有简单产品

提问于
浏览
22

如何获得与可配置产品相关的所有简单产品?我发现如何做相反的事情(从简单的产品中获得可配置的产品),但这不是我需要的 .

我想显示我所选产品的库存量(可配置属性) . 我最初的想法是打印所有数量的库存并使用jQuery控制显示 . 任何的想法?

5 回答

  • 53

    使用以下脚本

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

    在脚本内:

    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)
        }
    }
    

    现在将 onchange 事件添加到同一页面的下拉列表中:

    onchange = "spConfig.getIdOfSelectedProduct()"
    

    Full description

  • 5

    使用以下代码

    用于获取完整产品信息的代码(其中3是可配置产品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
    }
    

    获得儿童产品ID的另一个代码

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

    希望这可以帮助!!

  • 11

    可配置产品可以包含多个与之关联的其他产品 .

    以下是获取与可配置产品关联的所有子产品的代码 .

    这里是代码:)

    /**
     * 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);
    

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

  • 1

    我知道了 . 谢谢你的回复 .

    <?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; ?>
    
  • 17

    对于任何想要这样做的人,并显示结果,我将分享我所做的一切,以完成它

    添加到 script 段: 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');
               }
            }
        }
    

    在同一页面的 select 中添加:

    onchange = "spConfig.getIdOfSelectedProduct()"
    

    随意编辑语句打印的内容,但这应该可以帮助您 . 它还占有0库存,在css和文本中将其更改为 Out of stock

相关问题