Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save henideepak/333e0cb5efb94345bc47be06c88b1907 to your computer and use it in GitHub Desktop.
Save henideepak/333e0cb5efb94345bc47be06c88b1907 to your computer and use it in GitHub Desktop.
How to expire Drupal commerce licences programmaticly
function theme_preprocess_node(&$variables) {
if ($type == 'node') {
// current user role
$currentuserId = \Drupal::currentUser()->id();
// Load licenses associated with the current user.
$license_ids = \Drupal::entityQuery('commerce_license')
->condition('uid', $currentuserId)
->accessCheck(FALSE)
->condition('state', 'active')
->execute();
// Loop through each license and do something with it.
foreach ($license_ids as $license) {
$l = \Drupal::entityTypeManager()->getStorage('commerce_license')->load($license);
$role = $l->get('license_role')->getValue()[0]['target_id'];
if($role == 'buyer') {
$l->set('state', 'expired');
$l->save();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment