Created
April 19, 2023 11:22
-
-
Save henideepak/333e0cb5efb94345bc47be06c88b1907 to your computer and use it in GitHub Desktop.
How to expire Drupal commerce licences programmaticly
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
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