3

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.

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.

10
  • 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 and I'm wondering if I need any plugin components to create this?
    – Will Law
    Apr 10 '17 at 18:12
  • Use plugin factory to make the main plugin file, and then just put my code above into the init() function. Apr 11 '17 at 10:51
  • 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
    Apr 11 '17 at 12:11
  • Put your plugin up on github/gist and I can take a look and make corrections. Apr 11 '17 at 12:25
  • Here's the plugin on GitHub github.com/willlaw/variant-discount
    – Will Law
    Apr 11 '17 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.