Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-10T18:12:26.570Z
Answers.Text: 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?
Answers.UserDisplayName: Will Law
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-11T10:51:38.597Z
Answers.Text: Use plugin factory to make the main plugin file, and then just put my code above into the init() function.
Answers.UserDisplayName: Luke Holder
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-11T12:11:17.567Z
Answers.Text: 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?
Answers.UserDisplayName: Will Law
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-11T12:25:08.377Z
Answers.Text: Put your plugin up on github/gist and I can take a look and make corrections.
Answers.UserDisplayName: Luke Holder
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-11T13:42:37.257Z
Answers.Text: Here's the plugin on GitHub https://github.com/willlaw/variant-discount
Answers.UserDisplayName: Will Law
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-13T15:04:15.767Z
Answers.Text: did you get a chance to take a look? So appreciate your help so far
Answers.UserDisplayName: Will Law
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-14T04:33:59.640Z
Answers.Text: I just tested the plugin without modification and it works. Screenshots: http://jmp.sh/FmHwD0u and http://jmp.sh/LL9p75j
Answers.UserDisplayName: Luke Holder
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-14T15:38:24.540Z
Answers.Text: 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.`
Answers.UserDisplayName: Will Law
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-14T15:59:15.697Z
Answers.Text: …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?
Answers.UserDisplayName: Will Law
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-17T22:51:59.517Z
Answers.Text: 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?
Answers.UserDisplayName: Will Law
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-03T05:02:52.027Z
Answers.LastActivityDate: 2017-04-03T05:02:52.027Z
Answers.UserDisplayName: Luke Holder
Body: <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>
  
Tags: <plugin-craftcommerce>
Title: How to apply a discount or sale to a variant?
ThreadText: # 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 username_0 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 username_0 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/username_0/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/username_0/variant-discount/blob/salededuction/VariantDiscountPlugin.php). It's still not working. What do you think?  
  
  ---
  
  