class Donation in Commerce Donate 8
Provides the donation pane.
Plugin annotation
@CommerceCheckoutPane(
  id = "donation",
  label = @Translation("Donation"),
  default_step = "order_information",
  wrapper_element = "fieldset",
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait- class \Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneBase implements CheckoutPaneInterface, ContainerFactoryPluginInterface- class \Drupal\commerce_donate\Plugin\Commerce\CheckoutPane\Donation implements CheckoutPaneInterface
 
 
- class \Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneBase implements CheckoutPaneInterface, ContainerFactoryPluginInterface
 
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of Donation
File
- src/Plugin/ Commerce/ CheckoutPane/ Donation.php, line 23 
Namespace
Drupal\commerce_donate\Plugin\Commerce\CheckoutPaneView source
class Donation extends CheckoutPaneBase implements CheckoutPaneInterface {
  /**
   * {@inheritdoc}
   */
  public function isVisible() {
    // Hide the pane if there's already a donation order item?
    $order_item = $this
      ->getOrderItem();
    if ($order_item) {
      return TRUE;
    }
    else {
      return FALSE;
    }
  }
  /**
   * {@inheritdoc}
   */
  public function buildPaneSummary() {
    $summary = [];
    if ($this
      ->isVisible()) {
      $order_item = $this
        ->getOrderItem();
      // Expand this to provide the appropriate output at checkout review.
      $summary = [
        '#plain_text' => $order_item
          ->label(),
      ];
    }
    return $summary;
  }
  /**
   * {@inheritdoc}
   */
  public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
    $current_currency = \Drupal::service('commerce_currency_resolver.current_currency');
    $selected_currency = $current_currency
      ->getCurrency();
    $entity_type_manager = \Drupal::service('entity_type.manager');
    $currency = $entity_type_manager
      ->getStorage('commerce_currency')
      ->load($selected_currency);
    $currency_symbol = $currency
      ->getSymbol();
    $predefined_amounts = [
      '50' => $currency_symbol . '50',
      '100' => $currency_symbol . '100',
      '250' => $currency_symbol . '250',
    ];
    $predefined_amount_keys = array_keys($predefined_amounts);
    $order_item = $this
      ->getOrderItem();
    $unit_price = $order_item
      ->getUnitPrice();
    $amount = $unit_price ? Calculator::trim($unit_price
      ->getNumber()) : reset($predefined_amount_keys);
    $pane_form['donation'] = [
      '#type' => 'checkbox',
      '#title' => t('I would like to make a donation'),
      '#default_value' => $unit_price ? '1' : '0',
    ];
    $pane_form['details'] = [
      '#type' => 'fieldset',
      '#states' => [
        'visible' => [
          ':input[name="donation[donation]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $pane_form['details']['amount'] = [
      '#type' => 'select_or_other_buttons',
      '#title' => t('I would like to Donate'),
      '#options' => $predefined_amounts,
      '#default_value' => $amount,
      '#required' => TRUE,
    ];
    $pane_form['details']['in_memory'] = [
      '#type' => 'checkbox',
      '#title' => t('I wish to make this donation in memory of someone'),
      '#default_value' => $order_item->field_in_memory->value,
    ];
    $pane_form['details']['in_memory_name'] = [
      '#type' => 'textfield',
      '#title' => t('Donate in memory of'),
      '#placeholder' => t("Enter person's name"),
      '#default_value' => $order_item->field_in_memory_name->value,
      '#states' => [
        'visible' => [
          ':input[name="donation[details][in_memory]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $pane_form['details']['in_memory_memorial'] = [
      '#type' => 'checkbox',
      '#title' => t('Receive an In Memory Card.'),
      '#default_value' => $order_item->field_in_memory_memorial->value,
      '#states' => [
        'visible' => [
          ':input[name="donation[details][in_memory]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    return $pane_form;
  }
  /**
   * {@inheritdoc}
   */
  public function validatePaneForm(array &$pane_form, FormStateInterface $form_state, array &$complete_form) {
    $values = $form_state
      ->getValue($pane_form['#parents']);
    $amount = $values['details']['amount'][0];
    if (!is_numeric($amount)) {
      $form_state
        ->setError($pane_form['details']['amount'], t('The amount must be a valid number.'));
    }
  }
  /**
   * {@inheritdoc}
   */
  public function submitPaneForm(array &$pane_form, FormStateInterface $form_state, array &$complete_form) {
    $order_item = $this
      ->getOrderItem();
    $values = $form_state
      ->getValue($pane_form['#parents']);
    $amount = $values['details']['amount'][0];
    $make_donation = $values['donation'];
    $current_currency = \Drupal::service('commerce_currency_resolver.current_currency');
    $selected_currency = $current_currency
      ->getCurrency();
    $entity_type_manager = \Drupal::service('entity_type.manager');
    $currency = $entity_type_manager
      ->getStorage('commerce_currency')
      ->load($selected_currency);
    $currency_symbol = $currency
      ->getSymbol();
    $currency_formatter = \Drupal::service('commerce_price.currency_formatter');
    $amount_label = $currency_formatter
      ->format($amount, $selected_currency);
    $order_item->title = t('@amount donation', [
      '@amount' => $amount_label,
    ]);
    $order_item->unit_price = [
      'number' => $amount,
      'currency_code' => $selected_currency,
    ];
    $order_item->field_in_memory = $values['details']['in_memory'];
    $order_item->field_in_memory_name = $values['details']['in_memory_name'];
    $order_item->field_in_memory_memorial = $values['details']['in_memory_memorial'];
    $order_item
      ->save();
    // Add or update Donation Line item.
    if (!$this->order
      ->hasItem($order_item) && $make_donation) {
      $this->order
        ->addItem($order_item);
    }
    // Remove Donation if required.
    if (!$make_donation && $this->order
      ->hasItem($order_item)) {
      $this->order
        ->removeItem($order_item);
    }
  }
  /**
   * Gets the donation order item.
   *
   * If one isn't found, it will be created.
   *
   * @return \Drupal\commerce_order\Entity\OrderItemInterface
   *   The donation order item.
   */
  protected function getOrderItem() {
    $donation_order_item = NULL;
    // Try to find an existing order item.
    foreach ($this->order
      ->getItems() as $order_item) {
      if ($order_item
        ->bundle() == 'donation') {
        $donation_order_item = $order_item;
        break;
      }
    }
    return $donation_order_item;
  }
}Members
| Name   | Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| CheckoutPaneBase:: | protected | property | The parent checkout flow. | |
| CheckoutPaneBase:: | protected | property | The entity type manager. | |
| CheckoutPaneBase:: | protected | property | The current order. | |
| CheckoutPaneBase:: | public | function | Form constructor. Overrides PluginFormInterface:: | 6 | 
| CheckoutPaneBase:: | public | function | Builds a summary of the pane configuration. Overrides CheckoutPaneInterface:: | 5 | 
| CheckoutPaneBase:: | public | function | Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: | |
| CheckoutPaneBase:: | public static | function | Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: | 7 | 
| CheckoutPaneBase:: | public | function | Gets default configuration for this plugin. Overrides ConfigurableInterface:: | 6 | 
| CheckoutPaneBase:: | public | function | Gets this plugin's configuration. Overrides ConfigurableInterface:: | |
| CheckoutPaneBase:: | public | function | Gets the pane display label. Overrides CheckoutPaneInterface:: | |
| CheckoutPaneBase:: | public | function | Gets the pane ID. Overrides CheckoutPaneInterface:: | |
| CheckoutPaneBase:: | public | function | Gets the pane label. Overrides CheckoutPaneInterface:: | |
| CheckoutPaneBase:: | public | function | Gets the pane step ID. Overrides CheckoutPaneInterface:: | |
| CheckoutPaneBase:: | public | function | Gets the pane weight. Overrides CheckoutPaneInterface:: | |
| CheckoutPaneBase:: | public | function | Gets the pane wrapper element. Overrides CheckoutPaneInterface:: | |
| CheckoutPaneBase:: | public | function | Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: | |
| CheckoutPaneBase:: | public | function | Sets the current order. Overrides CheckoutPaneInterface:: | |
| CheckoutPaneBase:: | public | function | Sets the pane step ID. Overrides CheckoutPaneInterface:: | |
| CheckoutPaneBase:: | public | function | Sets the pane weight. Overrides CheckoutPaneInterface:: | |
| CheckoutPaneBase:: | public | function | Form submission handler. Overrides PluginFormInterface:: | 6 | 
| CheckoutPaneBase:: | public | function | Form validation handler. Overrides PluginFormInterface:: | |
| CheckoutPaneBase:: | public | function | Constructs a new CheckoutPaneBase object. Overrides PluginBase:: | 6 | 
| DependencySerializationTrait:: | protected | property | An array of entity type IDs keyed by the property name of their storages. | |
| DependencySerializationTrait:: | protected | property | An array of service IDs keyed by property name used for serialization. | |
| DependencySerializationTrait:: | public | function | 1 | |
| DependencySerializationTrait:: | public | function | 2 | |
| Donation:: | public | function | Builds the pane form. Overrides CheckoutPaneInterface:: | |
| Donation:: | public | function | Builds a summary of the pane values. Overrides CheckoutPaneBase:: | |
| Donation:: | protected | function | Gets the donation order item. | |
| Donation:: | public | function | Determines whether the pane is visible. Overrides CheckoutPaneBase:: | |
| Donation:: | public | function | Handles the submission of an pane form. Overrides CheckoutPaneBase:: | |
| Donation:: | public | function | Validates the pane form. Overrides CheckoutPaneBase:: | |
| MessengerTrait:: | protected | property | The messenger. | 29 | 
| MessengerTrait:: | public | function | Gets the messenger. | 29 | 
| MessengerTrait:: | public | function | Sets the messenger. | |
| PluginBase:: | protected | property | Configuration information passed into the plugin. | 1 | 
| PluginBase:: | protected | property | The plugin implementation definition. | 1 | 
| PluginBase:: | protected | property | The plugin_id. | |
| PluginBase:: | constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
| PluginBase:: | public | function | Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: | |
| PluginBase:: | public | function | Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: | |
| PluginBase:: | public | function | Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: | 3 | 
| PluginBase:: | public | function | Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: | |
| PluginBase:: | public | function | Determines if the plugin is configurable. | |
| StringTranslationTrait:: | protected | property | The string translation service. | 1 | 
| StringTranslationTrait:: | protected | function | Formats a string containing a count of items. | |
| StringTranslationTrait:: | protected | function | Returns the number of plurals supported by a given language. | |
| StringTranslationTrait:: | protected | function | Gets the string translation service. | |
| StringTranslationTrait:: | public | function | Sets the string translation service to use. | 2 | 
| StringTranslationTrait:: | protected | function | Translates a string to the current language or to a given language. | 
