3

For a Craft Commerce site, the store owners wish to be notified automatically when a stock is starting to get low. Can this be achieved with a plugin, and if so, how?

3

Just like Craft itself, Craft Commerce has lots of events you can have a plugin respond to. In this case, you'd want to write a plugin that waits for commerce_orders.onOrderComplete (per documentation) that evaluates the stock levels and probably uses Craft's Email Service to fire off notifications to the store owners.

Here's some quick pseudocode:

<?php

namespace Craft;

class StockNotificationPlugin extends BasePlugin
{
    ...

    function init()
    {
        craft()->on('commerce_orders.onOrderComplete', function($event) 
        {
            // loop through the items in the order ($order->lineItems), 
            // checking the remaining stock ($item->purchasable->stock)
            // against your desired threshold, firing off notifications
            // if applicable
        }
    }

    ...
}
  • Thanks very much Matt, one is not a developer anymore though, front end is my bread and butter now. This needs a Control Panel interface, where you can set who gets the email notifications via the plugin, as set by the store owner - as it could be a few people that get the notifications - a stock warehouse as well for example and of course input fields for the actual stock level control per product. Yes, you could do that via a custom field/s, but it would be nice to have a configured plugin I feel. – moondawg Mar 9 '16 at 19:55
  • @moondawg You might consider posting a feature request for this, or see if you could hire a developer to build the plugin for you. (The Craft Slack account is crawling with friendly developers.) – Matt Stein Mar 9 '16 at 20:00
  • Thanks Matt, Craft via Twitter said they could help. Quite happy to pay then for this to be in the 'core'. – moondawg Mar 9 '16 at 20:47
  • @moondawg Kudos to you. Appreciate it when the industry pays for new core features! Well done – Matt P Mar 10 '16 at 8:41
  • In Norwegian "Matt stein" means dim rock :D – Wobee Mar 10 '16 at 16:12
3

We have created an working example plugin that can notify users by email when the stock of a variant drops below a defined threshold.

https://github.com/pixelandtonic/CommerceStockNotifier

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.