You are here

class QuotePane in Ubercart 8.4

Shipping quote checkout pane plugin.

Plugin annotation


@CheckoutPane(
  id = "quotes",
  title = @Translation("Calculate shipping cost"),
  weight = 5,
  shippable = TRUE
)

Hierarchy

Expanded class hierarchy of QuotePane

File

shipping/uc_quote/src/Plugin/Ubercart/CheckoutPane/QuotePane.php, line 20

Namespace

Drupal\uc_quote\Plugin\Ubercart\CheckoutPane
View source
class QuotePane extends CheckoutPanePluginBase {

  /**
   * {@inheritdoc}
   */
  public function view(OrderInterface $order, array $form, FormStateInterface $form_state) {
    $contents['#description'] = $this
      ->t('Shipping quotes are generated automatically when you enter your address and may be updated manually with the button below.');
    $contents['#attached']['library'][] = 'uc_quote/uc_quote.styles';
    $contents['uid'] = [
      '#type' => 'hidden',
      '#value' => \Drupal::currentUser()
        ->id(),
    ];
    $contents['quote_button'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Click to calculate shipping'),
      '#submit' => [
        [
          $this,
          'paneSubmit',
        ],
      ],
      '#weight' => 0,
      '#ajax' => [
        'effect' => 'slide',
        'progress' => [
          'type' => 'bar',
          'message' => $this
            ->t('Receiving quotes...'),
        ],
      ],
      // Shipping quotes can be retrieved even if the form doesn't validate.
      '#limit_validation_errors' => [],
    ];
    $contents['quotes'] = [
      '#type' => 'container',
      '#attributes' => [
        'id' => 'quote',
      ],
      '#tree' => TRUE,
      '#weight' => 1,
    ];

    // If this was an Ajax request, we reinvoke the 'prepare' op to ensure
    // that we catch any changes in panes heavier than this one.
    if ($form_state
      ->getTriggeringElement()) {
      $this
        ->prepare($order, $form, $form_state);
    }
    $contents['quotes'] += $order->quote_form;
    $form_state
      ->set([
      'uc_ajax',
      'uc_quote',
      'panes][quotes][quote_button',
    ], [
      'payment-pane' => '::ajaxReplaceCheckoutPane',
      'quotes-pane' => '::ajaxReplaceCheckoutPane',
    ]);
    $form_state
      ->set([
      'uc_ajax',
      'uc_quote',
      'panes][quotes][quotes][quote_option',
    ], [
      'payment-pane' => '::ajaxReplaceCheckoutPane',
    ]);
    return $contents;
  }

  /**
   * {@inheritdoc}
   */
  public function prepare(OrderInterface $order, array $form, FormStateInterface $form_state) {

    // If a quote was explicitly selected, add it to the order.
    if (isset($form['panes']['quotes']['quotes']['quote_option']['#value']) && isset($form['panes']['quotes']['quotes']['quote_option']['#default_value']) && $form['panes']['quotes']['quotes']['quote_option']['#value'] !== $form['panes']['quotes']['quotes']['quote_option']['#default_value']) {
      $quote_option = explode('---', $form_state
        ->getValue([
        'panes',
        'quotes',
        'quotes',
        'quote_option',
      ]));
      $order->quote['method'] = $quote_option[0];
      $order->quote['accessorials'] = $quote_option[1];
      $order->data->uc_quote_selected = TRUE;
    }

    // If the current quote was never explicitly selected, discard it and
    // use the default.
    if (empty($order->data->uc_quote_selected)) {
      unset($order->quote);
    }

    // Ensure that the form builder uses the default value to decide which
    // radio button should be selected.
    $input = $form_state
      ->getUserInput();
    unset($input['panes']['quotes']['quotes']['quote_option']);
    $form_state
      ->setUserInput($input);
    $order->quote_form = uc_quote_build_quote_form($order, !$form_state
      ->get('quote_requested'));
    $default_option = _uc_quote_extract_default_option($order->quote_form);
    if ($default_option) {
      $order->quote['rate'] = $order->quote_form[$default_option]['rate']['#value'];
      $quote_option = explode('---', $default_option);
      $order->quote['method'] = $quote_option[0];
      $order->quote['accessorials'] = $quote_option[1];
      $method = ShippingQuoteMethod::load($quote_option[0]);
      $label = $method
        ->label();
      $result = \Drupal::database()
        ->query("SELECT line_item_id FROM {uc_order_line_items} WHERE order_id = :id AND type = :type", [
        ':id' => $order
          ->id(),
        ':type' => 'shipping',
      ]);
      if ($lid = $result
        ->fetchField()) {
        uc_order_update_line_item($lid, $label, $order->quote['rate']);
      }
      else {
        uc_order_line_item_add($order
          ->id(), 'shipping', $label, $order->quote['rate']);
      }
    }
    else {
      unset($order->quote);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function process(OrderInterface $order, array $form, FormStateInterface $form_state) {
    $this
      ->prepare($order, $form, $form_state);
    if (!isset($order->quote) && \Drupal::config('uc_quote.settings')
      ->get('require_quote')) {
      $form_state
        ->setErrorByName('panes][quotes][quotes][quote_option', $this
        ->t('You must select a shipping option before continuing.'));
      return FALSE;
    }
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function review(OrderInterface $order) {
    $review = [];
    $result = \Drupal::database()
      ->query("SELECT * FROM {uc_order_line_items} WHERE order_id = :id AND type = :type", [
      ':id' => $order
        ->id(),
      ':type' => 'shipping',
    ]);
    if ($line_item = $result
      ->fetchAssoc()) {
      $review[] = [
        'title' => $line_item['title'],
        'data' => [
          '#theme' => 'uc_price',
          '#price' => $line_item['amount'],
        ],
      ];
    }
    return $review;
  }

  /**
   * Pane submission handler to trigger quote calculation.
   */
  public function paneSubmit($form, FormStateInterface $form_state) {
    $form_state
      ->setRebuild();
    $form_state
      ->set('quote_requested', TRUE);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CheckoutPanePluginBase::$status protected property Whether the pane is enabled or not.
CheckoutPanePluginBase::$weight protected property The weight of the checkout pane.
CheckoutPanePluginBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration 3
CheckoutPanePluginBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
CheckoutPanePluginBase::getTitle public function Returns the title of the pane, to be displayed on the checkout form. Overrides CheckoutPanePluginInterface::getTitle
CheckoutPanePluginBase::getWeight public function Returns the weight of the checkout pane. Overrides CheckoutPanePluginInterface::getWeight
CheckoutPanePluginBase::isEnabled public function Returns whether the checkout pane is enabled. Overrides CheckoutPanePluginInterface::isEnabled
CheckoutPanePluginBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
CheckoutPanePluginBase::settingsForm public function Returns the settings form for a checkout pane. Overrides CheckoutPanePluginInterface::settingsForm 3
CheckoutPanePluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 1
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.
QuotePane::paneSubmit public function Pane submission handler to trigger quote calculation.
QuotePane::prepare public function Prepares a pane for display. Overrides CheckoutPanePluginBase::prepare
QuotePane::process public function Processes a checkout pane. Overrides CheckoutPanePluginBase::process
QuotePane::review public function Returns the review contents of a checkout pane. Overrides CheckoutPanePluginInterface::review
QuotePane::view public function Returns the contents of a checkout pane. Overrides CheckoutPanePluginInterface::view
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.