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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\uc_cart\CheckoutPanePluginBase implements CheckoutPanePluginInterface
- class \Drupal\uc_quote\Plugin\Ubercart\CheckoutPane\QuotePane
- class \Drupal\uc_cart\CheckoutPanePluginBase implements CheckoutPanePluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of QuotePane
File
- shipping/
uc_quote/ src/ Plugin/ Ubercart/ CheckoutPane/ QuotePane.php, line 20
Namespace
Drupal\uc_quote\Plugin\Ubercart\CheckoutPaneView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CheckoutPanePluginBase:: |
protected | property | Whether the pane is enabled or not. | |
CheckoutPanePluginBase:: |
protected | property | The weight of the checkout pane. | |
CheckoutPanePluginBase:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurableInterface:: |
3 |
CheckoutPanePluginBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
CheckoutPanePluginBase:: |
public | function |
Returns the title of the pane, to be displayed on the checkout form. Overrides CheckoutPanePluginInterface:: |
|
CheckoutPanePluginBase:: |
public | function |
Returns the weight of the checkout pane. Overrides CheckoutPanePluginInterface:: |
|
CheckoutPanePluginBase:: |
public | function |
Returns whether the checkout pane is enabled. Overrides CheckoutPanePluginInterface:: |
|
CheckoutPanePluginBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
CheckoutPanePluginBase:: |
public | function |
Returns the settings form for a checkout pane. Overrides CheckoutPanePluginInterface:: |
3 |
CheckoutPanePluginBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
1 |
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 | |
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. | |
QuotePane:: |
public | function | Pane submission handler to trigger quote calculation. | |
QuotePane:: |
public | function |
Prepares a pane for display. Overrides CheckoutPanePluginBase:: |
|
QuotePane:: |
public | function |
Processes a checkout pane. Overrides CheckoutPanePluginBase:: |
|
QuotePane:: |
public | function |
Returns the review contents of a checkout pane. Overrides CheckoutPanePluginInterface:: |
|
QuotePane:: |
public | function |
Returns the contents of a checkout pane. Overrides CheckoutPanePluginInterface:: |
|
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. |