【问题标题】:Get All simple product from a Configurable Product in Magento Product View从 Magento 产品视图中的可配置产品中获取所有简单产品
【发布时间】:2012-08-30 11:37:39
【问题描述】:

如何获得与可配置产品相​​关的所有简单产品?我找到了相反的方法(从简单的产品中获得可配置的产品),但这不是我需要的。

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

【问题讨论】:

    标签: php magento


    【解决方案1】:

    使用下面的代码

    获取完整产品信息的代码(其中 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);
    

    希望这会有所帮助!

    【讨论】:

    • 致命错误:在非对象上调用成员函数 getChildrenIds()
    • $childProducts = Mage::getModel('catalog/product_type_configurable')->getChildrenIds(3); 非常适合我(减去额外的分号。谢谢!
    • 第一种方法给了我所有的子产品,但它说所有的子产品都已启用,即使大多数实际上都已禁用。这是为什么呢?
    • @NickRolando 更好地使用第二个选项。这个很完美!
    【解决方案2】:

    一个可配置的产品可以关联多个其他产品。

    这是获取与可配置产品关联的所有子产品的代码。

    这里是代码:)

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

    【讨论】:

    • 请注意,您可以简单地执行此操作,而不是手动加载 product_type_configurable 模型:$product->getTypeInstance()->getChildrenIds()$product->getTypeInstance()->getUsedProducts()
    【解决方案3】:

    中使用以下脚本

    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

    【讨论】:

    • 很棒的解决方案哈迪克!
    • 这不是 PHP。
    • @Qix,它是 Javascript。由于它需要在客户端执行,当客户从下拉列表中选择一个选项时,PHP 将毫无用处(除非伴随着一些 AJAX 驱动的 Javascript)。
    【解决方案4】:

    我明白了。感谢您的回复。

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

    【讨论】:

    • 你不应该在循环中load()
    • 有什么建议吗?
    【解决方案5】:

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

    添加到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和text里改成Out of stock

    【讨论】:

      【解决方案6】:

      在块中编写以下最简单的代码,然后在模板文件中调用它来获取产品相关产品:

          $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);
      

      现在,在模板中编写以下代码来打印名称:

         $collection = $block->getCollection(); 
         foreach ($collection as $key => $value) 
         {
            echo $value->getName();
            echo "<br>";
         }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-22
        • 1970-01-01
        • 1970-01-01
        • 2012-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-18
        相关资源
        最近更新 更多