Call our sales line
08000 484 679
Home > Magento Tips > Set products to ‘In Stock’ when Qty is above 0

Set products to ‘In Stock’ when Qty is above 0

Posted on: 1st Nov 2012 By: Adam Moss 4 Comments

I encountered an issue recently with Magento’s inventory system whereby products would have a quantity of above zero, which would technically make them in stock, but they would be set to ‘Out of Stock’ on the system. The main reason for this was that the client had forgotten to do this extra step while uploading products with Dataflow. It’s like having a car service and forgetting to switch the service light off, except without the soul-crushing expenditure.

I wrote a small module which works with a custom cron schedule. All it does is check the cataloginventory_stock_item table for items that have a ‘qty’ above zero, if so it will set the ‘is_in_stock’ field to 1 (in stock). This is then followed by a reindex of the Stock Index to ensure the other inventory tables are up to date. Did you know there’s 4 tables for the cataloginventory model? Overkill? Yes.

Creare/Stockupdates/controllers/UpdateController.php

class Creare_Stockupdates_UpdateController extends Mage_Core_Controller_Front_Action
{
	public function updateAction()
	{
		if ($_SERVER['REMOTE_ADDR'] == Mage::getStoreConfig('dev/stockupdater/stockupdater_server_ip_address'))
		{
		$resource = Mage::getSingleton('core/resource');
		$writeconnection = $resource->getConnection('core_write');
		$writeconnection->query('UPDATE cataloginventory_stock_item SET is_in_stock = 1 WHERE qty > 0');

		$process = Mage::getModel('index/process')->load(8); // 8 is 'Stock Status'
		$process->reindexAll();
		} else {
			$this->_redirectUrl('/');
		}
	}
}

I have also added an IP address check around the body of the method so only the cron server can run the file. If a non-valid IP accesses the action path they’ll simply be redirected to the homepage.

Creare/Stockupdates/etc/config.xml

Set up your frontend router by-the-numbers.

<?xml version="1.0"?>
<config>
  <modules>
    <Creare_Stockupdates>
      <version>0.1.0</version>
    </Creare_Stockupdates>
  </modules>
  <frontend>
    <routers>
      <stockupdates>
        <use>standard</use>
        <args>
          <module>Creare_Stockupdates</module>
          <frontName>stockupdates</frontName>
        </args>
      </stockupdates>
    </routers>
  </frontend>
</config>

Creare/Stockupdates/etc/system.xml

Optional: Put an IP address check into your configuration. If you can get this working with the Magento cron, or with a backend router then you can make it more secure without having to do this step.

<?xml version="1.0"?>
<config>
  <sections>
    <dev>
    <groups>
      <stockupdater translate="label">
        <label>Creare Stockupdater Script</label>
        <sort_order>1</sort_order>
        <show_in_default>1</show_in_default>
        <show_in_website>1</show_in_website>
        <show_in_store>1</show_in_store>
        <fields>
          <stockupdater_server_ip_address translate="label">
            <label>Stockupdater Server IP Address</label>
            <frontend_type>text</frontend_type>
            <sort_order>100</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
            <comment>This is the IP address of the server that will run the cron job.</comment>
          </stockupdater_server_ip_address>
        </fields>
      </stockupdater>
    </groups>
    </dev>
  </sections>
</config>

Which gives you the following in System > Configuration > Developer:

Stockupdater

app/etc/modules/Creare_Stockupdates.xml

Activate your module.

<?xml version="1.0"?>
<config>
<modules>
   <Creare_Stockupdates>
      <active>true</active>
      <codePool>local</codePool>
      <version>1.0</version>
   </Creare_Stockupdates>
</modules>
</config>

Once you’ve added all these files, you just need to point your cURL command to your controller action which will be: http://www.yourdomain.co.uk/stockupdates/update/update

Hope this comes in useful and thanks for reading. As usual, if you have any suggestions/improvements please leave them in the comments below.

By Adam Moss

Adam is Ecommerce Manager and a PHP developer at Creare Group. Adam is responsible for training Magento development within the company. Follow Adam on Twitter: http://twitter.com/adampmoss. - .

4 Responses to “ Set products to ‘In Stock’ when Qty is above 0 ”

  1. Pau
    #1 | 1st November 2012

    Hi,

    This is really interesting, I’ve run into this issue many times, and not only because the client forgot to change that field when importing products.

    Anyway, I wanted to comment that a better way of creating a cron task would be creating a model with the cron function in it and setting the cron on the config.xml file. It’s a very easy thing to do, it will be completely integrated with Magento and you won’t need to worry about IP restrictions or other security issues.

    Basically:

    1. Create a model in Model/StockStatus.php (for example)
    2. Define the model and create the function pasting the code you have already created here.
    3. Add the to the config.xml file, referencing to that model and that function and set how often it will be executed.

    Thanks for sharing!

    Pau

  2. Adam Moss
    #2 | 2nd November 2012

    Hi Pau, you’re right that this would be a simpler way of doing it, negating the need for a second cron.

    I’ll work on that script and produce an update in the coming weeks. Thanks for your feedback!

  3. Ryan
    #3 | 28th November 2012

    Hi Adam
    Great post and very handy for many stores that use Magento’s Dataflow
    Did you get anywhere with Pau’s updates.
    Would be good to get a full tutorial using the alternative cron method.

  4. Adam Moss
    #4 | 29th November 2012

    Hi Ryan, I’ll be cracking onto this update within the next couple of weeks. I’ll update this post when it’s ready.

Post A Comment

Your comments:
Enclose code snippets within the appropriate tags: [php][/php]   [js][/js]   [xml][/xml]   [css][/css]   [html][/html]
E.g: [php]<?php echo "hello world"; ?>[/php]

Search Blog

Follow us on Twitter

Archives

For the record...

Views & opinions in this blog are those of the individual and do not necessarily reflect those of E-commerce Web Design or the Creare Group.