diff --git a/includes/plugins/license_type/base.inc b/includes/plugins/license_type/base.inc index 800f825..dfba03c 100644 --- a/includes/plugins/license_type/base.inc +++ b/includes/plugins/license_type/base.inc @@ -293,7 +293,25 @@ abstract class CommerceLicenseBase extends Entity implements CommerceLicenseInte // the expiration timestamp. Only do that if the timestamps are currently // empty (they might be set already, for example during a migration). if ($granted && empty($this->granted)) { - $this->granted = commerce_license_get_time(); + if ($this->uid != 0) { + $query = new EntityFieldQuery(); + $result = $query + ->entityCondition('entity_type', 'commerce_license') + ->propertyCondition('uid', $this->uid) + ->propertyCondition('product_id', $this->product_id) + ->propertyCondition('status', COMMERCE_LICENSE_ACTIVE) + ->propertyOrderBy('expires', 'DESC') + ->range(0,1) + ->execute(); + } + else { + // For anonymous checkout with uid 0 and existing email. New license + // will be created as renewal another license for user with uid 0, if + // license existed with the same product_id and commerce_license type for + // anonymous user. You should get email from checkout form and add it to + // query condition or use checkout only for registered users. + $result = array(); + } + + if (isset($result['commerce_license'])) { + $current_id = array_shift(array_keys($result['commerce_license'])); + $license = entity_load_single('commerce_license', $current_id); + $this->granted = $license->expires; + } + else { + $this->granted = commerce_license_get_time(); + } + $duration = $this->wrapper->product->commerce_license_duration->value(); if ($duration > 0 && empty($this->expires)) { $this->expires = $this->granted + $duration;