Skip to content

Instantly share code, notes, and snippets.

@AugustMiller
Last active January 9, 2020 09:53
Show Gist options
  • Save AugustMiller/7acd512fc7085c3abcce3f9b44a22576 to your computer and use it in GitHub Desktop.
Save AugustMiller/7acd512fc7085c3abcce3f9b44a22576 to your computer and use it in GitHub Desktop.
Simple Commerce Adjuster implementation
<?php
/**
* Example Adjuster.
* Place in: `/myplugin/adjusters/Test.php` (and rename as such)
*
* @link https://oof.studio/
* @copyright Copyright (c) 2018 oof. Studio
*/
namespace oofbar\test\adjusters;
use Craft;
use craft\base\Component;
use craft\commerce\base\AdjusterInterface;
use craft\commerce\elements\Order;
use craft\commerce\models\OrderAdjustment;
use oofbar\test\Test;
/**
* Example Adjuster
*
* 5% discount on all line items.
*
* @author oof. Studio <hello@oof.studio>
* @since 0.1
*/
class Test extends Component implements AdjusterInterface
{
// Constants
// =========================================================================
/**
* The discount adjustment type.
*/
const ADJUSTMENT_TYPE = 'test-example';
// Public Methods
// =========================================================================
/**
* @inheritdoc
*/
public function adjust(Order $order): array
{
$adjustments = [];
foreach ($order->lineItems as $lineItem)
{
$adjustment = $this->_getEmptyAdjustment($order);
$adjustment->setLineItem($lineItem);
// Calculate Price diff:
$basePrice = $lineItem->getSubtotal();
$discountAmount = $basePrice * -0.05;
$adjustment->amount = $discountAmount;
$adjustments[] = $adjustment;
}
return $adjustments;
}
// Private Methods
// =========================================================================
private function _getEmptyAdjustment(Order $order)
{
$adjustment = new OrderAdjustment();
$adjustment->type = self::ADJUSTMENT_TYPE;
$adjustment->name = '5% Discount';
$adjustment->orderId = $order->id;
$adjustment->description = 'Discount for nice people';
$adjustment->sourceSnapshot = [
'privateProp' => 'Criteria you want to make sure you have access to, in case you have to recalculate, later!'
];
return $adjustment;
}
}
<?php
/**
* Example Adjuster plugin.
* Place in: `/myplugin/Test.php` (and rename as such)
*
* @link https://oof.studio/
* @copyright Copyright (c) 2018 oof. Studio
*/
namespace oofbar\test;
use oofbar\test\adjusters\Test as TestAdjuster;
use Craft;
use craft\base\Plugin as BasePlugin;
use craft\events\RegisterComponentTypesEvent;
use craft\commerce\services\OrderAdjustments;
use yii\base\Event;
/**
* Test Plugin
*
* @author oof. Studio
* @package Test
* @since 0.1
*/
class Test extends BasePlugin
{
// Static Properties
// =========================================================================
/**
* Static property that is an instance of this plugin class so that it can be accessed via
* Test::$plugin
*
* @var Plugin
*/
public static $plugin;
// Public Methods
// =========================================================================
/**
* Set our $plugin static property to this class so that it can be accessed via
* Test::$plugin
*
* Called after the plugin class is instantiated; do any one-time initialization
* here such as hooks and events.
*
* If you have a '/vendor/autoload.php' file, it will be loaded for you automatically;
* you do not need to load it in your init() method.
*
*/
public function init()
{
parent::init();
self::$plugin = $this;
// Add our custom Adjusters, pushing them onto the front of the queue (so they take precedence over Tax/Discounts/Shipping):
Event::on(
OrderAdjustments::class,
OrderAdjustments::EVENT_REGISTER_ORDER_ADJUSTERS,
function(RegisterComponentTypesEvent $event) {
array_unshift(
$event->types,
TestAdjuster::class
);
return $event->types;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment