Example #1
0
 /**
  * Outputs a cart view for each non-empty cart belonging to the current user.
  *
  * @return array
  *   A render array.
  */
 public function cartPage()
 {
     $build = [];
     $carts = $this->cartProvider->getCarts();
     $carts = array_filter($carts, function ($cart) {
         return $cart->hasLineItems();
     });
     if (!empty($carts)) {
         $cart_views = $this->getCartViews($carts);
         foreach ($carts as $cart_id => $cart) {
             $build[$cart_id] = ['#prefix' => '<div class="cart cart-form">', '#suffix' => '</div>', '#type' => 'view', '#name' => $cart_views[$cart_id], '#arguments' => [$cart_id], '#embed' => TRUE];
         }
     } else {
         $build['empty'] = ['#prefix' => '<div class="cart-empty-page">', '#markup' => $this->t('Your shopping cart is empty.'), '#suffix' => '</div.'];
     }
     return $build;
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     parent::submitForm($form, $form_state);
     $line_item_type_storage = $this->entityManager->getStorage('commerce_line_item_type');
     /** @var \Drupal\commerce_order\Entity\LineItemInterface $line_item */
     $line_item = $this->entity;
     /** @var \Drupal\commerce\PurchasableEntityInterface $purchased_entity */
     $purchased_entity = $line_item->getPurchasedEntity();
     /** @var \Drupal\commerce_order\Entity\LineItemTypeInterface $line_item_type */
     $line_item_type = $line_item_type_storage->load($line_item->bundle());
     $store = $this->selectStore($purchased_entity);
     $cart = $this->cartProvider->getCart($line_item_type->getOrderTypeId(), $store);
     if (!$cart) {
         $cart = $this->cartProvider->createCart('default', $store);
     }
     $this->cartManager->addLineItem($cart, $line_item, $form_state->get(['settings', 'combine']));
     drupal_set_message(t('@entity added to @cart-link.', ['@entity' => $purchased_entity->label(), '@cart-link' => Link::createFromRoute('your cart', 'commerce_cart.page')->toString()]));
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 function submitForm(array &$form, FormStateInterface $form_state)
 {
     $variation = $this->variationStorage->load($form_state->getValue('variation'));
     $available_stores = $variation->getProduct()->getStores();
     if (count($available_stores) === 1) {
         $store = reset($available_stores);
     } else {
         $store = $this->storeContext->getStore();
         if (!in_array($store, $available_stores)) {
             // Throw an exception.
         }
     }
     // @todo The order type should not be hardcoded.
     $cart = $this->cartProvider->getCart('default', $store);
     if (!$cart) {
         $cart = $this->cartProvider->createCart('default', $store);
     }
     $quantity = $form_state->getValue('quantity');
     $combine = $form['#settings']['combine'];
     $this->cartManager->addEntity($cart, $variation, $quantity, $combine);
     drupal_set_message(t('@variation added to @cart-link.', ['@variation' => $variation->label(), '@cart-link' => Link::createFromRoute('your cart', 'commerce_cart.page')->toString()]));
 }
Example #4
0
 /**
  * Builds the cart block.
  *
  * @return array
  *   A render array.
  */
 public function build()
 {
     /** @var \Drupal\commerce_order\Entity\OrderInterface[] $carts */
     $carts = $this->cartProvider->getCarts();
     $carts = array_filter($carts, function ($cart) {
         /** @var \Drupal\commerce_order\Entity\OrderInterface $cart */
         return $cart->hasLineItems();
     });
     $count = 0;
     $cart_views = [];
     if (!empty($carts)) {
         $cart_views = $this->getCartViews($carts);
         foreach ($carts as $cart_id => $cart) {
             foreach ($cart->getLineItems() as $line_item) {
                 $count += (int) $line_item->getQuantity();
             }
         }
     }
     $links = [];
     $links[] = ['#type' => 'link', '#title' => t('Cart'), '#url' => Url::fromRoute('commerce_cart.page')];
     return ['#attached' => ['library' => ['commerce_cart/cart_block']], '#theme' => 'commerce_cart_block', '#icon' => ['#theme' => 'image', '#uri' => drupal_get_path('module', 'commerce') . '/icons/ffffff/cart.png', '#alt' => $this->t('Shopping cart')], '#count' => $count, '#item_text' => $this->configuration['item_text'], '#url' => Url::fromRoute('commerce_cart.page')->toString(), '#content' => $cart_views, '#links' => $links];
 }