Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #2763705 by mglaman: Implement a usage API
- Loading branch information
Showing
22 changed files
with
615 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,7 @@ promotion: | |
label: Promotion | ||
has_ui: true | ||
weight: 0 | ||
promotion_coupon: | ||
label: Coupon | ||
has_ui: true | ||
weight: 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Install, update and uninstall functions for the commerce_promotion module. | ||
*/ | ||
|
||
/** | ||
* Implements hook_schema(). | ||
*/ | ||
function commerce_promotion_schema() { | ||
$schema['commerce_promotion_usage'] = [ | ||
'description' => 'Stores module data as key/value pairs per user.', | ||
'fields' => [ | ||
'promotion_id' => [ | ||
'description' => 'Primary key: {commerce_promotion}.promotion_id for promotion.', | ||
'type' => 'int', | ||
'unsigned' => TRUE, | ||
'not null' => TRUE, | ||
'default' => 0, | ||
], | ||
'coupon_id' => [ | ||
'description' => 'Primary key: {commerce_promotion_coupon}.id for coupon.', | ||
'type' => 'int', | ||
'unsigned' => TRUE, | ||
'not null' => FALSE, | ||
'default' => 0, | ||
], | ||
'order_id' => [ | ||
'description' => 'Primary key: {commerce_order}.order_id for order.', | ||
'type' => 'int', | ||
'unsigned' => TRUE, | ||
'not null' => TRUE, | ||
'default' => 0, | ||
], | ||
'uid' => [ | ||
'description' => 'Primary key: {users}.uid for user.', | ||
'type' => 'int', | ||
'unsigned' => TRUE, | ||
'not null' => FALSE, | ||
'default' => 0, | ||
], | ||
], | ||
'primary key' => ['promotion_id', 'coupon_id', 'order_id'], | ||
'indexes' => [ | ||
'promotion_id' => ['promotion_id'], | ||
'coupon_id' => ['coupon_id'], | ||
], | ||
'foreign keys' => [ | ||
'promotion_id' => ['commerce_promotion' => 'promotion_id'], | ||
'coupon_id' => ['commerce_promotion_coupon' => 'id'], | ||
'order_id' => ['commerce_order' => 'order_id'], | ||
'uid' => ['users' => 'uid'], | ||
], | ||
]; | ||
|
||
return $schema; | ||
} | ||
|
||
/** | ||
* Install the `commerce_promotion_usage` table schema. | ||
*/ | ||
function commerce_promotion_update_8001() { | ||
drupal_install_schema('commerce_promotion'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
modules/promotion/src/EventSubscriber/OrderEventSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
namespace Drupal\commerce_promotion\EventSubscriber; | ||
|
||
use Drupal\commerce_promotion\PromotionUsageInterface; | ||
use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Drupal\state_machine\Event\WorkflowTransitionEvent; | ||
|
||
class OrderEventSubscriber implements EventSubscriberInterface { | ||
|
||
/** | ||
* The promotion storage. | ||
* | ||
* @var \Drupal\commerce_promotion\PromotionStorageInterface | ||
*/ | ||
protected $promotionStorage; | ||
|
||
/** | ||
* The coupon storage. | ||
* | ||
* @var \Drupal\commerce_promotion\CouponStorageInterface | ||
*/ | ||
protected $couponStorage; | ||
|
||
/** | ||
* The promotion usage. | ||
* | ||
* @var \Drupal\commerce_promotion\PromotionUsageInterface | ||
*/ | ||
protected $usage; | ||
|
||
/** | ||
* Constructs a new OrderEventSubscriber object. | ||
* | ||
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager | ||
* The entity type manager. | ||
* @param \Drupal\commerce_promotion\PromotionUsageInterface $usage | ||
* The promotion usage. | ||
*/ | ||
public function __construct(EntityTypeManagerInterface $entity_type_manager, PromotionUsageInterface $usage) { | ||
$this->promotionStorage = $entity_type_manager->getStorage('commerce_promotion'); | ||
$this->couponStorage = $entity_type_manager->getStorage(('commerce_promotion_coupon')); | ||
$this->usage = $usage; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents() { | ||
$events = [ | ||
'commerce_order.place.pre_transition' => 'addUsage', | ||
]; | ||
return $events; | ||
} | ||
|
||
/** | ||
* Adds promotion usage when cart placed. | ||
* | ||
* @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event | ||
* The workflow transition event. | ||
* | ||
* @todo Investigate moving to kernel terminate somehow, for performance. | ||
*/ | ||
public function addUsage(WorkflowTransitionEvent $event) { | ||
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */ | ||
$order = $event->getEntity(); | ||
/** @var \Drupal\commerce_order\Adjustment[] $adjustments */ | ||
$adjustments = $order->collectAdjustments(); | ||
|
||
foreach ($adjustments as $adjustment) { | ||
if ($adjustment->getType() == 'promotion') { | ||
$promotion = $this->promotionStorage->load($adjustment->getSourceId()); | ||
$this->usage->addUsage($order, $promotion); | ||
} | ||
if ($adjustment->getType() == 'promotion_coupon') { | ||
/** @var \Drupal\commerce_promotion\Entity\CouponInterface $coupon */ | ||
$coupon = $this->couponStorage->load($adjustment->getSourceId()); | ||
$this->usage->addUsage($order, $coupon->getPromotion(), $coupon); | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.