Skip to content

Commit

Permalink
Add twig tags for basic cart operations directly in templates
Browse files Browse the repository at this point in the history
  • Loading branch information
bossanova808 committed Oct 21, 2016
1 parent f9792ae commit b088bad
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 2 deletions.
61 changes: 60 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Provides an alternative controller to assist in adding multiple items to your Craft Commerce cart at once.

Also provides some handy twig tags for adding items, removing a line item, and clearing the cart directly in your templates.

## Adding Multiple Items To Your Cart

Use the following code in your product template to make use of this new controller.

Notes:
Expand Down Expand Up @@ -74,7 +78,7 @@ Alternatively, submit via Ajax & get a JSON response, which (on success) include
});
```

## Update Cart
## Updating Multiple Items In Your Cart

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.

Expand Down Expand Up @@ -115,6 +119,61 @@ craft()->on('multiAdd_cart.onBeforeMultiAddToCart', function($event) {
```

## Twig Tags

MultiAdd also provides a few extra useful twig tags that you can use to perform cart operations directly in your templates.

Example - Setup:

```
{% set cart = craft.commerce.cart %}
{% set items = [] %}
{% set items = items|merge([{"purchasableId":1385,"qty":1, "note":"Test note"}]) %}
{% set items = items|merge([{"purchasableId":854,"qty":2, "options": {"colour":"green"} }]) %}
```

Items is an array, and now holds:

```
array(2) {
[0]=>
array(3) {
["purchasableId"]=>
int(1385)
["qty"]=>
int(1)
["note"]=>
string(9) "Test note"
}
[1]=>
array(3) {
["purchasableId"]=>
int(854)
["qty"]=>
int(2)
["options"]=>
array(1) {
["colour"]=>
string(9) "green"
}
}
}
```

Example Twig Operations:

```
# add items to cart
{{ craft.multiAdd.multiAddToCart(cart, items) }}
# remove the first line item
{{ craft.multiAdd.removeLineItem(cart, cart.lineItems[0].id ) }}
# clear the cart
{{ craft.multiAdd.removeAllLineItems(cart) }}
```

## Compatibility

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.
Expand Down
2 changes: 1 addition & 1 deletion multiadd/MultiAddPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function getName()

public function getVersion()
{
return '0.1.8';
return '0.1.9';
}

public function getSchemaVersion()
Expand Down
41 changes: 41 additions & 0 deletions multiadd/variables/MultiAddVariable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
namespace Craft;

class MultiAddVariable
{

public function multiAddToCart($cart, $items){

$error = "";

//Must be an array of items, each being a purchasable ID and a qty
if(!is_array($items)){
return "Must supply an array of items, each with a purchasableId and a qty (and optionally note, options)";
}
else {
craft()->multiAdd_cart->multiAddToCart($cart, $items, $error);
}

if($error){
return $error;
}
else {
return "Items added to cart";
}

}

public function removeAllLineItems($cart){

craft()->commerce_cart->clearCart($cart);

}

public function removeLineItem($cart, $lineItemId){

craft()->commerce_cart->removeFromCart($cart, $lineItemId);

}


}
8 changes: 8 additions & 0 deletions releases.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
[
{
"version": "0.1.9",
"downloadUrl": "https://github.com/engram-design/MultiAdd/archive/0.1.9.zip",
"date": "2016-10-21T11:17:28+10:00",
"notes": [
"[Added] Twig tags for adding item(s) to cart, removing a line item, and clearing the cart",
]
},
{
"version": "0.1.8",
"downloadUrl": "https://github.com/engram-design/MultiAdd/archive/0.1.8.zip",
Expand Down

0 comments on commit b088bad

Please sign in to comment.