class Quotes in Ubercart 8.4
Get a shipping quote for the order from a quoting module.
Plugin annotation
@UbercartOrderPane(
id = "quotes",
title = @Translation("Shipping quote"),
weight = 7,
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\uc_order\OrderPanePluginBase implements OrderPanePluginInterface
- class \Drupal\uc_order\EditableOrderPanePluginBase implements EditableOrderPanePluginInterface
- class \Drupal\uc_quote\Plugin\Ubercart\OrderPane\Quotes
- class \Drupal\uc_order\EditableOrderPanePluginBase implements EditableOrderPanePluginInterface
- class \Drupal\uc_order\OrderPanePluginBase implements OrderPanePluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of Quotes
File
- shipping/
uc_quote/ src/ Plugin/ Ubercart/ OrderPane/ Quotes.php, line 21
Namespace
Drupal\uc_quote\Plugin\Ubercart\OrderPaneView source
class Quotes extends EditableOrderPanePluginBase {
/**
* {@inheritdoc}
*/
public function getClasses() {
return [
'pos-left',
];
}
/**
* {@inheritdoc}
*/
public function view(OrderInterface $order, $view_mode) {
}
/**
* {@inheritdoc}
*/
public function buildForm(OrderInterface $order, array $form, FormStateInterface $form_state) {
$form['quote_button'] = [
'#type' => 'submit',
'#value' => $this
->t('Get shipping quotes'),
'#submit' => [
[
$this,
'retrieveQuotes',
],
],
'#ajax' => [
'callback' => [
$this,
'replaceOrderQuotes',
],
'wrapper' => 'quote',
'effect' => 'slide',
'progress' => [
'type' => 'bar',
'message' => $this
->t('Receiving quotes...'),
],
],
];
$form['quotes'] = [
'#type' => 'container',
'#attributes' => [
'id' => 'quote',
],
'#tree' => TRUE,
];
if ($form_state
->get('quote_requested')) {
// Rebuild form products, from uc_order_edit_form_submit()
foreach ($form_state
->getValue('products') as $product) {
if (!isset($product['remove']) && intval($product['qty']) > 0) {
foreach ([
'qty',
'title',
'model',
'weight',
'weight_units',
'cost',
'price',
] as $field) {
$order->products[$product['order_product_id']]->{$field} = $product[$field];
}
}
}
$form['quotes'] += uc_quote_build_quote_form($order);
$form['quotes']['add_quote'] = [
'#type' => 'submit',
'#value' => $this
->t('Apply to order'),
'#submit' => [
[
$this,
'applyQuote',
],
],
'#ajax' => [
'callback' => [
$this,
'updateOrderRates',
],
'effect' => 'fade',
'progress' => [
'type' => 'throbber',
'message' => $this
->t('Applying quotes...'),
],
],
];
}
$form_state
->set([
'uc_ajax',
'uc_quote',
'delivery][delivery_country',
], [
'quote' => [
$this,
'replaceOrderQuotes',
],
]);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(OrderInterface $order, array &$form, FormStateInterface $form_state) {
}
/**
* Form submission handler to retrieve quotes.
*/
public function retrieveQuotes($form, FormStateInterface $form_state) {
$element = $form_state
->getTriggeringElement();
$form_state
->set('quote_requested', $element['#value'] == $form['quotes']['quote_button']['#value']);
$form_state
->setRebuild();
}
/**
* Ajax callback: Manually applies a shipping quote to an order.
*/
public function applyQuote($form, FormStateInterface $form_state) {
if ($form_state
->hasValue([
'quotes',
'quote_option',
])) {
if ($order = $form_state
->get('order')) {
$quote_option = explode('---', $form_state
->getValue([
'quotes',
'quote_option',
]));
$order->quote['method'] = $quote_option[0];
$order->quote['accessorials'] = $quote_option[1];
$method = ShippingQuoteMethod::load($quote_option[0]);
$label = $method
->label();
$quote_option = $form_state
->getValue([
'quotes',
'quote_option',
]);
$order->quote['rate'] = $form_state
->getValue([
'quotes',
$quote_option,
'rate',
]);
$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']);
$form_state
->set('uc_quote', [
'lid' => $lid,
'title' => $label,
'amount' => $order->quote['rate'],
]);
}
else {
uc_order_line_item_add($order
->id(), 'shipping', $label, $order->quote['rate']);
}
// Save selected shipping
uc_quote_uc_order_update($order);
// Update line items.
$order->line_items = $order
->getLineItems();
// @todo Still needed?
$form_state
->set('order', $order);
$form_state
->setRebuild();
$form_state
->set('quote_requested', FALSE);
}
}
}
/**
* Ajax callback to update the quotes on the order edit form.
*/
public function replaceOrderQuotes($form, FormStateInterface $form_state) {
return $form['quotes']['quotes'];
}
/**
* Ajax callback for applying shipping rates.
*/
public function updateOrderRates($form, FormStateInterface $form_state) {
$response = new AjaxResponse();
// Update shipping line item.
if ($form_state
->has('uc_quote')) {
$lid = $form_state
->get([
'uc_quote',
'lid',
]);
$form['line_items'][$lid]['title']['#value'] = $form_state
->get([
'uc_quote',
'title',
]);
$form['line_items'][$lid]['amount']['#value'] = $form_state
->get([
'uc_quote',
'amount',
]);
}
$response
->addCommand(new ReplaceCommand('#order-line-items', trim(drupal_render($form['line_items']))));
// Reset shipping form.
$response
->addCommand(new ReplaceCommand('#quote', trim(drupal_render($form['quotes']['quotes']))));
$status_messages = [
'#type' => 'status_messages',
];
$response
->addCommand(new PrependCommand('#quote', drupal_render($status_messages)));
return $response;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
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. | |
OrderPanePluginBase:: |
public | function | ||
OrderPanePluginBase:: |
public | function |
Returns the title of an order pane. Overrides OrderPanePluginInterface:: |
2 |
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. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
Quotes:: |
public | function | Ajax callback: Manually applies a shipping quote to an order. | |
Quotes:: |
public | function |
Form constructor. Overrides EditableOrderPanePluginInterface:: |
|
Quotes:: |
public | function |
Returns the classes used to wrap an order pane. Overrides OrderPanePluginBase:: |
|
Quotes:: |
public | function | Ajax callback to update the quotes on the order edit form. | |
Quotes:: |
public | function | Form submission handler to retrieve quotes. | |
Quotes:: |
public | function |
Form submission handler. Overrides EditableOrderPanePluginInterface:: |
|
Quotes:: |
public | function | Ajax callback for applying shipping rates. | |
Quotes:: |
public | function |
Returns the contents of an order pane as a store administrator. Overrides OrderPanePluginInterface:: |
|
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. |