/** * {@inheritdoc} */ public function build() { $cart = Cart::create(\Drupal::getContainer()); $product_count = count($cart->getContents()); // Display nothing if the block is set to hide on empty and there are no // items in the cart. if (!$this->configuration['hide_empty'] || $product_count) { $items = array(); $item_count = 0; $total = 0; if ($product_count) { foreach ($cart->getContents() as $item) { $display_item = \Drupal::moduleHandler()->invoke($item->data->module, 'uc_cart_display', array($item)); if (count(Element::children($display_item))) { $items[] = array('nid' => $display_item['nid']['#value'], 'qty' => $display_item['qty']['#default_value'], 'title' => $display_item['title']['#markup'], 'price' => $display_item['#total'], 'desc' => isset($display_item['description']['#markup']) ? $display_item['description']['#markup'] : FALSE); $total += $display_item['#total']; $item_count += $display_item['qty']['#default_value']; } } } // Build the cart links. $summary_links['view-cart'] = array('title' => $this->t('View cart'), 'url' => Url::fromRoute('uc_cart.cart'), 'attributes' => array('rel' => ['nofollow'])); // Only add the checkout link if checkout is enabled. if (\Drupal::config('uc_cart.settings')->get('checkout_enabled')) { $summary_links['checkout'] = array('title' => $this->t('Checkout'), 'url' => Url::fromRoute('uc_cart.checkout'), 'attributes' => array('rel' => ['nofollow'])); } $build['block'] = array('#theme' => 'uc_cart_block', '#items' => $items, '#item_count' => $item_count, '#total' => $total, '#summary_links' => $summary_links, '#collapsed' => $this->configuration['collapsed']); // Add the cart block CSS. $build['#attached']['library'][] = 'uc_cart/uc_cart_block.styles'; // If the block is collapsible, add the appropriate JS. if ($this->configuration['collapsible']) { $build['#attached']['library'][] = 'system/drupal.system'; $build['#attached']['library'][] = 'uc_cart/uc_cart_block.scripts'; } return $build; } }
public function setUp() { parent::setUp(); // Create a simple customer user account. $this->cart = Cart::create(\Drupal::getContainer()); // Create a simple customer user account. $this->customer = $this->drupalCreateUser(); // Ensure test mails are logged. \Drupal::configFactory()->getEditable('system.mail')->set('interface.uc_order', 'test_mail_collector')->save(); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { if (!$form_state->getRedirect()) { $data = \Drupal::moduleHandler()->invokeAll('uc_add_to_cart_data', array($form_state->getValues())); $msg = $this->config('uc_cart.settings')->get('add_item_msg'); $cart = Cart::create(\Drupal::getContainer()); $redirect = $cart->addItem($form_state->getValue('nid'), $form_state->getValue('qty'), $data, NULL, $msg); if (isset($redirect)) { $form_state->setRedirectUrl($redirect); } } }
/** * Completes the sale and finishes checkout. */ public function complete() { $session = \Drupal::service('session'); if (!$session->has('cart_order') || empty($_SESSION['uc_checkout'][$session->get('cart_order')]['do_complete'])) { return $this->redirect('uc_cart.cart'); } $order = uc_order_load($session->get('cart_order'), TRUE); if (empty($order)) { // Display messages to customers and the administrator if the order was lost. drupal_set_message($this->t("We're sorry. An error occurred while processing your order that prevents us from completing it at this time. Please contact us and we will resolve the issue as soon as possible."), 'error'); $this->logger('uc_cart')->error('An empty order made it to checkout! Cart order ID: @cart_order', ['@cart_order' => $session->get('cart_order')]); return $this->redirect('uc_cart.cart'); } $cart_config = $this->config('uc_cart.settings'); $build = $this->cart->completeSale($order, $cart_config->get('new_customer_login')); $session->remove('cart_order'); unset($_SESSION['uc_checkout'][$order->id()]); // Add a comment to let sales team know this came in through the site. uc_order_comment_save($order->id(), 0, $this->t('Order created through website.'), 'admin'); return $build; }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $cart = Cart::create(\Drupal::getContainer()); $cart->emptyCart(); $form_state->setRedirect('uc_cart.cart'); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $cart_links_config = $this->config('uc_cart_links.settings'); $actions = explode('-', urldecode($this->actions)); $rebuild_cart = FALSE; $messages = array(); $id = $this->t('(not specified)'); $cart = Cart::create(\Drupal::getContainer()); foreach ($actions as $action) { switch (Unicode::substr($action, 0, 1)) { // Set the ID of the Cart Link. case 'i': case 'I': $id = Unicode::substr($action, 1, 32); break; // Add a product to the cart. // Add a product to the cart. case 'p': case 'P': // Set the default product variables. $p = array('nid' => 0, 'qty' => 1, 'data' => array()); $msg = TRUE; // Parse the product action to adjust the product variables. $parts = explode('_', $action); foreach ($parts as $part) { switch (Unicode::substr($part, 0, 1)) { // Set the product node ID: p23 case 'p': case 'P': $p['nid'] = intval(Unicode::substr($part, 1)); break; // Set the quantity to add to cart: _q2 // Set the quantity to add to cart: _q2 case 'q': case 'Q': $p['qty'] = intval(Unicode::substr($part, 1)); break; // Set an attribute/option for the product: _a3o6 // Set an attribute/option for the product: _a3o6 case 'a': case 'A': $attribute = intval(Unicode::substr($part, 1, stripos($part, 'o') - 1)); $option = (string) Unicode::substr($part, stripos($part, 'o') + 1); if (!isset($p['attributes'][$attribute])) { $p['attributes'][$attribute] = $option; } else { // Multiple options for this attribute implies checkbox // attribute, which we must store as an array if (is_array($p['attributes'][$attribute])) { // Already an array, just append this new option $p['attributes'][$attribute][$option] = $option; } else { // Set but not an array, means we already have at least one // option, so put that into an array with this new option $p['attributes'][$attribute] = array($p['attributes'][$attribute] => $p['attributes'][$attribute], $option => $option); } } break; // Suppress the add to cart message: _s // Suppress the add to cart message: _s case 's': case 'S': $msg = FALSE; break; } } // Add the item to the cart, suppressing the default redirect. if ($p['nid'] > 0 && $p['qty'] > 0) { // If it's a product kit, we need black magic to make everything work // right. In other words, we have to simulate FAPI's form values. $node = node_load($p['nid']); if ($node->status) { if (isset($node->products) && is_array($node->products)) { foreach ($node->products as $nid => $product) { $p['data']['products'][$nid] = array('nid' => $nid, 'qty' => $product->qty); } } $cart->addItem($p['nid'], $p['qty'], $p['data'] + \Drupal::moduleHandler()->invokeAll('uc_add_to_cart_data', array($p)), NULL, $msg, FALSE, FALSE); $rebuild_cart = TRUE; } else { $this->logger('uc_cart_link')->error('Cart Link on %url tried to add an unpublished product to the cart.', array('%url' => $this->getRequest()->server->get('HTTP_REFERER'))); } } break; // Empty the shopping cart. // Empty the shopping cart. case 'e': case 'E': if ($cart_links_config->get('empty')) { $cart->emptyCart(); } break; // Display a pre-configured message. // Display a pre-configured message. case 'm': case 'M': // Load the messages if they haven't been loaded yet. if (empty($messages)) { $data = explode("\n", $cart_links_config->get('messages')); foreach ($data as $message) { list($mkey, $mdata) = explode('|', $message, 2); $messages[trim($mkey)] = trim($mdata); } } // Parse the message key and display it if it exists. $mkey = intval(Unicode::substr($action, 1)); if (!empty($messages[$mkey])) { drupal_set_message($messages[$mkey]); } break; } // Rebuild the cart cache if necessary. if ($rebuild_cart) { $cart->getContents(NULL, 'rebuild'); } } if ($cart_links_config->get('track')) { db_merge('uc_cart_link_clicks')->key(array('cart_link_id' => (string) $id))->fields(array('clicks' => 1, 'last_click' => REQUEST_TIME))->expression('clicks', 'clicks + :i', array(':i' => 1))->execute(); } $_SESSION['uc_cart_last_url'] = $this->getRequest()->server->get('HTTP_REFERER'); $query = $this->getRequest()->query; if ($query->has('destination')) { $options = UrlHelper::parse($query->get('destination')); $path = $options['path']; } else { $path = 'cart'; $options = array(); } $options += array('absolute' => TRUE); // Form redirect is for confirmed links. $form_state->setRedirectUrl(Url::fromUri('base:/' . $path, $options)); }
/** * {@inheritdoc} */ public static function create(ContainerInterface $container) { return new static($container->get('plugin.manager.uc_cart.checkout_pane'), Cart::create($container)); }