1

What I would like is for a 10% discount to be applied to non sale items after the total cart quantity reaches 10 total items. I would ONLY like this discount on non sale items. Is that possible?

So, say I have 2 Products in my cart:

  • Item A - $20 on sale for $10, qty 9
  • Item B - $20, qty 1

This makes my total in cart quantity 10 line items even though 9 of them are sale items.

What I would like is for Item B to be discounted 10% making my cart total $108

( (10*9) + (20 * .9) ) = 108

As it currently stands, if I toggle Exclude this discount for products that are already on sale it doesn't count the sales items towards the total.

  • So 1) cart needs to reach 10 items. 2) Then 10% off on any item not already on sale. – RitterKnight May 8 '17 at 16:37
  • Correct. You're much more succinct than I ;-) – Peter Tell May 8 '17 at 17:28
3

Based on some research, I believe a custom Adjuster is the way to solve this problem.

I created an adjuster with the following:

craft/plugins/myplugin/MypluginPlugin.php

public function commerce_registerOrderAdjusters()
{
  return [
    401 => new \Commerce\Adjusters\Myplugin_TenpercentDiscount
  ];
}

craft/plugins/myplugin/Adjusters/Myplugin_TenPercentDiscount.php

<?php

namespace Commerce\Adjusters;
use Craft\Commerce_LineItemModel;
use Craft\Commerce_OrderAdjustmentModel;
use Craft\Commerce_OrderModel;

class Myplugin_TenPercentDiscount implements Commerce_AdjusterInterface {

public function adjust(Commerce_OrderModel &$order, array $lineItems = []){

    if (empty($lineItems))
    {
        return [];
    }

    if ($order->totalQty >= 10)
    {
        $adjuster = new Commerce_OrderAdjustmentModel();

        $adjuster->type        = 'Item Discount';
        $adjuster->name        = '10% Off Items';
        $adjuster->description = 'Your order qualifies for a 10% discount on all non sale items because you are purchasing at least 10 products.';
        $adjuster->orderId     = $order->id;

        $amountOff = 0;
        foreach ($lineItems as $item)
        {
            if (empty($item->onSale))
            {
                $amountOff += ($item->subtotal + $item->discount) * .1;
            }
        }
        $amountOff = $amountOff * -1;

        $adjuster->amount      = $amountOff;
        $adjuster->optionsJson = ['lineItemsAffected' => null];
        $order->baseDiscount  += $amountOff;

        $adjuster->included = false;

        return [$adjuster];
    }
    return[];
}

}

Does anyone see any flaws with this approach?

  • Hi peter, I am struggling onto where I'd have to put this code. I've tried to put it under craft->plugins->Commerce->Adjusters and named it Myplugin_TenPercentDiscount.php. But that does not seem to work. Sorry, I am a bit lost in all of this. – outline4 Mar 3 '18 at 15:42
  • Hi @outline4 -- You're close. You'll need to create your own plugin. I suggest using Plugin Factory for the scaffolding: pluginfactory.io and then you'll want the adjuster in the path: craft/plugins/yourplugin/Adjusters/Yourplugin_Adjustername.php – Peter Tell Mar 23 '18 at 21:09
  • Forgot to add, you also need to register the adjuster, I updated my code above to show that – Peter Tell Mar 23 '18 at 21:10

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.