SNIPPETBANK.NET

PHP - OpenCart 2.0 Low Stock Notification


There are numerous OpenCart extensions that allow you to get notified when product stock levels are running low. Most of these extensions work by sending an email/sms at the time an order has been placed. Personally I prefer not to add this extra processing burden on the checkout process. My solution is to setup a cron job that sends out and email every 24 hours with details of all the products that have a low stock level. I find this solution to be cleaner as it doesn't affect any extensions or custom code that might already be implemented.

Implementing this solution is very simple and requires only 3 steps. First, a new controller class must be created to query the products table and send out the email. Second, write a new method to retrieve all products that have a quantity less then X and finally setting up the cron job.

Step 1 - Create a new controller

Copy and paste the code into a new file and save it as stocklevel.php in catalog/tool. Be sure to enter your email address in the mail function on line 16.

  1.  
  2. <?php  
  3. class ControllerToolStocklevel extends Controller {  
  4.       
  5.     public function index(){  
  6.         $this->load->model('catalog/product');  
  7.           
  8.         $productStockLevels = $this->model_catalog_product->getProductsWithLowQty();  
  9.           
  10.         $message = "Products with low stock quantity\n\n";  
  11.           
  12.         foreach($productStockLevels as $product){  
  13.             $message .= "Product Id: ".$product['product_id'].", Model: ".$product['model'].", Qty: ".$product['quantity']."\n";  
  14.         }  
  15.  
  16.         if(mail("your email address here""Low Stock Level", $message)){  
  17.             echo "Mail sent";  
  18.         }else{  
  19.             echo "Mail not sent";  
  20.         }  
  21.  
  22.         echo str_replace("\n""<br/>", $message);  
  23.     }  
  24. }  
  25. ?>  
  26.  

Step 2 - Create a new model method to query products

Open the file model/catalog/product.php and copy/paste the following code at the end of the file just before the last closing curly brace.

  1.  
  2. public function getProductsWithLowQty(){  
  3.     $quantity = 20;  
  4.     $query = $this->db->query("SELECT product_id, model, quantity FROM " . DB_PREFIX . "product WHERE quantity < ".$quantity)->rows;  
  5.     return $query;  
  6. }  
  7.  

The method above will return all products that have a quantity of less than 20. Adjust the $quantity variable to suit your needs.

Step 3 - Testing and setting up cron job

Before you setup a cron job you can test the controller by navigating to index.php?route=tool/stocklevel. Finally, you'll need to setup a con job to execute the controller. Consult your hosting plan on how to setup a cron job if your not sure how to do this.


Java - An Introduction To Threads
This article presents a simple overview on what threads are and how to implement them in Java. In the next article we examine how threads can be used to develop a simple multi-threaded stand alone server.
PHP - Detecting Spam Requests With Raw Analytics
If you own a website, chances are you have some sort of analytics software installed such as Google Analytics to report on traffic. But Google Analytics doesn't report on spiders/bots/referral spammers that crawl your site using your bandwidth and resources. In this article, I discuss the approach I took to try and block these spiders and spammers.
C# - Group Chat
If you want to know how to develop a group chat application, have a look at this project. The project includes a server and a client. The server listens for TCP client connections. Once a client is connected to the server, the server maintains the connection in a collection. Every time a message is sent from a client to the server, the server broadcasts the message to all clients in the collection.
C# - Custom InputBox
C# unlike Visual Basic, does not have an Input DialogBox class to capture user input. Buts that's not a problem as you can just add a reference to Microsoft.VisualBasic.dll and use the static InputBox class from the Microsoft.VisualBasic.Interaction namespace. But if you want a custom InputBox, then checkout this project.
PHP - JsonDb A Flat File Database
JsonDb is a simple flat file database library designed to perform CRUD operations on a flat file. In this article we take a look at how JsonDb can be used to store data in a json file.
PHP - MicroMVC A Simple Framework
The MVC pattern is a widely used pattern for developing web applications. PHP has a vast range of open source MVC frameworks that help to ease development. This article presents a lightweight MVC framework that you can use as a bootstrap.
ASP.Net - Simple File Upload
Asp.Net makes uploading files as simple as a few lines of code. In this article, I explain how to upload files using Web Forms and C#.
C# - Text To Speech
The .Net Framework makes developing Text to Speech applications very simple with the System.Speech assembly. In this article, I explain how easy it is to get started with Text to Speech.
C# - Image Slider Control
This Windows control can be used to view images in a slide show with captions. The control is configurable with a few useful settings. The list below contains a set of properties which can be used to configure the ImageSlider control.
C# - Custom Button Control
Creating custom controls with the .Net Framework is relatively easy. In this article I explain how to create a custom button control using GDI.
C# - Reflection
The .Net Framework provides a reflection capability that allows you to obtain information about assemblies, modules and types as well as being able to dynamically create instances of types. In this article, I explain how to get type information and create instances dynamically.
PHP - Simple Database ORM
DataContext is a simple ORM class that can be used to manage CRUD operations on a database. Standard PHP objects or arrays are used to insert/update data.
PHP - Introduction To PDO Part 1
PHP Data Objects (PDO) is a simple database abstraction extension that allows you to interact with databases. This tutorial introduces you to the PDO extension and provides sample code to help you get started.
PHP - Introduction To PDO Part 2
In part 1, I explained how to connect to a MySQL database server using the PDO extension. In this tutorial, I explain more about the PDOStatement class and how it's used to fetch data.
A Custom Windows Message Box It's taken a while, but I've finally, updated a custom MessageBox class, that I had developed a few years ago. In this article, I'll explain some of the new changes and why I made them.
Introduction To Stored Procedures And Functions Part 1 MySQL introduced Stored Routines as of 5.0 making it possible to execute SQL code stored on the server. In this article I will explain what Stored Routines are and how they can be used with examples.
ASP.Net C# MessageBox Modal boxes are great to get a users response. Sometimes, they replace the traditional JavaScript alert and confirm dialogs giving the end user a better experience. But managing modal boxes in code ca...
Accordion Menu In Window Forms In this tutorial, I'll explain how to develop a custom Accordion menu class, that you can use in your Windows Forms application to group controls together.
JSON Viewer At some point you might have been exposed to JSON data whilst working on a project. Sometimes the data can be hard to read if your not using a decoder and your working with large data.