View source
<?php
namespace Drupal\uc_quote\Plugin\Ubercart\CheckoutPane;
use Drupal\Core\Form\FormStateInterface;
use Drupal\uc_cart\CheckoutPanePluginBase;
use Drupal\uc_order\OrderInterface;
use Drupal\uc_quote\Entity\ShippingQuoteMethod;
class QuotePane extends CheckoutPanePluginBase {
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...'),
],
],
'#limit_validation_errors' => [],
];
$contents['quotes'] = [
'#type' => 'container',
'#attributes' => [
'id' => 'quote',
],
'#tree' => TRUE,
'#weight' => 1,
];
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;
}
public function prepare(OrderInterface $order, array $form, FormStateInterface $form_state) {
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 (empty($order->data->uc_quote_selected)) {
unset($order->quote);
}
$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);
}
}
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;
}
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;
}
public function paneSubmit($form, FormStateInterface $form_state) {
$form_state
->setRebuild();
$form_state
->set('quote_requested', TRUE);
}
}