You may want to take a look at getBuyRequest in app/code/core/Mage/Sales/Model/Quote/Item.php.
This method appears to be used to pull the product options, and then add them to the quote.
However, you would also want to modify the setProduct method as well, so that way you can setQty() on the Product being saved to the model to reference later in buyRequest.
You could do something like:
->setQty(Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty())
in setProduct().
I wouldn't recommend doing these things in app/code/local/Mage overrides though, it would definitely be better to create a module for these things. In fact, you could accomplish this through an Observer event, listening for sales_quote_item_save_before and do something similar to:
public function checkQuoteItemQty($obs) {
$event = $obs->getEvent();
$item = $event->getDataObject();
$product = Mage::getModel('catalog/product')->load($item->getProduct()->getProductId());
$itemInventory = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product);
if ($item->getQty() > $itemInventory->getQty()) {
$item->setQty($itemInventory->getQty());
}
return $this;
}
Hope this helps!
This happens because the configurable product does not have minimum sale qty and a total qty.
You need to get the simple product that is represented by the configurable product and get the values from it. Try this:
$simpleProduct = null;
foreach ($_item->getQuote()->getAllItems() as $simpleItem){
if ($simpleItem->getParentId() == $_item->getId()){
$simpleProduct = $simpleItem->getProduct();
break;
}
}
if ($simpleProduct){
//work your magic with $simpleProduct
}
I didn't test, but it seams like the way to go.
I've tested and this works for me. I was close. Instead of getParentId()
it should be getParentItemId()
. Here is the code from my checkout/cart/item/default.phtml
file:
$simpleProduct = $this->getProduct();
if ($this->getProduct()->getTypeId() == 'configurable') {
foreach ($_item->getQuote()->getAllItems() as $simpleItem){
if ($simpleItem->getParentItemId() == $_item->getId()){
$simpleProduct = $simpleItem->getProduct();
break;
}
}
}
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($simpleProduct);
$min_sale_qty = (int)$stockItem->getData('min_sale_qty');
$total_qyt = (int)$stockItem->getQty();
?>
<select name="cart[<?php echo $_item->getId() ?>][qty]">
<?php for($i = $min_sale_qty; $i <= $total_qyt; $i = $i + $min_sale_qty) : ?>
<option value="<?php echo $i?>" <?php echo ($i == $this->getQty())? "selected=selected": ""; ?>>
<?php echo $i?>
</option>
<?php endfor;?>
</select>
I'm almost sure that there is an other way of getting the simple product and avoid the foreach
loop but this will do for now.
Best Answer
Updating the quantity means that the full quote has to be recalculated invalidating the chosen payment and shipment methods. This will require the user to go back to these steps and choose the new information.
It can probably be done using a custom controller that adds the new quantity then calling that over AJAX for example but after every quantity change you will have to send the user back to the shipment method step or at least prevent him from checking out so he can move back himself. All in all I highly doubt it will be very user friendly.