Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save junaidpv/c3e8a84426c6d6a6a6372c04dca8143b to your computer and use it in GitHub Desktop.
Save junaidpv/c3e8a84426c6d6a6a6372c04dca8143b to your computer and use it in GitHub Desktop.
Patch to be used on top of https://www.drupal.org/project/commerce_license/issues/2943888#comment-14532157 to fix the issue allow re-purchaing differnet PV role licesnse. This patch is specific for MHCC site.
diff --git a/src/Entity/License.php b/src/Entity/License.php
index 6e18a95..e160a43 100644
--- a/src/Entity/License.php
+++ b/src/Entity/License.php
@@ -12,6 +12,7 @@ use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
+use Drupal\commerce_order\Entity\OrderItemInterface;
/**
* Defines the License entity.
@@ -562,4 +563,41 @@ class License extends ContentEntityBase implements LicenseInterface {
}
}
+ /**
+ * Created by combining logics from LicenseStorage::createFromOrderItem() and LicenseStorage::createFromProductVariation()
+ * To apply new product variation to existing license.
+ */
+ public function applyOrderItem(OrderItemInterface $order_item) {
+
+ $purchased_entity = $order_item->getPurchasedEntity();
+
+ if ($purchased_entity->bundle() != 'mhcc_membership') {
+ return;
+ }
+
+ // Take the license owner from the order, for the case when orders are
+ // created for another user.
+ $order = $order_item->getOrder();
+ $uid = $order->getCustomerId();
+ $this->setOriginatingOrder($order);
+
+
+ // @todo throw an exception if the variation doesn't have this field.
+ $license_type_plugin = $purchased_entity->get('license_type')->first()->getTargetInstance();
+
+ $this->set('type', $license_type_plugin->getPluginId());
+
+ // Keep in renewal_in_progress state?
+ // $this->set('state', 'active');
+
+ $this->set('product_variation', $purchased_entity->id());
+ $this->set('expiration_type', $purchased_entity->get('license_expiration')->first()->getValue());
+
+ // Set the license's plugin-specific configuration from the
+ // product variation's license_type field plugin instance.
+ $this->setValuesFromPlugin($license_type_plugin);
+
+ $this->save();
+ }
+
}
diff --git a/src/EventSubscriber/LicenseRenewalCartEventSubscriber.php b/src/EventSubscriber/LicenseRenewalCartEventSubscriber.php
index ba000b8..8ddcd5d 100644
--- a/src/EventSubscriber/LicenseRenewalCartEventSubscriber.php
+++ b/src/EventSubscriber/LicenseRenewalCartEventSubscriber.php
@@ -117,6 +117,13 @@ class LicenseRenewalCartEventSubscriber implements EventSubscriberInterface {
$existing_license->save();
}
+ if ($variation->bundle() == 'mhcc_membership') {
+ // To help show current extended time.
+ // Make sure not save license with this temp values.
+ $existing_license->set('product_variation', $variation->id());
+ $existing_license->set('expiration_type', $variation->get('license_expiration')->first()->getValue());
+ }
+
// Shows a message with existing and extended dates when order completed.
$expiresTime = $existing_license->getExpiresTime();
$datetime = (new \DateTimeImmutable())->setTimestamp($expiresTime);
diff --git a/src/EventSubscriber/OrderSubscriber.php b/src/EventSubscriber/OrderSubscriber.php
index 85c1b22..80fb0d1 100644
--- a/src/EventSubscriber/OrderSubscriber.php
+++ b/src/EventSubscriber/OrderSubscriber.php
@@ -101,6 +101,9 @@ class OrderSubscriber implements EventSubscriberInterface {
if (!$license) {
$license = $this->createLicenseFromOrderItem($order_item);
}
+ else {
+ $license->applyOrderItem($order_item);
+ }
$purchased_entity = $order_item->getPurchasedEntity();
$product_variation_type = $product_variation_type_storage->load($purchased_entity->bundle());
$activate_on_place = $product_variation_type->getThirdPartySetting('commerce_license', 'activate_on_place');
diff --git a/src/LicenseStorage.php b/src/LicenseStorage.php
index 94d45aa..407ab38 100644
--- a/src/LicenseStorage.php
+++ b/src/LicenseStorage.php
@@ -60,11 +60,27 @@ class LicenseStorage extends CommerceContentEntityStorage implements LicenseStor
* {@inheritdoc}
*/
public function getExistingLicense(ProductVariationInterface $variation, $uid) {
- $existing_licenses_ids = $this->getQuery()
+ if ($variation->bundle() == 'mhcc_membership') {
+ $product_variation_storage = \Drupal::entityTypeManager()->getStorage($variation->getEntityTypeId());
+ $product_variations = $product_variation_storage->loadByProperties(['product_id' => $variation->getProductId()]);
+ if (!empty($product_variations)) {
+ $existing_licenses_ids = $this->getQuery()
+ ->condition('state', ['active', 'renewal_in_progress'], 'IN')
+ ->condition('uid', $uid)
+ ->condition('product_variation', array_keys($product_variations), 'IN')
+ ->execute();
+ }
+ else {
+ $existing_licenses_ids = [];
+ }
+ }
+ else {
+ $existing_licenses_ids = $this->getQuery()
->condition('state', ['active', 'renewal_in_progress'], 'IN')
->condition('uid', $uid)
->condition('product_variation', $variation->id())
->execute();
+ }
if (!empty($existing_licenses_ids)) {
$existing_license_id = array_shift($existing_licenses_ids);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment