<p>Variants can not be selected when setting up discounts in the UI.</p>

<p>We have variant-based sales/discounts high on the list for the next version of Craft Commerce, but nothing planned to make available in the short term. It will also allow sales and discounts to work with any custom purchasable.</p>

<p>I tried to think of a way around it, and the one way I can came up with in the short term is a little hack:</p>

<p>You could use the description field to limit the discount to specific variant SKU's.</p>

<p>So all discounts would work and be set up as they normally would, but with a condition on the product, but if you got them to put the keyword "ONLY" into the description and then got them to paste the variant SKU into the description. i.e:</p>

<pre><code>ONLY the red tubs (SKU162839) are on discount.
</code></pre>

<p>or</p>

<pre><code>Discount ONLY applies to SKU78439 and SKU94300
</code></pre>

<p>You could then add the following event listener into a plugin:</p>

<pre><code>// So far this would only be called when the discount has
matched with everything else about the product.
craft()-&gt;on('commerce_discounts.onBeforeMatchLineItem',
function($event){
   $lineItem = $event-&gt;params['lineItem'];
   $discount = $event-&gt;params['discount'];

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

<p>I wrote the above code without testing, but something along those should work.</p>

<p>Alternatively you could place a custom field on the product which list the variants on discount, and have the above event listener check use that information on the product (instead of matching the SKU on the discount description).</p>

<p>Sorry it's a bit of a hack for now, we will get discounts on variants in the next version of Commerce.</p>

<p>Hope that helps.</p>

CC BY-SA 3.0
2017-04-10T18:12:26.570Z
this is really helpful — it's a hack, but as long as it makes it work. The only thing is, I've never built a plugin before. I'm using [Plugin Factory](https://pluginfactory.io) and I'm wondering if I need any plugin components to create this?
Will Law
CC BY-SA 3.0
2017-04-11T10:51:38.597Z
Use plugin factory to make the main plugin file, and then just put my code above into the init() function.
Luke Holder
CC BY-SA 3.0
2017-04-11T12:11:17.567Z
thank you again. I got the plugin installed, but sadly it doesn't function. No pressure but could you could test the code and see if it works for you?
Will Law
CC BY-SA 3.0
2017-04-11T12:25:08.377Z
Put your plugin up on github/gist and I can take a look and make corrections.
Luke Holder
CC BY-SA 3.0
2017-04-11T13:42:37.257Z
Here's the plugin on GitHub https://github.com/willlaw/variant-discount
Will Law
CC BY-SA 3.0
2017-04-13T15:04:15.767Z
did you get a chance to take a look? So appreciate your help so far
Will Law
CC BY-SA 3.0
2017-04-14T04:33:59.640Z
I just tested the plugin without modification and it works. Screenshots: http://jmp.sh/FmHwD0u and http://jmp.sh/LL9p75j
Luke Holder
CC BY-SA 3.0
2017-04-14T15:38:24.540Z
Oh of course! I understand what's going on. So I'm applying all my deductions through Sales, not Discounts, so I can display the deductions on the page as well as the cart. So I tried changing `commerce_discounts.onBeforeMatchLineItem` for `commerce_sales.onBeforeMatchProductAndSale` as well as the parameters `lineItem` and `discount` for `product` and `sale`… but I get an internal server error of `Property "Craft\Commerce_ProductModel.sku" is not defined.`
Will Law
CC BY-SA 3.0
2017-04-14T15:59:15.697Z
…which makes sense, given the product doesn't have an SKU, the variants do. How would I access the variants SKU's using this event?
Will Law
CC BY-SA 3.0
2017-04-17T22:51:59.517Z
I've had a few attempts at this. If you get a chance, [have a look](https://github.com/willlaw/variant-discount/blob/salededuction/VariantDiscountPlugin.php). It's still not working. What do you think?
Will Law
CC BY-SA 3.0
2017-04-03T05:02:52.027Z
2017-04-03T05:02:52.027Z
Luke Holder
<p>The sale and discount menus only discount at the product level. However, I need the ability to apply discounts and sales at the variant level. How would I do this?</p>

<p><strong>Example:</strong>
I sell a piece of software (the product). There are new licences and upgrade licences (the variants). I want to apply a discount to only the new licences.</p>

<plugin-craftcommerce>
How to apply a discount or sale to a variant?
# How to apply a discount or sale to a variant?

(Asked by: username_0 on 2017-03-31)

The sale and discount menus only discount at the product level. However, I need the ability to apply discounts and sales at the variant level. How would I do this?

**Example:**
I sell a piece of software (the product). There are new licences and upgrade licences (the variants). I want to apply a discount to only the new licences.

**Tags:** plugin-craftcommerce


---

## 1 answers

### Answer by username_1 on 2017-04-03. Score: 4

Variants can not be selected when setting up discounts in the UI.
We have variant-based sales/discounts high on the list for the next version of Craft Commerce, but nothing planned to make available in the short term. It will also allow sales and discounts to work with any custom purchasable.
I tried to think of a way around it, and the one way I can came up with in the short term is a little hack:
You could use the description field to limit the discount to specific variant SKU's.
So all discounts would work and be set up as they normally would, but with a condition on the product, but if you got them to put the keyword "ONLY" into the description and then got them to paste the variant SKU into the description. i.e:
ONLY the red tubs (SKU162839) are on discount.

or
Discount ONLY applies to SKU78439 and SKU94300

You could then add the following event listener into a plugin:
// So far this would only be called when the discount has
matched with everything else about the product.
craft()->on('commerce_discounts.onBeforeMatchLineItem',
function($event){
   $lineItem = $event->params['lineItem'];
   $discount = $event->params['discount'];

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

I wrote the above code without testing, but something along those should work.
Alternatively you could place a custom field on the product which list the variants on discount, and have the above event listener check use that information on the product (instead of matching the SKU on the discount description).
Sorry it's a bit of a hack for now, we will get discounts on variants in the next version of Commerce.
Hope that helps.

> **Comments:**
> **username_0**: this is really helpful — it's a hack, but as long as it makes it work. The only thing is, I've never built a plugin before. I'm using [Plugin Factory](https://pluginfactory.io) and I'm wondering if I need any plugin components to create this?  
> **username_1**: Use plugin factory to make the main plugin file, and then just put my code above into the init() function.  
> **username_0**: thank you again. I got the plugin installed, but sadly it doesn't function. No pressure but could you could test the code and see if it works for you?  
> **username_1**: Put your plugin up on github/gist and I can take a look and make corrections.  
> **username_0**: Here's the plugin on GitHub https://github.com/willlaw/variant-discount  
> **username_0**: did you get a chance to take a look? So appreciate your help so far  
> **username_1**: I just tested the plugin without modification and it works. Screenshots: http://jmp.sh/FmHwD0u and http://jmp.sh/LL9p75j  
> **username_0**: Oh of course! I understand what's going on. So I'm applying all my deductions through Sales, not Discounts, so I can display the deductions on the page as well as the cart. So I tried changing `commerce_discounts.onBeforeMatchLineItem` for `commerce_sales.onBeforeMatchProductAndSale` as well as the parameters `lineItem` and `discount` for `product` and `sale`… but I get an internal server error of `Property "Craft\Commerce_ProductModel.sku" is not defined.`  
> **username_0**: …which makes sense, given the product doesn't have an SKU, the variants do. How would I access the variants SKU's using this event?  
> **username_0**: I've had a few attempts at this. If you get a chance, [have a look](https://github.com/willlaw/variant-discount/blob/salededuction/VariantDiscountPlugin.php). It's still not working. What do you think?  

---

