From 6aed9f4cf34ab6f60584eca0ef6910d9938ac032 Mon Sep 17 00:00:00 2001 From: Lennard Swaneveld Date: Tue, 13 Dec 2016 13:26:50 +0100 Subject: [PATCH 1/4] Views Add to cart handler --- .../src/Plugin/views/field/AddToCart.php | 179 ++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 modules/order/src/Plugin/views/field/AddToCart.php diff --git a/modules/order/src/Plugin/views/field/AddToCart.php b/modules/order/src/Plugin/views/field/AddToCart.php new file mode 100644 index 0000000000..3c2ff03d5f --- /dev/null +++ b/modules/order/src/Plugin/views/field/AddToCart.php @@ -0,0 +1,179 @@ +cartManager = $cart_manager; + $this->cartProvider = $cart_provider; + $this->storeContext = $store_context; + $this->currentUser = $current_user; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('commerce_cart.cart_manager'), + $container->get('commerce_cart.cart_provider'), + $container->get('commerce_store.store_context'), + $container->get('current_user') + ); + } + + /** + * {@inheritdoc} + */ + public function getValue(ResultRow $row, $field = NULL) { + // Make sure the current user is allowed to use the cart. + if (!$this->currentUser->hasPermission('access cart')) { + return; + } + return ''; + } + + /** + * Form constructor for the views form. + * + * @param array $form + * An associative array containing the structure of the form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + */ + public function viewsForm(&$form, FormStateInterface $form_state) { + // Hijack the original submit button and mold it to our use case. + $add_to_cart_button = $form['actions']['submit']; + $add_to_cart_button['#value'] = $this->t('Add to cart'); + unset($form['actions']['submit']); + + // Make sure the current user is allowed to use the cart. + if (!$this->currentUser->hasPermission('access cart')) { + return; + } + // Make sure we do not accidentally cache this form. + $form['#cache']['max-age'] = 0; + // The view is empty, abort. + if (empty($this->view->result)) { + unset($form['actions']); + return; + } + + $form[$this->options['id']]['#tree'] = TRUE; + foreach ($this->view->result as $row_index => $row) { + $form[$this->options['id']][$row_index][] = [ + '#type' => 'number', + '#title' => $this->t('Quantity'), + '#title_display' => 'invisible', + '#default_value' => round(0), + '#size' => 4, + '#min' => 0, + '#max' => 9999, + '#step' => 1, + ]; + $form[$this->options['id']][$row_index][] = $add_to_cart_button; + } + } + + /** + * Submit handler for the views form. + * + * @param array $form + * An associative array containing the structure of the form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + */ + public function viewsFormSubmit(&$form, FormStateInterface $form_state) { + $quantities = $form_state->getValue($this->options['id']); + foreach ($quantities as $row_index => $quantity) { + if (empty($quantity[0])) { + continue; + } + /** @var \Drupal\commerce_product\Entity\ProductVariationInterface $product_variation */ + $product_variation = $this->getEntity($this->view->result[$row_index]); + + $order_item = $this->cartManager->createOrderItem($product_variation, $quantity[0]); + + $cart = $this->cartProvider->getCart('default', $this->storeContext->getStore()); + if (!$cart) { + $cart = $this->cartProvider->createCart('default', $this->storeContext->getStore()); + } + + $this->cartManager->addOrderItem($cart, $order_item); + } + } + + /** + * {@inheritdoc} + */ + public function query() { + // Do nothing. + } + + /** + * {@inheritdoc} + */ + public function clickSortable() { + return FALSE; + } + +} From 4e64cc2ddab185c22de76460a4fa92c657b114e2 Mon Sep 17 00:00:00 2001 From: Lennard Swaneveld Date: Tue, 13 Dec 2016 13:38:57 +0100 Subject: [PATCH 2/4] Views Add to cart moved to commoerce cart module --- modules/{order => cart}/src/Plugin/views/field/AddToCart.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename modules/{order => cart}/src/Plugin/views/field/AddToCart.php (98%) diff --git a/modules/order/src/Plugin/views/field/AddToCart.php b/modules/cart/src/Plugin/views/field/AddToCart.php similarity index 98% rename from modules/order/src/Plugin/views/field/AddToCart.php rename to modules/cart/src/Plugin/views/field/AddToCart.php index 3c2ff03d5f..4e4bb1aefb 100644 --- a/modules/order/src/Plugin/views/field/AddToCart.php +++ b/modules/cart/src/Plugin/views/field/AddToCart.php @@ -1,6 +1,6 @@ Date: Tue, 13 Dec 2016 13:40:46 +0100 Subject: [PATCH 3/4] Comment tweak --- modules/cart/src/Plugin/views/field/AddToCart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/cart/src/Plugin/views/field/AddToCart.php b/modules/cart/src/Plugin/views/field/AddToCart.php index 4e4bb1aefb..d6cc00cd9c 100644 --- a/modules/cart/src/Plugin/views/field/AddToCart.php +++ b/modules/cart/src/Plugin/views/field/AddToCart.php @@ -13,7 +13,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Defines a form element for editing the order item quantity. + * Defines a form element for adding a produtr variation to a cart. * * @ViewsField("commerce_cart_add_to_cart") */ From 808a3de1b3fd604152834c940a2e948e902e30db Mon Sep 17 00:00:00 2001 From: Lennard Swaneveld Date: Tue, 13 Dec 2016 13:50:18 +0100 Subject: [PATCH 4/4] Comment tweak, retry... --- modules/cart/src/Plugin/views/field/AddToCart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/cart/src/Plugin/views/field/AddToCart.php b/modules/cart/src/Plugin/views/field/AddToCart.php index d6cc00cd9c..b54cab618a 100644 --- a/modules/cart/src/Plugin/views/field/AddToCart.php +++ b/modules/cart/src/Plugin/views/field/AddToCart.php @@ -13,7 +13,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Defines a form element for adding a produtr variation to a cart. + * Defines a form element for adding a product variation to a cart. * * @ViewsField("commerce_cart_add_to_cart") */