Skip to content

Commit 1cea25a

Browse files
gitbojanz
authored andcommitted
Issue #2900917 by swickham, bojanz: Add an optional "Empty cart" button
1 parent e118258 commit 1cea25a

4 files changed

Lines changed: 718 additions & 0 deletions

File tree

modules/cart/commerce_cart.module

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,13 @@ function commerce_cart_views_data_alter(array &$data) {
240240
'help' => t('Adds a button for removing the order item.'),
241241
'id' => 'commerce_order_item_remove_button',
242242
];
243+
$data['commerce_order']['empty_cart_button'] = [
244+
'title' => t('Empty cart button'),
245+
'help' => t('Adds a button for emptying the cart.'),
246+
'area' => [
247+
'id' => 'commerce_order_empty_cart_button',
248+
],
249+
];
243250
}
244251

245252
/**
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
namespace Drupal\commerce_cart\Plugin\views\area;
4+
5+
use Drupal\commerce_cart\CartManagerInterface;
6+
use Drupal\Core\Form\FormStateInterface;
7+
use Drupal\Core\Entity\EntityTypeManagerInterface;
8+
use Drupal\views\Plugin\views\area\AreaPluginBase;
9+
use Symfony\Component\DependencyInjection\ContainerInterface;
10+
11+
/**
12+
* Defines an area handler for the "Empty cart" button.
13+
*
14+
* @ViewsArea("commerce_order_empty_cart_button")
15+
*/
16+
class EmptyCartButton extends AreaPluginBase {
17+
18+
/**
19+
* The cart manager.
20+
*
21+
* @var \Drupal\commerce_cart\CartManagerInterface
22+
*/
23+
protected $cartManager;
24+
25+
/**
26+
* The order storage.
27+
*
28+
* @var \Drupal\Core\Entity\EntityStorageInterface
29+
*/
30+
protected $orderStorage;
31+
32+
/**
33+
* Constructs a new EmptyCartButton object.
34+
*
35+
* @param array $configuration
36+
* A configuration array containing information about the plugin instance.
37+
* @param string $plugin_id
38+
* The plugin ID for the plugin instance.
39+
* @param mixed $plugin_definition
40+
* The plugin implementation definition.
41+
* @param \Drupal\commerce_cart\CartManagerInterface $cart_manager
42+
* The cart manager.
43+
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
44+
* The entity type manager.
45+
*/
46+
public function __construct(array $configuration, $plugin_id, $plugin_definition, CartManagerInterface $cart_manager, EntityTypeManagerInterface $entity_type_manager) {
47+
parent::__construct($configuration, $plugin_id, $plugin_definition);
48+
49+
$this->cartManager = $cart_manager;
50+
$this->orderStorage = $entity_type_manager->getStorage('commerce_order');
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
57+
return new static(
58+
$configuration,
59+
$plugin_id,
60+
$plugin_definition,
61+
$container->get('commerce_cart.cart_manager'),
62+
$container->get('entity_type.manager')
63+
);
64+
}
65+
66+
/**
67+
* {@inheritdoc}
68+
*/
69+
public function render($empty = FALSE) {
70+
return [];
71+
}
72+
73+
/**
74+
* Gets whether the views form should be shown when the view has no results.
75+
*
76+
* @param bool $empty
77+
* Whether the view has results.
78+
*
79+
* @return bool
80+
* True if the views form should be shown, FALSE otherwise.
81+
*/
82+
public function viewsFormEmpty($empty) {
83+
return $empty;
84+
}
85+
86+
/**
87+
* Builds the views form.
88+
*
89+
* @param array $form
90+
* An associative array containing the structure of the form.
91+
* @param \Drupal\Core\Form\FormStateInterface $form_state
92+
* The current state of the form.
93+
*/
94+
public function viewsForm(array &$form, FormStateInterface $form_state) {
95+
// Make sure we do not accidentally cache this form.
96+
$form['#cache']['max-age'] = 0;
97+
98+
$form[$this->options['id']]['#tree'] = TRUE;
99+
$form['actions']['empty_cart'] = [
100+
'#type' => 'submit',
101+
'#value' => t('Empty cart'),
102+
'#empty_cart_button' => TRUE,
103+
'#attributes' => [
104+
'class' => ['empty-cart-button'],
105+
],
106+
];
107+
}
108+
109+
/**
110+
* Submits the views form.
111+
*
112+
* @param array $form
113+
* An associative array containing the structure of the form.
114+
* @param \Drupal\Core\Form\FormStateInterface $form_state
115+
* The current state of the form.
116+
*/
117+
public function viewsFormSubmit(array &$form, FormStateInterface $form_state) {
118+
$triggering_element = $form_state->getTriggeringElement();
119+
if (!empty($triggering_element['#empty_cart_button'])) {
120+
/** @var \Drupal\commerce_order\Entity\OrderInterface $cart */
121+
$cart = $this->orderStorage->load($this->view->argument['order_id']->getValue());
122+
$this->cartManager->emptyCart($cart);
123+
124+
drupal_set_message($this->t('Your shopping cart has been emptied.'));
125+
}
126+
}
127+
128+
}

0 commit comments

Comments
 (0)