You are here

public function Quotes::applyQuote in Ubercart 8.4

Ajax callback: Manually applies a shipping quote to an order.

File

shipping/uc_quote/src/Plugin/Ubercart/OrderPane/Quotes.php, line 112

Class

Quotes
Get a shipping quote for the order from a quoting module.

Namespace

Drupal\uc_quote\Plugin\Ubercart\OrderPane

Code

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);
    }
  }
}