<p>This is a fairly complex question and this is only really a sketch of an answer.  </p>

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

<p>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).</p>

<p>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</p>

<pre><code>         $this-&gt;requirePostRequest();
         $param1 = craft()-&gt;request-&gt;getPost('whatever'));              

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


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

        $this-&gt;returnJson(["success"=&gt;true, "html"=&gt;$html]);
</code></pre>

<p>... in your stockEditForm you then just loop through the products as normal</p>

<pre><code>       {% for product in products %} ...etc
</code></pre>

<p>... then some ajax to actually render that on the plugin page</p>

<pre><code>        $.ajax({
        type: 'POST',
        url: '/actions/yourplugin/bulkStockEdit',
        data: form_data,
        success: function(response) {
            $spinner.hide();
            $results.html(response.html);
            $results.show();
        },
        statusCode: {
            500: function(jqXHR, textStatus, errorThrown) {
              $spinner.hide();
              $results.html(jqXHR.responseText);
              $results.show();
            }
        },
    });
</code></pre>

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

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

CC BY-SA 3.0
2016-11-15T19:28:24.850Z
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
CC BY-SA 3.0
2016-11-16T00:02:48.017Z
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 :)
Jeremy Daalder
CC BY-SA 3.0
2016-11-13T02:06:35.927Z
2016-11-13T02:06:35.927Z
Jeremy Daalder
<p>I need to setup a way to quick-edit products while viewing them in a list, similar to woocommerce bulk edit feature: </p>

<p><a href="https://i.sstatic.net/6SY8C.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/6SY8C.png" alt="enter image description here"></a></p>

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

<p>For example, I can grab products in my plugin template file using:</p>

<pre><code>{% set products = craft.commerce.products.order('title ASC').limit(10) %}
</code></pre>

<p>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. </p>

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

<plugin-development><templating><plugin-craftcommerce><php>
Commerce - Edit Products via Plugin
# Commerce - Edit Products via Plugin

(Asked by: username_0 on 2016-11-12)

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

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.

**Tags:** plugin-development, templating, plugin-craftcommerce, php

---

## 1 answers

### Answer by username_1 on 2016-11-13. Score: 3 | Accepted answer

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
         $this->requirePostRequest();
         $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
        $.ajax({
        type: 'POST',
        url: '/actions/yourplugin/bulkStockEdit',
        data: form_data,
        success: function(response) {
            $spinner.hide();
            $results.html(response.html);
            $results.show();
        },
        statusCode: {
            500: function(jqXHR, textStatus, errorThrown) {
              $spinner.hide();
              $results.html(jqXHR.responseText);
              $results.show();
            }
        },
    });

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!

> **Comments:**
> **username_0**: Dude, awesome. I forgot to comment/check answer on this earlier but I've been building on your suggestion. So far so good, thanks!  
> **username_1**: 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 :)  

---

