New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue #2763705 by mglaman: Implement a usage API #695
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?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.', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. None of these are primary keys, right? We might as well drop the phrase. |
||
'type' => 'int', | ||
'unsigned' => TRUE, | ||
'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'); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,3 +84,17 @@ function commerce_promotion_post_update_4() { | |
$promotion->save(); | ||
} | ||
} | ||
|
||
/** | ||
* Remove promotion current_usage field. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Remove the promotion current_usage field." |
||
*/ | ||
function commerce_promotion_post_update_5() { | ||
$entity_definition_update = \Drupal::entityDefinitionUpdateManager(); | ||
$storage_definition = BaseFieldDefinition::create('integer') | ||
->setName('current_usage') | ||
->setTargetEntityTypeId('commerce_promotion') | ||
->setLabel(t('Current usage')) | ||
->setDescription(t('The number of times the promotion was used.')) | ||
->setDefaultValue(0); | ||
$entity_definition_update->uninstallFieldStorageDefinition($storage_definition); | ||
} |
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); | ||
} | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Untrue.