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
12 changed files
with
556 additions
and
12 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 |
---|---|---|
@@ -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
79 changes: 79 additions & 0 deletions
79
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,79 @@ | ||
<?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 promotion usage. | ||
* | ||
* @var \Drupal\commerce_promotion\PromotionUsageInterface | ||
*/ | ||
protected $usage; | ||
|
||
/** | ||
* Constructs a new OrderEventSubscriber object. | ||
* | ||
* @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->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(); | ||
|
||
// @todo The offer plugin sets the adjustment. So we have no way of knowing | ||
// the source promotion or coupon for an adjustment being applied. Also, with | ||
// the current \Drupal\commerce_promotion\Entity\PromotionInterface::apply | ||
// method there is know way to know it was applied by a coupon. | ||
foreach ($adjustments as $adjustment) { | ||
if ($adjustment->getType() == 'promotion') { | ||
$promotion = $this->promotionStorage->load($adjustment->getSourceId()); | ||
$this->usage->addPromotionUsage($promotion, $order); | ||
} | ||
} | ||
|
||
/** @var \Drupal\commerce_promotion\Entity\CouponInterface[] $coupons */ | ||
$coupons = $order->get('coupons')->referencedEntities(); | ||
foreach ($coupons as $coupon) { | ||
$this->usage->addCouponUsage($coupon, $order); | ||
} | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace Drupal\commerce_promotion; | ||
|
||
use Drupal\commerce_order\Entity\OrderInterface; | ||
use Drupal\commerce_promotion\Entity\CouponInterface; | ||
use Drupal\commerce_promotion\Entity\PromotionInterface; | ||
use Drupal\Core\Database\Connection; | ||
|
||
class PromotionUsage implements PromotionUsageInterface { | ||
|
||
/** | ||
* The database connection to use. | ||
* | ||
* @var \Drupal\Core\Database\Connection | ||
*/ | ||
protected $connection; | ||
|
||
/** | ||
* Constructs a PromotionUsage object. | ||
* | ||
* @param \Drupal\Core\Database\Connection $connection | ||
* The database connection to use. | ||
*/ | ||
public function __construct(Connection $connection) { | ||
$this->connection = $connection; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getPromotionUsage(PromotionInterface $promotion) { | ||
$count = $this->connection->select('commerce_promotion_usage', 'cpu') | ||
->condition('promotion_id', $promotion->id()) | ||
->countQuery() | ||
->execute() | ||
->fetchField(); | ||
return $count; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function addPromotionUsage(PromotionInterface $promotion, OrderInterface $order) { | ||
$this->connection->insert('commerce_promotion_usage') | ||
->fields([ | ||
'promotion_id' => $promotion->id(), | ||
'order_id' => $order->id(), | ||
'uid' => $order->getCustomerId(), | ||
]) | ||
->execute(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getCouponUsage(CouponInterface $coupon) { | ||
$count = $this->connection->select('commerce_promotion_usage', 'cpu') | ||
->condition('coupon_id', $coupon->id()) | ||
->countQuery() | ||
->execute() | ||
->fetchField(); | ||
return $count; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function addCouponUsage(CouponInterface $coupon, OrderInterface $order) { | ||
$this->connection->upsert('commerce_promotion_usage') | ||
->key(['promotion_id' => $coupon->getPromotionId(), 'order_id' => $order->id(), 'uid' => $order->getCustomerId()]) | ||
->fields([ | ||
'promotion_id' => $coupon->getPromotionId(), | ||
'coupon_id' => $coupon->id(), | ||
'order_id' => $order->id(), | ||
'uid' => $order->getCustomerId(), | ||
]) | ||
->execute(); | ||
} | ||
|
||
} |
Oops, something went wrong.