├── .gitignore ├── CHANGELOG.md ├── LICENSE.md ├── README.md └── src ├── Example.php └── adjusters ├── ExampleLineItemAdjuster.php └── ExampleOrderAdjuster.php /.gitignore: -------------------------------------------------------------------------------- 1 | # CRAFT ENVIRONMENT 2 | .env.php 3 | .env.sh 4 | .env 5 | 6 | # COMPOSER 7 | /vendor 8 | 9 | # BUILD FILES 10 | /bower_components/* 11 | /node_modules/* 12 | /build/* 13 | /yarn-error.log 14 | 15 | # MISC FILES 16 | .cache 17 | .DS_Store 18 | .idea 19 | .project 20 | .settings 21 | *.esproj 22 | *.sublime-workspace 23 | *.sublime-project 24 | *.tmproj 25 | *.tmproject 26 | .vscode/* 27 | !.vscode/settings.json 28 | !.vscode/tasks.json 29 | !.vscode/launch.json 30 | !.vscode/extensions.json 31 | config.codekit3 32 | prepros-6.config 33 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Example Adjuster Changelog 2 | 3 | The format is based on [Keep a Changelog](http://keepachangelog.com/). 4 | 5 | ## 0.0.1 - 2018-10-23 6 | ### Added 7 | - Initial release 8 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Rumors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Example Adjuster Module 2 | 3 | A simplest-case `AdjusterInterface` implementation. Scaffolded with [pluginfactory.io](https://pluginfactory.io). 4 | 5 | ## Requirements 6 | 7 | This module requires Craft CMS 3.0.0-RC1 or later, and Craft Commerce 2.0.0-beta11 8 | 9 | ## Installation 10 | 11 | To install the module, download this repository as a ZIP, unpack it, and put the folder into your project's `modules` directory, named `exampleadjuster`. 12 | 13 | Ensure the module is loaded on every request by adding entries to your `app.php` file: 14 | 15 | ```php 16 | return [ 17 | 'modules' => [ 18 | 'exampleadjuster' => [ 19 | 'class' => \modules\exampleadjuster\Example::class, 20 | ], 21 | ], 22 | 'bootstrap' => ['exampleadjuster'] 23 | ]; 24 | ``` 25 | 26 | You'll also need to make sure that you add the following to your project's `composer.json` file so that Composer can find your module: 27 | 28 | ```json 29 | "autoload": { 30 | "psr-4": { 31 | "modules\\exampleadjuster\\": "modules/exampleadjuster/src/" 32 | } 33 | } 34 | ``` 35 | 36 | After you have added this, you will need to do: 37 | 38 | ```bash 39 | composer dump-autoload 40 | ``` 41 | 42 | …from the project’s root directory, to rebuild the Composer autoload map. This will happen automatically any time you do a `composer install` or `composer update` as well. 43 | 44 | 🌳 45 | -------------------------------------------------------------------------------- /src/Example.php: -------------------------------------------------------------------------------- 1 | types` array. LineItem adjustments will happen 76 | // before the complete Order adjustment is calculated. 77 | Event::on( 78 | OrderAdjustments::class, 79 | OrderAdjustments::EVENT_REGISTER_ORDER_ADJUSTERS, 80 | function(RegisterComponentTypesEvent $event) { 81 | array_unshift( 82 | $event->types, 83 | ExampleLineItemAdjuster::class, 84 | ExampleOrderAdjuster::class 85 | ); 86 | 87 | return $event->types; 88 | }); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/adjusters/ExampleLineItemAdjuster.php: -------------------------------------------------------------------------------- 1 | 25 | * @since 0.1 26 | */ 27 | class ExampleLineItemAdjuster implements AdjusterInterface 28 | { 29 | // Constants 30 | // ========================================================================= 31 | 32 | /** 33 | * The discount adjustment type. 34 | */ 35 | const ADJUSTMENT_TYPE = 'oofbar-example-line-item'; 36 | 37 | // Public Methods 38 | // ========================================================================= 39 | 40 | /** 41 | * @inheritdoc 42 | */ 43 | public function adjust(Order $order): array 44 | { 45 | $adjustments = []; 46 | foreach ($order->lineItems as $lineItem) 47 | { 48 | $adjustment = $this->_getEmptyOrderAdjustmentFor($order); 49 | $adjustment->lineItemId = $lineItem->id; 50 | 51 | // Calculate Price difference: 52 | $basePrice = $lineItem->getSubtotal(); 53 | $discountAmount = $basePrice * -0.05; 54 | 55 | $adjustment->amount = $discountAmount; 56 | 57 | $adjustments[] = $adjustment; 58 | } 59 | 60 | return $adjustments; 61 | } 62 | 63 | // Private Methods 64 | // ========================================================================= 65 | 66 | private function _getEmptyOrderAdjustmentFor(Order $order) 67 | { 68 | $adjustment = new OrderAdjustment(); 69 | $adjustment->type = self::ADJUSTMENT_TYPE; 70 | $adjustment->name = '5% Discount'; 71 | $adjustment->orderId = $order->id; 72 | $adjustment->description = 'A discount for nice people, like you!'; 73 | $adjustment->sourceSnapshot = [ 74 | 'examplePrivateProp' => 'Criteria you want to make sure you have access to, in case you have to recalculate, later!' 75 | ]; 76 | 77 | return $adjustment; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/adjusters/ExampleOrderAdjuster.php: -------------------------------------------------------------------------------- 1 | 25 | * @since 0.1 26 | */ 27 | class ExampleOrderAdjuster implements AdjusterInterface 28 | { 29 | // Constants 30 | // ========================================================================= 31 | 32 | /** 33 | * The discount adjustment type. 34 | */ 35 | const ADJUSTMENT_TYPE = 'oofbar-example-order'; 36 | 37 | // Public Methods 38 | // ========================================================================= 39 | 40 | /** 41 | * @inheritdoc 42 | */ 43 | public function adjust(Order $order): array 44 | { 45 | $adjustments = []; 46 | $adjustment = $this->_getEmptyOrderAdjustmentFor($order); 47 | 48 | // Calculate Price difference: 49 | $basePrice = $order->getTotalPrice(); 50 | $discountAmount = $basePrice * -0.1; 51 | 52 | $adjustment->amount = $discountAmount; 53 | 54 | $adjustments[] = $adjustment; 55 | 56 | return $adjustments; 57 | } 58 | 59 | // Private Methods 60 | // ========================================================================= 61 | 62 | private function _getEmptyOrderAdjustmentFor(Order $order) 63 | { 64 | $adjustment = new OrderAdjustment(); 65 | $adjustment->type = self::ADJUSTMENT_TYPE; 66 | $adjustment->name = '10% Discount'; 67 | $adjustment->orderId = $order->id; 68 | $adjustment->description = 'A discount for nice people, like you!'; 69 | $adjustment->sourceSnapshot = [ 70 | 'examplePrivateProp' => 'Criteria you want to make sure you have access to, in case you have to recalculate, later!' 71 | ]; 72 | 73 | return $adjustment; 74 | } 75 | } 76 | --------------------------------------------------------------------------------