0

I am trying to change the quantity input shown on the product page to a select field where the maximum quantity selectable equals the total stock quantity minus quantity in cart. While this is generally working, I can not get the select to actually add the selected quantity to the cart, instead only one item will be added to the cart.

Here is my code:

<?php
    $onStockQty = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);
    $qtyStock = (int)$onStockQty->getQty();
    $inCartQty = Mage::getSingleton('checkout/cart')->getQuote()->getItemByProduct($_product);
    if ($inCartQty) {
        $qtyCart = (int)$inCartQty->getQty();
        $qtySminusC = $qtyStock - $qtyCart;
    }
    $minSaleQty = (int)$onStockQty->getData('min_sale_qty');
    if ($qtySminusC > '99') {
        $qtySminusC = '99';
    }
?>

<select name="<?php echo Mage::helper('core')->quoteEscape($this->__('Qty')) ?>" class="qty" id="qty">
    <?php for($i = $minSaleQty; $i <= $qtySminusC; $i = $i + $minSaleQty) : ?>
        <option value="<?php echo $i?>" <?php echo ($i == $this->getQty())? "selected=selected": ""; ?>>
            <?php echo $i?>
        </option>
    <?php endfor;?>
</select>
<button type="button" title="<?php echo $buttonTitle ?>" class="button btn-qty btn-action" onclick="productAddToCartForm.submit(this)"><span><span><?php echo $this->__('Ok') ?></span></span></button>
  • Are you need added qty in cart right? – Abdul Jan 16 '18 at 12:45
  • Not sure what you mean... I am talking about the product view, not the cart – loeffel Jan 16 '18 at 14:51
0

Actually I was going to comment but don't have enough reputations so posting it as answer: In the backend, go to products inventory section and check the option Maximum Qty Allowed in Shopping Cart. May need to change this.

  • My code is generally working and that setting has nothing to do with my problem. The problem is that the selected option from the dropdown is ignored when submitting the form, only one item is added regardless of what quantitiy I select. I can repeat this as many times as the item is in stock, then it will fail as it is supposed to. I need to somehow modify the form submit function to take into account the quantity selected in the dropdown. – loeffel Jan 16 '18 at 14:49

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.