/** * Order pane submit callback: Remove a line item from an order. */ public function removeLineItem($form, FormStateInterface $form_state) { $order =& $form_state->get('order'); $triggering_element = $form_state->getTriggeringElement(); $line_item_id = intval($triggering_element['#return_value']); uc_order_delete_line_item($line_item_id); $order->line_items = $order->getLineItems(); $form_state->setRebuild(); }
/** * Displays the cart checkout page built of checkout panes from enabled modules. */ public function checkout() { $cart_config = $this->config('uc_cart.settings'); $items = $this->cartManager->get()->getContents(); if (count($items) == 0 || !$cart_config->get('checkout_enabled')) { return $this->redirect('uc_cart.cart'); } // Send anonymous users to login page when anonymous checkout is disabled. if ($this->currentUser()->isAnonymous() && !$cart_config->get('checkout_anonymous')) { drupal_set_message($this->t('You must login before you can proceed to checkout.')); if ($this->config('user.settings')->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) { drupal_set_message($this->t('If you do not have an account yet, you should <a href=":url">register now</a>.', [':url' => Url::fromRoute('user.register', [], ['query' => drupal_get_destination()])->toString()])); } return $this->redirect('user.page', [], ['query' => drupal_get_destination()]); } // Load an order from the session, if available. if ($this->session->has('cart_order')) { $order = $this->loadOrder(); if ($order) { // Don't use an existing order if it has changed status or owner, or if // there has been no activity for 10 minutes (to prevent identity theft). if ($order->getStateId() != 'in_checkout' || $this->currentUser()->isAuthenticated() && $this->currentUser()->id() != $order->getOwnerId() || $order->getChangedTime() < REQUEST_TIME - CartInterface::CHECKOUT_TIMEOUT) { if ($order->getStateId() == 'in_checkout' && $order->getChangedTime() < REQUEST_TIME - CartInterface::CHECKOUT_TIMEOUT) { // Mark expired orders as abandoned. $order->setStatusId('abandoned')->save(); } unset($order); } } else { // Ghost session. $this->session->remove('cart_order'); drupal_set_message($this->t('Your session has expired or is no longer valid. Please review your order and try again.')); return $this->redirect('uc_cart.cart'); } } // Determine whether the form is being submitted or built for the first time. if (isset($_POST['form_id']) && $_POST['form_id'] == 'uc_cart_checkout_form') { // If this is a form submission, make sure the cart order is still valid. if (!isset($order)) { drupal_set_message($this->t('Your session has expired or is no longer valid. Please review your order and try again.')); return $this->redirect('uc_cart.cart'); } elseif ($this->session->has('uc_cart_order_rebuild')) { drupal_set_message($this->t('Your shopping cart contents have changed. Please review your order and try again.')); return $this->redirect('uc_cart.cart'); } } else { // Prepare the cart order. $rebuild = FALSE; if (!isset($order)) { // Create a new order if necessary. $order = Order::create(array('uid' => $this->currentUser()->id())); $order->save(); $this->session->set('cart_order', $order->id()); $rebuild = TRUE; } elseif ($this->session->has('uc_cart_order_rebuild')) { // Or, if the cart has changed, then remove old products and line items. $result = \Drupal::entityQuery('uc_order_product')->condition('order_id', $order->id())->execute(); if (!empty($result)) { $storage = $this->entityTypeManager()->getStorage('uc_order_product'); $entities = $storage->loadMultiple(array_keys($result)); $storage->delete($entities); } uc_order_delete_line_item($order->id(), TRUE); $rebuild = TRUE; } if ($rebuild) { // Copy the cart contents to the cart order. $order->products = array(); foreach ($items as $item) { $order->products[] = $item->toOrderProduct(); } $this->session->remove('uc_cart_order_rebuild'); } elseif (!uc_order_product_revive($order->products)) { drupal_set_message($this->t('Some of the products in this order are no longer available.'), 'error'); return $this->redirect('uc_cart.cart'); } } $min = $cart_config->get('minimum_subtotal'); if ($min > 0 && $order->getSubtotal() < $min) { drupal_set_message($this->t('The minimum order subtotal for checkout is @min.', ['@min' => uc_currency_format($min)]), 'error'); return $this->redirect('uc_cart.cart'); } // Trigger the "Customer starts checkout" hook and event. $this->moduleHandler()->invokeAll('uc_cart_checkout_start', array($order)); // rules_invoke_event('uc_cart_checkout_start', $order); return $this->formBuilder()->getForm('Drupal\\uc_cart\\Form\\CheckoutForm', $order); }
/** * {@inheritdoc} */ public static function postDelete(EntityStorageInterface $storage, array $orders) { // Delete data from the appropriate Ubercart order tables. $ids = array_keys($orders); $result = \Drupal::entityQuery('uc_order_product')->condition('order_id', $ids, 'IN')->execute(); if (!empty($result)) { entity_delete_multiple('uc_order_product', array_keys($result)); } db_delete('uc_order_comments')->condition('order_id', $ids, 'IN')->execute(); db_delete('uc_order_admin_comments')->condition('order_id', $ids, 'IN')->execute(); db_delete('uc_order_log')->condition('order_id', $ids, 'IN')->execute(); foreach ($orders as $order_id => $order) { // Delete line items for the order. uc_order_delete_line_item($order_id, TRUE); // Log the action in the database. \Drupal::logger('uc_order')->notice('Order @order_id deleted by user @uid.', ['@order_id' => $order_id, '@uid' => \Drupal::currentUser()->id()]); } }