<p>This should grab all of the variant's SKUs like you're looking for:</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria('Commerce_Variant');
$allVariants = $criteria-&gt;find();
$allSkus = array();

foreach ($allVariants as $variant)
{
    $allSkus[] = $variant-&gt;sku;
}
</code></pre>

CC BY-SA 3.0
2017-04-17T22:44:06.490Z
Thanks Brad. That solved the first part of my question. But I had the wrong idea to fix the plugin. Stripos searches strings, and even if I changed the for loop to become a longer string, it didn't fix the plugin. I'll need to rethink this
Will Law
CC BY-SA 3.0
2017-04-18T18:29:51.413Z
No pressure, but any possibility you could look at [this question](https://craftcms.stackexchange.com/questions/19722/how-to-apply-a-discount-or-sale-to-a-variant) relating to [the plugin](https://github.com/willlaw/variant-discount/tree/salededuction) mentioned above? — I'm at a loss on how to fix it.
Will Law
CC BY-SA 3.0
2017-04-17T20:30:42.080Z
2017-04-17T20:30:42.080Z
Brad Bell
<p>I want to create an array of all SKUs on Craft Commerce. The end goal is to create a plugin that only applies Sales to certain variants. It does this by  checking the Sale's description for any mention of SKUs and comparing this against the list of all available SKUs.</p>

<p>I thought this would do the trick…</p>

<pre><code>$allvariants = craft()-&gt;elements-&gt;getCriteria('Commerce_Variant');
$allskus = $allvariants -&gt; sku;
</code></pre>

<p>But it doesn't appear to work. The full code of the plugin is…</p>

<pre><code>craft()-&gt;on('commerce_sales.onBeforeMatchProductAndSale', function($event){
    $sale = $event-&gt;params['sale'];

    $allvariants = craft()-&gt;elements-&gt;getCriteria('Commerce_Variant');
    $allskus = $allvariants -&gt; sku;

    if (stripos($sale-&gt;description, 'only') === false) {
       return; /* do nothing, and let the sale match as it normally would, because the sale does not have 'only' in the description. */
    } else {
        if (stripos($sale-&gt;description, $allskus) === false) {
            $event-&gt;performAction = false; /* since this SKU is not in the description string, then don't apply this discount */
        }
    }
});`
</code></pre>

<p>(Full credit to @luke-holder who wrote the plugin to do this <a href="https://craftcms.stackexchange.com/questions/19722/how-to-apply-a-discount-or-sale-to-a-variant">for Discounts</a>)</p>

<plugin-development><plugin-craftcommerce>
Get all Variant SKUs using getCriteria
# Get all Variant SKUs using getCriteria

(Asked by: username_0 on 2017-04-17)

I want to create an array of all SKUs on Craft Commerce. The end goal is to create a plugin that only applies Sales to certain variants. It does this by  checking the Sale's description for any mention of SKUs and comparing this against the list of all available SKUs.
I thought this would do the trick…
$allvariants = craft()->elements->getCriteria('Commerce_Variant');
$allskus = $allvariants -> sku;

But it doesn't appear to work. The full code of the plugin is…
craft()->on('commerce_sales.onBeforeMatchProductAndSale', function($event){
    $sale = $event->params['sale'];

    $allvariants = craft()->elements->getCriteria('Commerce_Variant');
    $allskus = $allvariants -> sku;

    if (stripos($sale->description, 'only') === false) {
       return; /* do nothing, and let the sale match as it normally would, because the sale does not have 'only' in the description. */
    } else {
        if (stripos($sale->description, $allskus) === false) {
            $event->performAction = false; /* since this SKU is not in the description string, then don't apply this discount */
        }
    }
});`

(Full credit to @luke-holder who wrote the plugin to do this for Discounts)

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

---

## 1 answers

### Answer by username_1 on 2017-04-17. Score: 1 | Accepted answer

This should grab all of the variant's SKUs like you're looking for:
$criteria = craft()->elements->getCriteria('Commerce_Variant');
$allVariants = $criteria->find();
$allSkus = array();

foreach ($allVariants as $variant)
{
    $allSkus[] = $variant->sku;
}

> **Comments:**
> **username_0**: Thanks Brad. That solved the first part of my question. But I had the wrong idea to fix the plugin. Stripos searches strings, and even if I changed the for loop to become a longer string, it didn't fix the plugin. I'll need to rethink this  
> **username_0**: No pressure, but any possibility you could look at [this question](https://craftcms.stackexchange.com/questions/19722/how-to-apply-a-discount-or-sale-to-a-variant) relating to [the plugin](https://github.com/willlaw/variant-discount/tree/salededuction) mentioned above? — I'm at a loss on how to fix it.  

---

