I need to setup a way to quick-edit products while viewing them in a list, similar to woocommerce bulk edit feature:

enter image description here

I can't find any documentation on actual editing of the products, as opposed to editing the product in a cart (such as quantity.)

For example, I can grab products in my plugin template file using:

{% set products = craft.commerce.products.order('title ASC').limit(10) %}

But that doesn't give me access to things like how much inventory is left, so that I can output that and offer a field to edit it.

Did I miss the docs to do this? Not sure if Commerce is setup with services available for other plugins to edit products.


This is a fairly complex question and this is only really a sketch of an answer.

You'll want to get to know the available models and services in Commerce pretty well to achieve this in all.

You'll most likely do much more work in the plugin side of things than the template side of things - that is, you'll pull together the models/data you want in the plugin, and then pass those things through to the template, rather than just retrieve the data in the template I'd say (as per your example there).

You'd take the various selection paramaters (categories, stock level, whatever) and post these to a controller in your plugin. Then in your plugin, you'd get the variant models you want via PHP, and then post this data through to a template that would render it into a form, and probably just return this whole form via json as some html that you then display with some front end ajax

         $param1 = craft()->request->getPost('whatever'));              

        //This is a service in your plugin that gathers and returns variant models
        $data = craft()->yourplugin->searchStock($param1,...);

       $html = $this->renderTemplate('yourplugin/stockEditForm', array(
            'products' => $data,
        ), true);

        $this->returnJson(["success"=>true, "html"=>$html]);

... in your stockEditForm you then just loop through the products as normal

       {% for product in products %} ...etc

... then some ajax to actually render that on the plugin page

        type: 'POST',
        url: '/actions/yourplugin/bulkStockEdit',
        data: form_data,
        success: function(response) {
        statusCode: {
            500: function(jqXHR, textStatus, errorThrown) {

That will get you a form with the data populated into it. From there, each save button in your constructed will need to be hooked up to some ajax to call appropriate Commerce controllers to actually save the changed variants...

I think that, as a basic structure, would get you going with this. Bulk stock editing certainly would be handy at times!

  • Dude, awesome. I forgot to comment/check answer on this earlier but I've been building on your suggestion. So far so good, thanks!
    – taylor
    Nov 15 '16 at 19:28
  • Glad you're off and running @taylor - I was working on something sort of related (pulling in data from an MYOB accounting system into tables in a Craft plugin) - so the timing was very fortunate :) Nov 16 '16 at 0:02

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.