0

I need help with my custom stock status.
That's what I have today.
My problem is that this shows only the qty of the parent product not the qty of the actually in use child product. I need the qty of the actually in use child product.
My english is not the best.
So I hope everyone understand what I mean / know what I want to build.

I sell shoes and have a lot of sizes for one shoe. So one shoe is a configurable product and have 10 options (simple products) that are the sizes to select.
When I use my own code I can only get and use the quantity of the one configurable product (shoe) and not from the selected size (simple product).
Have anyone a idea how I can get and use the quantity of the selected product?

<?php if ($qtyStock = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty()>0):{?>
                <p class="availability in-stock">
        <span class="label"><?php echo $this->helper('catalog')->__('Availability:') ?></span>
        <span class="value"><?php echo $this->helper('catalog')->__('In stock') ?></span>
        <span class="value"><p>Delivery Time:</p>
        <div id="delivery_time"><p>Delivery in 3 Days</p></div></span>
    </p>
     <?php } else: { ?>
                <p class="availability out-of-stock">
        <span class="label"><?php echo $this->helper('catalog')->__('Availability:') ?></span>
        <span class="value"><?php echo $this->helper('catalog')->__('Out of stock') ?></span>
        <span class="value"><p>Delivery Time:</p>
        <div id="delivery_time"><p>Delivery in 8 Days.</p></div></span>
    </p>
                <?php } endif; ?>
  • "Hope you know what I mean", really? I mean you either think you made yourself clear or you know you didn't, but we shouldn't be guessing ;) – Julien Lachal Sep 29 '15 at 15:54
  • Is this ajax, if it is not you will have to load all the stock in the initial page load. – SH- Sep 30 '15 at 15:03
  • i don't know. how can i load it from the initial page load? – M-F Oct 13 '15 at 8:14
1
       -----------Create controller-------------

         <?php
