Skip to content

Commit b9ef8d0

Browse files
vasikebojanz
authored andcommitted
Issue #2868637 by vasike: AddToCartFormatter not working in Views
1 parent c15564f commit b9ef8d0

File tree

4 files changed

+186
-8
lines changed

4 files changed

+186
-8
lines changed

modules/cart/tests/modules/commerce_cart_test/config/install/views.view.test_multiple_cart_forms.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ dependencies:
44
module:
55
- commerce_product
66
id: test_multiple_cart_forms
7-
label: 'Test Multiple Cart Forms'
7+
label: 'Test Multiple Cart Forms (Rendered products)'
88
module: views
99
description: ''
1010
tag: ''
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
langcode: en
2+
status: true
3+
dependencies:
4+
module:
5+
- commerce_product
6+
id: test_multiple_cart_forms_fields
7+
label: 'Test Multiple Cart Forms (Fields)'
8+
module: views
9+
description: ''
10+
tag: ''
11+
base_table: commerce_product_field_data
12+
base_field: product_id
13+
core: 8.x
14+
display:
15+
default:
16+
display_plugin: default
17+
id: default
18+
display_title: Master
19+
position: 0
20+
display_options:
21+
access:
22+
type: none
23+
options: { }
24+
cache:
25+
type: tag
26+
options: { }
27+
query:
28+
type: views_query
29+
options:
30+
disable_sql_rewrite: false
31+
distinct: false
32+
replica: false
33+
query_comment: ''
34+
query_tags: { }
35+
exposed_form:
36+
type: basic
37+
options:
38+
submit_button: Apply
39+
reset_button: false
40+
reset_button_label: Reset
41+
exposed_sorts_label: 'Sort by'
42+
expose_sort_order: true
43+
sort_asc_label: Asc
44+
sort_desc_label: Desc
45+
pager:
46+
type: mini
47+
options:
48+
items_per_page: 10
49+
offset: 0
50+
id: 0
51+
total_pages: null
52+
expose:
53+
items_per_page: false
54+
items_per_page_label: 'Items per page'
55+
items_per_page_options: '5, 10, 25, 50'
56+
items_per_page_options_all: false
57+
items_per_page_options_all_label: '- All -'
58+
offset: false
59+
offset_label: Offset
60+
tags:
61+
previous: ‹‹
62+
next: ››
63+
style:
64+
type: default
65+
row:
66+
type: fields
67+
fields:
68+
variations:
69+
id: variations
70+
table: commerce_product__variations
71+
field: variations
72+
relationship: none
73+
group_type: group
74+
admin_label: ''
75+
label: ''
76+
exclude: false
77+
alter:
78+
alter_text: false
79+
text: ''
80+
make_link: false
81+
path: ''
82+
absolute: false
83+
external: false
84+
replace_spaces: false
85+
path_case: none
86+
trim_whitespace: false
87+
alt: ''
88+
rel: ''
89+
link_class: ''
90+
prefix: ''
91+
suffix: ''
92+
target: ''
93+
nl2br: false
94+
max_length: 0
95+
word_boundary: true
96+
ellipsis: true
97+
more_link: false
98+
more_link_text: ''
99+
more_link_path: ''
100+
strip_tags: false
101+
trim: false
102+
preserve_tags: ''
103+
html: false
104+
element_type: ''
105+
element_class: ''
106+
element_label_type: ''
107+
element_label_class: ''
108+
element_label_colon: false
109+
element_wrapper_type: ''
110+
element_wrapper_class: ''
111+
element_default_classes: true
112+
empty: ''
113+
hide_empty: false
114+
empty_zero: false
115+
hide_alter_empty: true
116+
click_sort_column: target_id
117+
type: commerce_add_to_cart
118+
settings:
119+
combine: false
120+
group_column: target_id
121+
group_columns: { }
122+
group_rows: true
123+
delta_limit: 0
124+
delta_offset: 0
125+
delta_reversed: false
126+
delta_first_last: false
127+
multi_type: separator
128+
separator: ', '
129+
field_api_classes: false
130+
plugin_id: field
131+
filters: { }
132+
sorts: { }
133+
title: 'Test Multiple Cart Forms'
134+
header: { }
135+
footer: { }
136+
empty: { }
137+
relationships: { }
138+
arguments: { }
139+
display_extenders: { }
140+
cache_metadata:
141+
max-age: -1
142+
contexts:
143+
- 'languages:language_content'
144+
- 'languages:language_interface'
145+
- url.query_args
146+
tags: { }
147+
page_1:
148+
display_plugin: page
149+
id: page_1
150+
display_title: Page
151+
position: 1
152+
display_options:
153+
display_extenders: { }
154+
path: test-multiple-cart-forms-fields
155+
cache_metadata:
156+
max-age: -1
157+
contexts:
158+
- 'languages:language_content'
159+
- 'languages:language_interface'
160+
- url.query_args
161+
tags: { }

modules/cart/tests/src/Functional/MultipleCartFormsTest.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ protected function setUp() {
5555
* Tests that a page with multiple add to cart forms works properly.
5656
*/
5757
public function testMultipleCartsOnPage() {
58+
// View of rendered products, each containing an add to cart form.
5859
$this->drupalGet('/test-multiple-cart-forms');
59-
6060
/** @var \Behat\Mink\Element\NodeElement[] $forms */
6161
$forms = $this->getSession()->getPage()->findAll('css', '.commerce-order-item-add-to-cart-form');
6262
$this->assertEquals(5, count($forms));
@@ -65,6 +65,20 @@ public function testMultipleCartsOnPage() {
6565
$this->cart = Order::load($this->cart->id());
6666
$order_items = $this->cart->getItems();
6767
$this->assertEquals(new Price('6', 'USD'), $order_items[0]->getTotalPrice());
68+
69+
// View of fields, one of which is the variations field
70+
// rendered via the "commerce_add_to_cart" formatter.
71+
$this->drupalGet('/test-multiple-cart-forms-fields');
72+
/** @var \Behat\Mink\Element\NodeElement[] $forms */
73+
$forms = $this->getSession()->getPage()->findAll('css', '.commerce-order-item-add-to-cart-form');
74+
$this->assertEquals(5, count($forms));
75+
$this->submitForm([], 'Add to cart', $forms[3]->getAttribute('id'));
76+
77+
\Drupal::entityTypeManager()->getStorage('commerce_order')->resetCache();
78+
$this->cart = Order::load($this->cart->id());
79+
$order_items = $this->cart->getItems();
80+
$this->assertEquals(2, count($order_items));
81+
$this->assertEquals(new Price('9', 'USD'), $order_items[1]->getTotalPrice());
6882
}
6983

7084
}

modules/product/src/Plugin/Field/FieldFormatter/AddToCartFormatter.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,18 @@ public function settingsSummary() {
6363
* {@inheritdoc}
6464
*/
6565
public function viewElements(FieldItemListInterface $items, $langcode) {
66-
return [
67-
'#lazy_builder' => ['commerce_product.lazy_builders:addToCartForm', [
68-
$items->getEntity()->id(),
69-
$this->viewMode,
70-
$this->getSetting('combine'),
71-
],
66+
$elements = [];
67+
$elements[0]['add_to_cart_form'] = [
68+
'#lazy_builder' => [
69+
'commerce_product.lazy_builders:addToCartForm', [
70+
$items->getEntity()->id(),
71+
$this->viewMode,
72+
$this->getSetting('combine'),
73+
],
7274
],
7375
'#create_placeholder' => TRUE,
7476
];
77+
return $elements;
7578
}
7679

7780
/**

0 commit comments

Comments
 (0)