I have built a plugin that, TL;DR subtracts credits. Everything is working great - until I actually need to change the price.

I just flat-out don't understand it.

This is how I am looping through to check a user's credits using the commerce_discounts.onBeforeMatchLineItem event. You can see where I was using the discount $event->performAction = false method to stop the discount from being applied. But, I need the ability to split the line items so-to-speak.

Example: qty of 5 of the same product; 3 are free, 2 are full price.

// $entry is the entry that keeps track how many credits are left.
if ((int)$entry->events > 0) {
    $i = (int)$entry->events;

    while ($i > 0) {
        error_log('doing stuff...');
            'events' => ((int)$entry->events > 0) ? ((int)$entry->events - (int)$i) : 0



        if ($i == 0) {
            error_log('OUT OF CREDITS');
            // $event->performAction = false;
    error_log((craft()->config->get('devMode')) ? 'CREDITS LEFT: ' . (int)$i : '');
} else {
    error_log((craft()->config->get('devMode')) ? 'Uh-oh user is out of event credits..' : '');
   $event->performAction = false;

I have an adjustor set up...it's basically from this thread.

What I don't understand is where to actually put the adjustor.


$myAdjuster->amount = -5.0;  // How do I make the amount be 100%?
$myAdjuster->orderId = $order->id;
    // Is this is where i need to loop through the line items and do the credit deductions?

$myAdjuster->optionsJson = ['lineItemsAffected' => null];

Do I do all of the looping and such within the adjustor? Instead of a dollar amount, I would like 100% off until my credits are zero. Then the user pays full price.

My apologies for such a large question. Any guidance is greatly appreciated!


1 Answer 1


I'm finding the question hard to follow, but if I am understanding you, then yes I would do it all in the adjuster - rather than using a discount here... I don't see what using a Commerce discount here offers, as you can't really express your credits system in any way meaningful that relates to a Commerce discount I think.

So this indeed looks like a job for an adjuster, which can act at either the lineItem or order level as per the linked example.

In terms of where you put that adjuster, there are basically three steps. The first two are in your main plugin file:

public function init()

and then later in that file...

 public function commerce_registerOrderAdjusters(){
   return [
     new BusinessLogic_TaxRemover

Then, you have the actual code in your adjuster to do whatever you want to do, which (repeating that example), looks like this:


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

    class BusinessLogic_TaxRemover implements Commerce_AdjusterInterface {

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

    $myAdjuster = new Commerce_OrderAdjustmentModel();

    $order->baseDiscount = $order->baseDiscount - 5;

    $myAdjuster->type = "Test";
    $myAdjuster->name = "Test Adjuster";
    $myAdjuster->description = "Takes $5 off the order";
    $myAdjuster->amount = -5.0;
    $myAdjuster->orderId = $order->id;
    //if your Adjuster affects lineItems rather than the total, you record the ids here
    $myAdjuster->optionsJson = ['lineItemsAffected'=>null];
    $myAdjuster->included = false;

    return [$myAdjuster];



...the main thing to note in there is you need to record the ids of any lineitems you're adjusting by changing this - ($myAdjuster->optionsJson = ['lineItemsAffected'=>null];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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