<p>You should be able to do this the usual way of setting entry fields in a plugin I think:</p>

<pre><code>        $product-&gt;setContentFromPost(array(
            'sku' =&gt; $sku,
        );
        craft()-&gt;commerce_products-&gt;saveProduct($product);
</code></pre>

<p>(Alternatively, you could listen to <code>onBeforeSaveProduct</code> -  <a href="https://craftcommerce.com/docs/events-reference#commerce_products.onbeforesaveproduct" rel="nofollow noreferrer">https://craftcommerce.com/docs/events-reference#commerce_products.onbeforesaveproduct</a> - and since the product will be saved after that anyway you could probably leave out the call to <code>saveProduct</code> in that case)</p>

<p>I don't think I've tried it with products but it certainly works with orders for much the same sort of thing.</p>

CC BY-SA 3.0
2016-12-06T00:41:22.567Z
2016-12-06T00:41:22.567Z
Jeremy Daalder
<p>I have a plugin that is listening to the <code>onSaveProduct</code> event.</p>

<p>In my product settings, in the "Automatic SKU Format" field I have: </p>

<pre><code>{product.title}
</code></pre>

<p>That way I am always working the main product title.</p>

<p>For the SKU, I want to only use the first initial of each word - so I am doing this from my plugin:</p>

<pre><code>...
$default_sku = $event-&gt;params['product']-&gt;defaultVariant-&gt;sku;
$expr = '/(?&lt;=\s|^)[a-z]/i';

preg_match_all($expr, $default_sku, $matches);

$sku = strtoupper(implode('', $matches[0]));

...

error_log((craft()-&gt;config-&gt;get('devMode')) ? 'SKU: ' . $sku : '');
</code></pre>

<p>Let's say I have "My Super Product" as the title. The above is generating this: <strong>MSP</strong>  </p>

<p>Perfect!</p>

<p>But, now I'm not sure how to go <em>back</em> and re-save the product.</p>

<p>Do I need to re-find the product and then set the <code>defaultSku</code> and then re-save it? Not sure how to proceed.</p>

<p>Thank you for any suggestions!</p>

<plugin-craftcommerce>
Commerce: How can I save a custom SKU from a plugin?
# Commerce: How can I save a custom SKU from a plugin?

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

I have a plugin that is listening to the onSaveProduct event.
In my product settings, in the "Automatic SKU Format" field I have: 
{product.title}

That way I am always working the main product title.
For the SKU, I want to only use the first initial of each word - so I am doing this from my plugin:
...
$default_sku = $event->params['product']->defaultVariant->sku;
$expr = '/(?<=\s|^)[a-z]/i';

preg_match_all($expr, $default_sku, $matches);

$sku = strtoupper(implode('', $matches[0]));

...

error_log((craft()->config->get('devMode')) ? 'SKU: ' . $sku : '');

Let's say I have "My Super Product" as the title. The above is generating this: MSP 
Perfect!
But, now I'm not sure how to go back and re-save the product.
Do I need to re-find the product and then set the defaultSku and then re-save it? Not sure how to proceed.
Thank you for any suggestions!

**Tags:** plugin-craftcommerce

---

## 1 answers

### Answer by username_1 on 2016-12-06. Score: 1

You should be able to do this the usual way of setting entry fields in a plugin I think:
        $product->setContentFromPost(array(
            'sku' => $sku,
        );
        craft()->commerce_products->saveProduct($product);

(Alternatively, you could listen to onBeforeSaveProduct -  https://craftcommerce.com/docs/events-reference#commerce_products.onbeforesaveproduct - and since the product will be saved after that anyway you could probably leave out the call to saveProduct in that case)
I don't think I've tried it with products but it certainly works with orders for much the same sort of thing.


---

