You are here

class CouponRedemption in Commerce Core 8.2

Same name in this branch
  1. 8.2 modules/promotion/src/Plugin/views/area/CouponRedemption.php \Drupal\commerce_promotion\Plugin\views\area\CouponRedemption
  2. 8.2 modules/promotion/src/Plugin/Commerce/CheckoutPane/CouponRedemption.php \Drupal\commerce_promotion\Plugin\Commerce\CheckoutPane\CouponRedemption
  3. 8.2 modules/promotion/src/Plugin/Commerce/InlineForm/CouponRedemption.php \Drupal\commerce_promotion\Plugin\Commerce\InlineForm\CouponRedemption

Provides the coupon redemption pane.

Plugin annotation


@CommerceCheckoutPane(
  id = "coupon_redemption",
  label = @Translation("Coupon redemption"),
  default_step = "_sidebar",
  wrapper_element = "container",
)

Hierarchy

Expanded class hierarchy of CouponRedemption

File

modules/promotion/src/Plugin/Commerce/CheckoutPane/CouponRedemption.php, line 22

Namespace

Drupal\commerce_promotion\Plugin\Commerce\CheckoutPane
View source
class CouponRedemption extends CheckoutPaneBase {

  /**
   * The inline form manager.
   *
   * @var \Drupal\commerce\InlineFormManager
   */
  protected $inlineFormManager;

  /**
   * Constructs a new CouponRedemption object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\commerce_checkout\Plugin\Commerce\CheckoutFlow\CheckoutFlowInterface $checkout_flow
   *   The parent checkout flow.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\commerce\InlineFormManager $inline_form_manager
   *   The inline form manager.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, CheckoutFlowInterface $checkout_flow, EntityTypeManagerInterface $entity_type_manager, InlineFormManager $inline_form_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $checkout_flow, $entity_type_manager);
    $this->inlineFormManager = $inline_form_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, CheckoutFlowInterface $checkout_flow = NULL) {
    return new static($configuration, $plugin_id, $plugin_definition, $checkout_flow, $container
      ->get('entity_type.manager'), $container
      ->get('plugin.manager.commerce_inline_form'));
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'allow_multiple' => FALSE,
    ] + parent::defaultConfiguration();
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationSummary() {
    if ($this->configuration['allow_multiple']) {
      $summary = $this
        ->t('Allows multiple coupons: Yes');
    }
    else {
      $summary = $this
        ->t('Allows multiple coupons: No');
    }
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    $form['allow_multiple'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Allow multiple coupons to be redeemed'),
      '#default_value' => $this->configuration['allow_multiple'],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::submitConfigurationForm($form, $form_state);
    if (!$form_state
      ->getErrors()) {
      $values = $form_state
        ->getValue($form['#parents']);
      $this->configuration['allow_multiple'] = $values['allow_multiple'];
    }
  }

  /**
   * {@inheritdoc}
   */
  public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
    $inline_form = $this->inlineFormManager
      ->createInstance('coupon_redemption', [
      'order_id' => $this->order
        ->id(),
      'max_coupons' => $this->configuration['allow_multiple'] ? NULL : 1,
    ]);
    $pane_form['form'] = [
      '#parents' => array_merge($pane_form['#parents'], [
        'form',
      ]),
    ];
    $pane_form['form'] = $inline_form
      ->buildInlineForm($pane_form['form'], $form_state);
    return $pane_form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitPaneForm(array &$pane_form, FormStateInterface $form_state, array &$complete_form) {

    // The form was submitted with a non-applied coupon in the input field,
    // mapped to a coupon ID in CouponRedemptionForm::validateForm().
    if (!empty($pane_form['form']['code']['#coupon_id'])) {
      $this->order
        ->get('coupons')
        ->appendItem($pane_form['form']['code']['#coupon_id']);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CheckoutPaneBase::$checkoutFlow protected property The parent checkout flow.
CheckoutPaneBase::$entityTypeManager protected property The entity type manager.
CheckoutPaneBase::$order protected property The current order.
CheckoutPaneBase::buildPaneSummary public function Builds a summary of the pane values. Overrides CheckoutPaneInterface::buildPaneSummary 3
CheckoutPaneBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
CheckoutPaneBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
CheckoutPaneBase::getDisplayLabel public function Gets the pane display label. Overrides CheckoutPaneInterface::getDisplayLabel
CheckoutPaneBase::getId public function Gets the pane ID. Overrides CheckoutPaneInterface::getId
CheckoutPaneBase::getLabel public function Gets the pane label. Overrides CheckoutPaneInterface::getLabel
CheckoutPaneBase::getStepId public function Gets the pane step ID. Overrides CheckoutPaneInterface::getStepId
CheckoutPaneBase::getWeight public function Gets the pane weight. Overrides CheckoutPaneInterface::getWeight
CheckoutPaneBase::getWrapperElement public function Gets the pane wrapper element. Overrides CheckoutPaneInterface::getWrapperElement
CheckoutPaneBase::isVisible public function Determines whether the pane is visible. Overrides CheckoutPaneInterface::isVisible 4
CheckoutPaneBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
CheckoutPaneBase::setOrder public function Sets the current order. Overrides CheckoutPaneInterface::setOrder
CheckoutPaneBase::setStepId public function Sets the pane step ID. Overrides CheckoutPaneInterface::setStepId
CheckoutPaneBase::setWeight public function Sets the pane weight. Overrides CheckoutPaneInterface::setWeight
CheckoutPaneBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
CheckoutPaneBase::validatePaneForm public function Validates the pane form. Overrides CheckoutPaneInterface::validatePaneForm 4
CouponRedemption::$inlineFormManager protected property The inline form manager.
CouponRedemption::buildConfigurationForm public function Form constructor. Overrides CheckoutPaneBase::buildConfigurationForm
CouponRedemption::buildConfigurationSummary public function Builds a summary of the pane configuration. Overrides CheckoutPaneBase::buildConfigurationSummary
CouponRedemption::buildPaneForm public function Builds the pane form. Overrides CheckoutPaneInterface::buildPaneForm
CouponRedemption::create public static function Creates an instance of the plugin. Overrides CheckoutPaneBase::create
CouponRedemption::defaultConfiguration public function Gets default configuration for this plugin. Overrides CheckoutPaneBase::defaultConfiguration
CouponRedemption::submitConfigurationForm public function Form submission handler. Overrides CheckoutPaneBase::submitConfigurationForm
CouponRedemption::submitPaneForm public function Handles the submission of an pane form. Overrides CheckoutPaneBase::submitPaneForm
CouponRedemption::__construct public function Constructs a new CouponRedemption object. Overrides CheckoutPaneBase::__construct
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.