I want a QTY box next to the add to cart button in grid category view with the products minimum quantity. I have tried using the code below and it works except that the field always shows a '0'.
How can I make it so that the field shows the minimum quantity of the product and not just '0'.
This is what I used to modify the list.phtml file:
<?php if(!$_product->isGrouped()): ?>
<label for="qty"><?php echo $this->__('Qty:') ?></label>
<input name="qty" type="text" class="input-text qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />
<?php endif; ?>
The function getProductDefaultQty is only available on view block and not the list :(
You could rewrite the class Mage_Catalog_Block_Product_List with a customer module and include this function in your module's class.
For the sake of this answer I will call your module Nat_Quantity (you can change this if you like)
Step 1: Create a moudle xml
Under /app/etc/modules/ create a file Nat_Quantity.xml. It should look something like (note the codePool has a uppercase P).
<?xml version="1.0"?>
<config>
<modules>
<Nat_Quantity>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Catalog />
</depends>
</Nat_Quantity>
</modules>
</config>
Step 2: Create your modules folder structure
Under /app/code/local/ create the folder Nat, then under there create the folder Quantity. Under this Quantity folder create the following two folders, etc and Block. (Note the etc is lowercase)
Step 3: Create your config.xml
Under /app/code/local/Nat/Quantity/etc create a config.xml file that will look something like:
<?xml version="1.0"?>
<config>
<modules>
<Nat_Quantity>
<version>1.0.0</version>
</Nat_Quantity>
</modules>
<global>
<blocks>
<catalog>
<rewrite>
<product_list>Nat_Quantity_Block_Product_List</product_list>
</rewrite>
</catalog>
</blocks>
</global>
</config>
Step 3: Create your block
Under /app/code/local/Nat/Quantity/Block/Product create a List.php which will looks something as follows:
<?php
class Nat_Quantity_Block_Product_List extends Mage_Catalog_Block_Product_List {
/**
* Get default qty - either as preconfigured, or as 1.
* Also restricts it by minimal qty.
*
* @param null|Mage_Catalog_Model_Product
*
* @return int|float
*/
public function getProductDefaultQty($product)
{
$qty = $this->getMinimalQty($product);
$config = $product->getPreconfiguredValues();
$configQty = $config->getQty();
if ($configQty > $qty) {
$qty = $configQty;
}
return $qty;
}
}
This should then allow you in the list template to call $this->getProductDefaultQty($product). You will need to pass into the function a validate product or you could pass in a product id and then load the product in the function
$product = Mage::getModel('catalog/product')->load($productId);