2

I am trying to write a Commerce 2 adjuster that will apply a discount on all items from a specific type if a customer bought a specific item with another product type in the last year as well as a minimum price.

It's my first attempt and I'm already stuck trying to retrieve the order history from a specific user. I can't find anything in the Commerce 2 docs. That's what I have so far. Please be gentle :-)

<?php
namespace craft\commerce\adjusters;

use Craft;
use craft\commerce\base\AdjusterInterface;
use craft\commerce\elements\Order;
use craft\commerce\models\OrderAdjustment;

class FriendDiscount implements AdjusterInterface
{

    // get the current user and check if the discount condition is true
    public function checkIfDonated()
    {
        $user = Craft::$app->getUser();
        if ($user) {
            // check if user bought line item with product type 'typeA' in the last 365 days and spent at least 100$ on it, then return true
            return true;
        }
        return [];
    }

    public function adjust(Order $order): array
    {

        $friendAdjuster = new OrderAdjustment();

        // apply discount if checkIfDonated() returned true
        if ($this->checkIfDonated() == true) {
            // apply discount only on items with the product type 'typeB'
            $friendAdjuster->type = "Discount";
            $friendAdjuster->name = "-14$ Off";
            $friendAdjuster->description = "-14$";
            $friendAdjuster->amount = -14;
            $friendAdjuster->orderId = $order->id;
            return [$friendAdjuster];
        }
        return [];

    }

}
2

The method to check the previous orders would look something like this:

public function checkIfDonated($order)
{

    $email = $order->getEmail();

    if (!$email) {
        return false;
    }

    $orders = Plugin::getInstance()->getOrders()->getOrdersByEmail($email);

    $didDonate = false;
    foreach ($orders as $previousOrder) {
        foreach ($previousOrder->getLineItems() as $lineItem) {
            $purchable = $lineItem->getPurchasable();
            if ($purchable && $purchable instanceof \craft\commerce\elements\Variant && $purchasable->getProduct()->getType()->handle == 'typeA') {
                $didDonate = true;
            }
        }
    }

    return $didDonate;
}

That above assumes you want to look at all orders made by anyone with that email address. You could add additional guards after the email check to see if they are logged in as a user etc. Hope that helps.

Also, don't forget to pass the order to the method: if ($this->checkIfDonated($order) == true)

And to add the Plugin import: use craft\commerce\Plugin; to the top of the adjuster class.

  • There is a missing equal in product type comparison, wasn't allowed to edit it :-) – HendrikEng Jun 27 '18 at 17:36

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.