/** * {@inheritdoc} */ public function buildForm(OrderInterface $order, array $form, FormStateInterface $form_state) { $form['quote_button'] = array('#type' => 'submit', '#value' => t('Get shipping quotes'), '#submit' => array(array($this, 'retrieveQuotes')), '#ajax' => array('callback' => array($this, 'replaceOrderQuotes'), 'wrapper' => 'quote', 'effect' => 'slide', 'progress' => array('type' => 'bar', 'message' => t('Receiving quotes...')))); $form['quotes'] = array('#tree' => TRUE, '#prefix' => '<div id="quote">', '#suffix' => '</div>'); 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 (array('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'] = array('#type' => 'submit', '#value' => t('Apply to order'), '#submit' => array(array($this, 'applyQuote')), '#ajax' => array('callback' => array($this, 'updateOrderRates'), 'effect' => 'fade', 'progress' => array('type' => 'throbber', 'message' => t('Applying quotes...')))); } $form_state->set(['uc_ajax', 'uc_quote', 'delivery][delivery_country'], array('quote' => array($this, 'replaceOrderQuotes'))); return $form; }
/** * {@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]; $methods = uc_quote_methods(); $method = $methods[$quote_option[0]]; $label = $method['quote']['accessorials'][$quote_option[1]]; $result = db_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); } }