You are here

CourseObjectCommerce.php in Course 3.x

File

modules/course_commerce/src/Plugin/course/CourseObject/CourseObjectCommerce.php
View source
<?php

namespace Drupal\course_commerce\Plugin\course\CourseObject;

use Drupal;
use Drupal\Core\Form\FormStateInterface;
use Drupal\course\Entity\CourseObject;
use function drupal_render;
use function node_view;
use function uc_product_types;

/**
 * @CourseObject(
 *   id = "commerce",
 *   label = "Payment",
 * )
 */
class CourseObjectCommerce extends CourseObject {

  /**
   * Specify that this object needs a navigation listener.
   */
  public function hasPolling() {
    return TRUE;
  }
  function getTakeType() {
    return 'content';
  }

  /**
   * 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.
   */
  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;
      }
    }
  }
  public function optionsForm(&$form, FormStateInterface $form_state) {
    parent::optionsForm($form, $form_state);
    $form['node']['instance']['#title'] = t('Product to check');
    $form['node']['instance']['#description'] .= '</br>' . t('Use an existing ubercart-enabled product for checking payment. Defaults to this course.');
  }

  /**
   * Set a specialized instance ID.
   *
   * Either the course node ID, or another ubercart node ID.
   */
  public function optionsSubmit(&$form, FormStateInterface $form_state) {
    if (isset($form_state['values']['instance'])) {
      if (!$form_state['values']['instance']) {
        $form_state['values']['instance'] = $this
          ->getCourse()
          ->id();
      }
    }
    parent::optionsSubmit($form, $form_state);
  }

  /**
   * Don't create new products.
   */
  public function createInstance($node = NULL) {
    if (!$this
      ->getInstanceId()) {
      $this
        ->setOption('instance', $this
        ->getCourse()
        ->id());
    }
  }
  function getNodeTypes() {
    return uc_product_types();
  }
  function getCloneAbility() {
    return t('Payment objects will be created as a reference to the new course');
  }
  public function freeze() {
    return TRUE;
  }
  function thaw($ice) {

    // Not creating a new product. Using this course.
    return $this
      ->getCourse()
      ->id();
  }

}

Classes

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