You are here

function CourseObjectCommerce::take in Course 3.x

Display the add to cart button, or a message that it was already purchased.

Implementing classes should override me if a custom display is desired.

Overrides CourseObject::take

File

modules/course_commerce/src/Plugin/course/CourseObject/CourseObjectCommerce.php, line 36

Class

CourseObjectCommerce
Plugin annotation @CourseObject( id = "commerce", label = "Payment", )

Namespace

Drupal\course_commerce\Plugin\course\CourseObject

Code

function take() {

  /* @var $product \Drupal\commerce_product\Entity\Product */
  $product = $this
    ->getCourse()
    ->get('commerce_product')
    ->referencedEntities()[0];
  $variation = $product
    ->get('variations')
    ->referencedEntities()[0];
  $user = Drupal::currentUser();
  $bypass = Drupal::config('course_commerce.settings')
    ->get('bypass_checkout');
  if ($product
    ->get('variations')
    ->isEmpty()) {

    // Product was not specified.
    $this
      ->getFulfillment($user)
      ->setComplete(1)
      ->save();
  }
  if ($this
    ->getFulfillment($user)
    ->isComplete()) {
    return "You've already purchased this activity.";
  }
  else {
    $view_builder = Drupal::entityTypeManager()
      ->getViewBuilder('commerce_product');
    $out = $view_builder
      ->view($product);
    $out['message']['#markup'] = t('You must purchase this course before proceeding.');
    if (!$out) {

      // The add to cart form was hidden?
      return "Something isn't right...";
    }
    else {
      return $out;
    }
  }
}