Managing Custom Stock Status – Magento

Apart from the default stock statuses provided by Magneto namely ‘in stock’ and ‘out of stock’, here is how you can manage custom stock statues.

In this example, I have a total of four stock statues namely ‘in stock’, ‘out of stock’, ‘drop ships’ and ‘stock backordered’.

In order to manage the custom stock statues, first – you need to create a custom product attribute via the Magento Admin. I am using the attribute name ‘fulfillment’ in this example; tt would be of type ‘dropdown’ with two options ‘drop ships’ and ‘stock backordered’.

Note that you need to assign this newly created attribute to one of the attribute sets.

Configuration for managing stock status for products

Below are a few scenarios (in regard to – product stock management via Magento Admin), we would display the appropriate stock status for a product, depending on the conditions mentioned below:

  • In Stock: the stock is being managed for this product AND the quantity of the product is greater than zero
  • Out of Stock: stock is being managed for this product AND the quantity of the product is less than or equal to zero
  • Drop Ships: the stock is not being managed AND the ‘fulfillment’ attribute is set to ‘drop ships’
  • Stock Backordered: the stock is not being managed AND the ‘fulfillment’ attribute is set to ‘stock backordered’

 

Displaying stock status on frontend

Now, in the following file,

app / design / frontend / default / YOUR THEME NAME / template / catalog / product / view / type / default.phtml

We can use the code below to override the default Magento behavior to display stock availability.

<!-- Stock Status -->
<div class="stock status">
<?php 
	$manageStock = $_product->getStockItem()->getManageStock();

	$inventoryQuantity = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();

	if ($manageStock == 1) // If 'Manage Stock' is set to YES
		{
		// If 'Qty' is less than or equal to zero
			if($inventoryQuantity <= 0) { ?> 
				 <div class="availability out-of-stock">
				 	<span>
				 	<?php 
				 		echo $this->__('Out of Stock'); ?>
				 	</span> 
				 </div>
			<?php } else { ?>
			<div class="availability in-stock">
				<span>
					<?php 
					echo $this->__('In Stock'); ?>
				</span>
			</div> 
		<?php } 
		}
	else {  // check if 'fulfillment is set to 'Drop Ships'
			if ($_product->getAttributeText('fulfillment') == "Drop Ships")
				{ ?>
				 <div class="availability drop-ships">
				 	<span>
				 	<?php 
				 		echo $this->__('Drop Ships') ?>
				 	</span> 
				 </div>

				<?php }
				// check if 'fulfillment' is set to 'Stock Backordered' 
			elseif ($_product->getAttributeText('fulfillment') == "Stock Backordered")
			 	{ ?>
			 	 <div class="availability backordered">
				 	<span>
				 	<?php 
				 		echo $this->__('Stock Backordered') ?>
				 	</span> 
				 </div>

			 <?php }
			else { echo "In stock"; } // safe default
		}
	?>
	</div>
<!-- // Stock Status -->

<!-- the default behaviour of Magento which is NOT being used
		 is commented below -->
<?php /* if ($_product->isAvailable()): ?>
   <div class="availability in-stock"><span><?php echo $this->__('Stock') ?></span></div>
<?php else: ?>
    <div class="availability out-of-stock"> <span><?php echo $this->__('Stock') ?></span></div>
<?php endif;  */ ?>

Notes:

I am assuming that the product is set to ‘out of stock’ if the quantity is less than zero.

Let’s say the quantity in stock for a product is 10. If you change it to 0 (for testing) then, the stock status is automatically set to ‘Out of stock’ by Magento. Now, if you change the quantity to any number greater than zero, Magento does NOT automatically change the stock status to ‘in stock’ hence, you need to manually change the stock status to ‘in stock’.

  • John

    Thanks! This is good tutorial.
    Any way to hook such custom stock options with configurable products? For example, to show configurable product option and then this custom stock message?
    Red – In stock
    Black – Backorder
    Green – Dropship

  • http://www.masksfromvenice.com Matt

    Hi,

    Thanks for the post, looks like just what I need. I’ve tried but it seems this code is out of date maybe due to upgrades of Magento?

    I’ve tried copying this exactly and it throws a 500 server error so had to revert. Could you clear this up please as I would like to implement this.

    Thanks again for sharing your code and ideas with us :)

    Regards,
    Matt.

  • soryn

    It keeps giving me this fatab error:
    Fatal error: Call to a member function getStockItem() on a non-object in C:wampwwwa2appdesignfrontendcustomthemedefaulttemplatecatalogproductviewtypedefault.phtml on line 4

    I don’t have any clue what is wrong.If you cand give me a hint ..
    I use magento 1.6.2.0

    Regards

  • Mangesh

    Regarding: “Let’s say the quantity in stock for a product is 10. If you change it to 0 (for testing) then, the stock status is automatically set to ‘Out of stock’ by Magento. Now, if you change the quantity to any number greater than zero, Magento does NOT automatically change the stock status to ‘in stock’ hence, you need to manually change the stock status to ‘in stock’.”

    Is there a fix for this – to have Magento update the “out of stock” when there is inventory allocated?

  • Lindy

    that’s really usefull. customers began to value our store when they see they are cared for. and to show more info about stock on the frontend to customer we use this module http://amasty.com/custom-stock-status.html. I did’t want to buy it but then saw all the features and decided to try. the thing is now i change stock statuses in different ways for dif products (‘available tomorrow’ or ‘delivery in 4 days’) and use alerts for options of configurables. it works! now wanna recommend to everyone

  • Mike

    You need to make sure the Attribute is Frontend Visible, in Attribute settings. Then change the if ($_product->getAttributeText(‘fulfillment’)

    to

    if ($_product->getData(‘fulfillment’)

    Works perfect 1.7.0.2

  • eric

    Mike,

    I’ve made the changes you’ve stated.. Still won’t display.

    Attributes:
    Scope = Global
    Catalog Inpute Type for Store Owner = Dropdown
    Unique Value = No
    Values Required = No
    Input Validation for Store Owner = None
    Apply To = All Product Types
    Use To Create Configurable Product = Yes

    Visibile on Product View Page on Front-end = Yes

    What do I need to change?