|
1 | | -# MultiAdd |
| 1 | +> |
| 2 | +> ### Multi Add has a new home with the fine folks at Verbb. Read about it in our [blog post](https://verbb.io/blog/welcome-to-verbb). |
| 3 | +> |
2 | 4 |
|
3 | | -Provides an alternative controller to assist in adding multiple items to your Craft Commerce cart at once. |
4 | | - |
5 | | -Also provides some handy twig tags for adding items, removing a line item, and clearing the cart directly in your templates. |
6 | | - |
7 | | -## Adding Multiple Items To Your Cart |
8 | | - |
9 | | -Use the following code in your product template to make use of this new controller. |
10 | | - |
11 | | -Notes: |
12 | | - |
13 | | -* The array *must* be named `items` and you must supply at least a `purchasableId` and a `qty`. |
14 | | -* Attributes are grouped together by a key in the form `items[key][attribute]` where key can be any value but must be the same for each of the attributes. `loop.index` is an obvious choice for this if iterative over products or variations in a loop. |
15 | | -* You can also POST an optional `note` per line item |
16 | | -* You can also POST arbitrary options, e.g. `[options][color]` |
17 | | -* Items with a zero `qty` are simply skipped and not added to the cart. |
18 | | - |
19 | | -Here's some example code to get you started - if you get stuck, just ask on the Craft CMS Slack #craftcommerce channel for help. |
20 | | - |
21 | | -This code displays all your products in one form: |
22 | | - |
23 | | -``` |
24 | | -<form method="POST" id="addToCart"> |
25 | | - <input type="hidden" name="action" value="multiAdd/multiAdd"> |
26 | | - <input type="hidden" name="redirect" value="/cart"> |
27 | | - {{ getCsrfInput() }} |
28 | | -
|
29 | | - {% for product in craft.commerce.products.find() %} |
30 | | - <input type="hidden" name="items[{{ loop.index }}][purchasableId]" value="{{ product.defaultVariant.id }}"> |
31 | | - <input type="hidden" name="items[{{ loop.index }}][qty]" value="1"> |
32 | | - <input type="text" name="items[{{ loop.index }}][note]"> |
33 | | - |
34 | | - <select name="items[{{ loop.index }}][options][color]"> |
35 | | - <option value="blue">Blue</option> |
36 | | - <option value="white">White</option> |
37 | | - <option value="red">Red</option> |
38 | | - </select> |
39 | | - {% endfor %} |
40 | | -</form> |
41 | | -``` |
42 | | - |
43 | | -Or say you want to list the variants of a particular product out: |
44 | | - |
45 | | -``` |
46 | | -<form method="POST" id="addToCart"> |
47 | | - <input type="hidden" name="action" value="multiAdd/multiAdd"> |
48 | | - <input type="hidden" name="redirect" value="/cart"> |
49 | | - {{ getCsrfInput() }} |
50 | | -
|
51 | | - {% set product = craft.commerce.products.slug('your-product') %} |
52 | | -
|
53 | | - {% for variant in product.variants %} |
54 | | - <input type="hidden" name="items[{{ loop.index }}][purchasableId]" value="{{ variant.id }}"> |
55 | | - <input type="hidden" name="items[{{ loop.index }}][qty]" value="1"> |
56 | | - <input type="text" name="items[{{ loop.index }}][note]"> |
57 | | - {% endfor %} |
58 | | -</form> |
59 | | -``` |
60 | | - |
61 | | -Alternatively, submit via Ajax & get a JSON response, which (on success) includes a comprehensive cart object with all the data you should need. |
62 | | - |
63 | | -``` |
64 | | - $("#addToCart").submit(function(e) { |
65 | | - e.preventDefault(); |
66 | | -
|
67 | | - var data = $(this).serialize(); |
68 | | - data[window.csrfTokenName] = window.csrfTokenValue; |
69 | | -
|
70 | | - $.post('/actions/' + $('input[name=action]').val(), data, function(response) { |
71 | | - if (response.success) { |
72 | | - $("#addToCartButton").val("Added!"); |
73 | | - cart.update( response.cart ); |
74 | | - } else { |
75 | | - $("#addToCartButton").val("Error!"); |
76 | | - } |
77 | | - }); |
78 | | -}); |
79 | | -``` |
80 | | - |
81 | | -## Updating Multiple Items In Your Cart |
82 | | - |
83 | | -When viewing your cart, it's currently not possible to update all your line items at once - instead it must be done for each line item as a separate event. This controller let's you update multiple line items at once. This might be desirable when a user has multiple line items in their cart, and wants to update quantities all at once by clicking an 'Update Cart' button. |
| 5 | +# Multi Add Plugin for Craft CMS |
84 | 6 |
|
85 | | -To achieve this, create your cart template using the following guide: |
| 7 | +<img width="500" src="https://verbb.io/uploads/plugins/multi-add/_800x455_crop_center-center/multi-add-social-card.png"> |
86 | 8 |
|
87 | | -``` |
88 | | -<form method="POST"> |
89 | | - <input type="hidden" name="action" value="multiAdd/updateCart"> |
90 | | - <input type="hidden" name="redirect" value="/cart"> |
91 | | - {{ getCsrfInput() }} |
92 | | -
|
93 | | - {% for item in cart.lineItems %} |
94 | | - <input type="text" size="4" name="items[{{ item.id }}][qty]" value="{{ item.qty }}"> |
95 | | - {% endfor %} |
96 | | -
|
97 | | - <button type="submit">Update Cart</button> |
98 | | -</form> |
99 | | -``` |
100 | | - |
101 | | - |
102 | | -## Events |
103 | | - |
104 | | -This plugin raises two events, much like normal the Commerce add to cart, which you can listen for in the same way. They are: |
105 | | - |
106 | | -`onBeforeMultiAddToCart` and `onMultiAddToCart` |
107 | | - |
108 | | -In each case the event parameters are: |
109 | | - |
110 | | -`order` (Commerce_OrderModel) |
111 | | -`lineItems` (an array of Commerce_LineItemModel) |
112 | | - |
113 | | -``` |
114 | | -craft()->on('multiAdd_cart.onBeforeMultiAddToCart', function($event) { |
115 | | - $order = $event->params['order']; |
116 | | - $lineItems = $event->params['lineItems']; |
117 | | - $event->performAction = false; |
118 | | -}); |
119 | | -
|
120 | | -``` |
121 | | - |
122 | | -## Twig Tags |
123 | | - |
124 | | -MultiAdd also provides a few extra useful twig tags that you can use to perform cart operations directly in your templates. |
125 | | - |
126 | | -Example - Setup: |
127 | | - |
128 | | -``` |
129 | | -{% set cart = craft.commerce.cart %} |
130 | | -{% set items = [] %} |
131 | | -
|
132 | | -{% set items = items|merge([{"purchasableId":1385,"qty":1, "note":"Test note"}]) %} |
133 | | -{% set items = items|merge([{"purchasableId":854,"qty":2, "options": {"colour":"green"} }]) %} |
134 | | -``` |
135 | | - |
136 | | -Items is an array, and now holds: |
137 | | - |
138 | | -``` |
139 | | -array(2) { |
140 | | - [0]=> |
141 | | - array(3) { |
142 | | - ["purchasableId"]=> |
143 | | - int(1385) |
144 | | - ["qty"]=> |
145 | | - int(1) |
146 | | - ["note"]=> |
147 | | - string(9) "Test note" |
148 | | - } |
149 | | - [1]=> |
150 | | - array(3) { |
151 | | - ["purchasableId"]=> |
152 | | - int(854) |
153 | | - ["qty"]=> |
154 | | - int(2) |
155 | | - ["options"]=> |
156 | | - array(1) { |
157 | | - ["colour"]=> |
158 | | - string(9) "green" |
159 | | - } |
160 | | - } |
161 | | -} |
162 | | -``` |
163 | | - |
164 | | -Example Twig Operations: |
| 9 | +Provides an alternative controller to assist in adding multiple items to your Craft Commerce cart at once. |
165 | 10 |
|
166 | | -``` |
167 | | -# add items to cart |
168 | | -{{ craft.multiAdd.multiAddToCart(cart, items) }} |
| 11 | +Also provides some handy twig tags for adding items, removing a line item, and clearing the cart directly in your templates. |
169 | 12 |
|
170 | | -# remove the first line item |
171 | | -{{ craft.multiAdd.removeLineItem(cart, cart.lineItems[0].id ) }} |
| 13 | +## Documentation |
172 | 14 |
|
173 | | -# clear the cart |
174 | | -{{ craft.multiAdd.removeAllLineItems(cart) }} |
175 | | -``` |
| 15 | +Visit the [Multi Add Plugin page](https://verbb.io/craft-plugins/multi-add) for all documentation, guides, pricing and developer resources. |
176 | 16 |
|
177 | | -## Compatibility |
| 17 | +## Support |
178 | 18 |
|
179 | | -This plugin has been tested with Craft 2.5 and Craft Commerce 1.0.1187 and above. It's in use daily on production systems. |
| 19 | +Get in touch with us via the [Multi Add Support page](https://verbb.io/craft-plugins/multi-add/support) or by [creating a Github issue](/verbb/multi-add/issues) |
180 | 20 |
|
181 | | -## Changelog |
| 21 | +<h2></h2> |
182 | 22 |
|
183 | | -See [releases.json](https://raw.githubusercontent.com/engram-design/MultiAdd/master/releases.json) |
| 23 | +<a href="https://verbb.io" target="_blank"> |
| 24 | + <img width="100" src="https://verbb.io/assets/img/verbb-pill.svg"> |
| 25 | +</a> |
184 | 26 |
|
185 | | -## Credits |
186 | 27 |
|
187 | | -By [Josh Crawford](https://github.com/engram-design) (of S. Group) (@crawf on Craft CMS Slack) and [Jeremy Daalder](https://github.com/bossanova808) (@jeremydaalder), with thanks to [Luke Holder](https://github.com/lukeholder) (@lukeholder). |
0 commit comments