9

I'm looking for a way to offer "3 for 2 / 2 for 1" type options with Commerce.

I've been able to achieve this just by adding a default value to a coupon code input.

It works, kinda. If we need to stick with that then we'd be satisfied but where it fails is; say I have 6 items in the cart, I only get the offer on the first 3.

This is a small store with just a handful of items for sale so not a deal breaker but I can imagine other larger stores doing similar.

Adjusters have been mentioned on Twitter but unsure how to follow that through.

7

The documentation for adjusters is here:

https://craftcommerce.com/docs/adjusters and https://craftcommerce.com/docs/order-adjustment-model

Essentially your plugin creates a class that fulfils the interface at: plugins/commerce/Commerce/Adjusters/Commerce_AdjusterInterface.php - which is very simple:

interface Commerce_AdjusterInterface
{
    /**
     * The adjust method modifies the order values (like baseShippingCost),
     * and records all adjustments by returning one or more orderAdjusterModels
     * to be saved on the order.
     *
     * @param Commerce_OrderModel $order
     * @param array $lineItems
     *
     * @return \Craft\Commerce_OrderAdjustmentModel[]
     */
    public function adjust(Commerce_OrderModel &$order, array $lineItems = []);
}

Your plugin (in your main plugin .php file) then just registers your new adjuster to hook it up. I'd suggest storing your actual adjuster classes in files in an Adjusters folder, lime Commerce itself does.

//at the top, to include your class
require_once('Adjusters/BusinessLogic_TestAdjuster.php');

...

//this registers the adjuster with Commerce so it will be applied to carts
public function commerce_registerOrderAdjusters(){
  return [
    new \Commerce\Adjusters\BusinessLogic_TaxRemover
  ];
}

The adjuster (as you can see) has access to the order and line items and you can basically use any code you like to implement your discount offer, e.g. checking if the qty of something is over 3, and then reducing the price. It builds & returns Commerce_OrderAdjustmentModels which describe the adjustment(s) made by the adjuster.

Here's a super simple example adjuster:

<?php

namespace Commerce\Adjusters;
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];

   }

}

My advice is to being it to generate plugin scaffolding using: http://pluginfactory.io/ - to get you started - removes a lot of the pain points in getting started (such as the cursed capitilisation issues which I swear are the hardest part about getting started with plugins!).

Alternatively, you can take the scaffolding from https://github.com/lindseydiloreto/craft-businesslogic - and put in there, which makes good sense as this pretty much the textbook definition of business logic. Just clone that, add your adjuster class, and do the registration and away you go!

As long as you're comfortable with PHP implementing a discounter like you describe is really quite simple. If you're not then it's a perfect way to get more comfortable and I'm sure folks will be happy to help along the way!

| improve this answer | |
  • I guess it's time to start getting my hands dirty with plugins. I'm a front-end guy and writing plugins scares me :) – Steven Grant Mar 14 '16 at 10:27
  • 1
    Find us on Slack if you need help as you go. You'll get there! :) – Jeremy Daalder Mar 14 '16 at 11:04
  • Nice Answer. Craft Commerce is pretty new too, I'd imagine they'll be some handy plugins with nice UIs for this sort of thing to make of the pain easier ;) – RitterKnight Mar 15 '16 at 3:57
  • @StevenGrant Perhaps accept this answer? Although I agree with the above, likely a good discounts plugin is in Commerce's future, for now you'd have to do it like this. – Jeremy Daalder Mar 16 '16 at 1:17
1

Best way to collate this sort of feedback or request is here, Steven: http://feedback.craftcms.com/forums/285221-feature-requests

Let us know where you add it, so we can back it too...

Hope it helps

| improve this answer | |

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.