class Magento_Guys_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        echo "Thank you !";
    }
     public function genCartAction()
    {
        $id = $this->getRequest()->getParam('pid');
        $_product = Mage::getModel('catalog/product')->load($id);
        $buy = Mage::helper('checkout/cart')->getAddUrl($_product);
        echo $qty = (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();

        //echo $id;
        //echo $this->getRequest()->getParam('id');;
    }
}
?>

    ----------------Change Add to cart code-------------------

    <?php if ($_product->isAvailable()): ?>
              <b class="available_quanity" style="display: none">Available Quantity :</b> <span id="simplestock" class="simplestock">Please select a color to view the quantity</span>
            <?php endif; ?>
            </div>
            <?php if(!$_product->isGrouped()): ?>
            <div class="qty">
                <label for="qty"><?php echo $this->__('Qty:') ?></label>
                  <?php $i = 0; ?>
                  <select id="qty" class="input-text" name="qty" style="width:50px;">
                    <?php while($i<=(int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty()):?>
                      <option value="<?php echo $i; ?>"><?php echo $i++; ?></option>
                    <?php endwhile; ?>
                  </select>
            </div>
            <?php endif; ?>

    ----------------Add Ajax for get Quantity---------------

    <script>
    jQuery(document).ready(function() {
        jQuery(".product-options select[id^='attribute']").on('change', function() {
            var id = getSimpleProductId();
            var qty = "";
            jQuery('.available_quanity').show();
            jQuery("#fancybox-loading").show();
            jQuery.ajax({      
                type: "POST",
                data: 'pid=' + id,
                url:'https://www.thewirelesscircle.com/guys/index/genCart',
                success:function(response){ 
                    if (response) {
                        qty = response;
                        var x = document.getElementById("qty");
                        var i;
                        removeOptions(x);
                        for(i=1;i<=qty;i++) {
                            var option = document.createElement("option");
                            option.text = i;
                            option.value = i;
                            x.add(option);
                        }
                    }
                    jQuery("#fancybox-loading").hide();
                }
            });

        });
    });

    </script>

    ---------------Get Selected Option Id in Magento-------------------

    <script type="text/javascript">
        function removeOptions(selectbox)
        {
            var i;
            for(i = selectbox.options.length - 1 ; i >= 0 ; i--)
            {
                selectbox.remove(i);
            }
        }
        function getSimpleProductId() {
            var productCandidates = [];
            jQuery.each(spConfig.settings, function (selectIndex, select) {
                var attributeId = select.id.replace('attribute', '');
                var selectedValue = select.options[select.selectedIndex].value;

                jQuery.each(spConfig.config.attributes[attributeId].options, function(optionIndex, option) {

                    if (option.id == selectedValue) {
                        var optionProducts = option.products;

                        if (productCandidates.length == 0) {
                            productCandidates = optionProducts;
                        } else {
                            var productIntersection = [];
                            jQuery.each(optionProducts, function (productIndex, productId) {
                                if (productCandidates.indexOf(productId) > -1) {
                                    productIntersection.push(productId);
                                }
                            });
                            productCandidates = productIntersection;
                        }
                    }
                });
            });
            return (productCandidates.length == 1) ? productCandidates[0] : null;
        }
    </script>
  • You can create a javascript array to avoid performing api call twice for the same item id. – DependencyHell Sep 17 '18 at 13:03
0

Donot need to load inventory module.

Just use isSaleable() of product model and it will work better.

<?php if ($product->isSaleable()):?>
<p class="availability in-stock"> <span class="label"><?php echo $this->helper('catalog')->__('Availability:') ?></span> <span class="value"><?php echo $this->helper('catalog')->__('In stock') ?></span> 
<span class="value">
<p>Delivery Time:</p>
<div id="delivery_time">
  <p>Delivery in 3 Days</p>
</div>
</span>
</p>
<?php } else: { ?>
<p class="availability out-of-stock"> <span class="label"><?php echo $this->helper('catalog')->__('Availability:') ?></span> <span class="value"><?php echo $this->helper('catalog')->__('Out of stock') ?></span> 
<span class="value">
<p>Delivery Time:</p>
<div id="delivery_time">
  <p>Delivery in 8 Days.</p>
</div>
</span>
</p>
<?php } endif; ?>
  • Thank you very much for your fast answer. <br/> But that fix not my problem. Because products that are not in stock (fewer than 1). Have so the same delivery time as products that are in stock (more than 1). – M-F Sep 30 '15 at 7:17
0

You can get the IDs of child products of a configurable product by using:

$ids = Mage::getResourceSingleton('catalog/product_type_configurable')
        ->getChildrenIds($_product->getId());

From here you can load the stock item directly (as you are using 1.9):

$stockItem = Mage::getModel('cataloginventory/stock_item')
           ->loadByProduct($ids[0]);

You may want to consider the efficiency of this and the fact that such information will be only available on the back-end. It might be better to extend getJsonConfig() in Mage_Catalog_Block_Product_View_Type_Configurable to add stock data. getJsonConfig() provides the config information available to javascript when manipulating the configurable options.

  • that looks like what i searching for. but this not working for me. '<?php echo $ids ?>' only shows 'ARRY'. I insert it on the view.html file of my template is that right or use i the wrong file? Thank you very much for your help – M-F Oct 26 '15 at 19:49
0

In my case configurables are always in stock, so I needed a way to get stock counts from the parent. Here's my implementation in 1.9.3.7

// Children Stock QTY
function getChildrenStockQTY($product, $stock_qty = 0) {
    if ($product->getTypeId() != 'configurable') return false;
    $simple_ids = Mage::getResourceSingleton('catalog/product_type_configurable')->getChildrenIds($product->getId());
    foreach ($simple_ids[0] as $simple_id) {
        $simple_model = Mage::getModel('cataloginventory/stock_item')->loadByProduct($simple_id);
        $stock_qty += $simple_model->getQty();
    }
    return $stock_qty;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.