You are here

function course_commerce_check_purchased in Course 3.x

Check if a user purchased a course previously.

1 call to course_commerce_check_purchased()
course_commerce_course_access in modules/course_commerce/course_commerce.module
Implements hook_course_access().

File

modules/course_commerce/course_commerce.module, line 275

Code

function course_commerce_check_purchased(Course $course, AccountInterface $account, $data = array()) {
  $uid = $account
    ->id();
  $orders = Drupal::entityTypeManager()
    ->getStorage('commerce_order')
    ->loadByProperties(array(
    'uid' => $uid,
  ));
  foreach ($orders as $order) {
    if ($order->order_status == 'complete') {
      foreach ($order->products as $product) {
        if ($product->nid == $nid) {
          return TRUE;
        }
      }
    }
  }
  return FALSE;
}