You are here

function course_commerce_check_cart in Course 3.x

Check if a course is already in a user's cart.

Note that we have to check if the user is purchasing the course for someone else.

File

modules/course_commerce/course_commerce.module, line 296

Code

function course_commerce_check_cart(Course $course, AccountInterface $user, $data = array()) {
  $contents = Drupal::entityTypeManager()
    ->getStorage('uc_cart_item')
    ->loadByProperties([
    'cart_id' => $user
      ->id(),
  ]);

  // Use the uid on behalf if provided.
  $uid = isset($data['uid']) ? $data['uid'] : $user
    ->id();
  foreach ($contents as $item) {

    // Check if user already has course in their cart, if the user already is
    // purchasing this course for somebody else.

    /* @var $item CartItem */
    if ($item
      ->get('nid')
      ->getString() == $node
      ->id() && (!isset($item->data['uid']) || $item->data['uid'] == $uid)) {
      return TRUE;
    }
  }
}