Preorder Buttons on Magneto Product Details/Grid/List views
Hi,
What we’ll be trying to accomplish: Show a PreOrder button instead of the default Order button
on Magento 1.4.1.1 whenever:
- The product’s stock quantity is zero.
- Backordering is enabled and the “Allow Qty Below 0 and Notify Customer†option is selected.
- The Stock Availability is set to “In Stockâ€
Step A: the addtocart.phtml
We’ll start with the addtocart.phtml template located in
/app/design/frontend/default/default/catalog/product/view/addtocart.phtml,
where the defaults are your interface and template names respectively.
We’ll basically replace the default button markup with the following code:
<?php define(CUSTOM_BACKORDERS_YES_NOTIFY , 2) ;
define(CUSTOM_STOCK_INSTOCK, 1);
$rquantity = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
$rbackorder = $_product->getStockItem()->getBackorders();
$rstockavailability = $_product->getStockItem()->getIsInStock(); ?>
<?php if (($rquantity == 0) && ($rbackorder == CUSTOM_BACKORDERS_YES_NOTIFY) && ($rstockavailability == CUSTOM_STOCK_INSTOCK) ) { ?>
<button type="button" title="<?php echo $this->__('Preorder') ?>" class="button btn-cart" onclick="productAddToCartForm.submit()"><span><span><?php echo $this->__('Preorder') ?></span></span></button>
<?php } else { ?>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="productAddToCartForm.submit()"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
<?php } ?>
Note that we are retrieving the Preorder string using Magento localiztion feature, we need to add this line
“Preorder”,”Preorder”
to
/app/design/frontend/default/default/locale/en_US/translate.csv
I actually tried to add a new file Mage_Catalog.csv in the above location which would make more sense but Magento didn’t pick it up.
Step B: the list.phtml
We’ll use the same code in list.phtml in two different places, the list mode listing and grid mode listing
<?php define(CUSTOM_BACKORDERS_YES_NOTIFY , 2) ;
define(CUSTOM_STOCK_INSTOCK, 1);
$rquantity = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
$rbackorder = Mage::getModel('catalog/product')->load($_product->getId())->getStockItem()->getBackorders();
$rstockavailability = $_product->getStockItem()->getIsInStock(); ?>
<?php if (($rquantity == 0) && ($rbackorder == CUSTOM_BACKORDERS_YES_NOTIFY) && ($rstockavailability == CUSTOM_STOCK_INSTOCK) ) { ?>
<button type="button" title="<?php echo $this->__('Preorder') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Preorder') ?></span></span></button>
<?php } else { ?>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
<?php } ?>
As you can see the only change is using
<?php $rbackorder = Mage::getModel('catalog/product')->load($_product->getId())->getStockItem()->getBackorders(); ?>
instead of
<?php $rbackorder = $_product->getStockItem()->getBackorders(); ?>
This is probably because Magento didn’t load the full product model on the list view (the only difference I could think of is
the getProduct() ?> line inthe addtocart.phtml).
That’s about it, any suggestions or better best practices are greatly appreciated.