Skip to content

Commit

Permalink
Fixed #357
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeholder committed May 28, 2018
1 parent 23d25c9 commit af3361f
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 149 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
## 2.0.0-beta.4 - 2018-05-09

### Added
- Added support for updating multiple line items at in one update cart request. ([#357](https://github.com/craftcms/commerce/issues/357))
- Added the ability to place a note on a refund transaction.
- Added `craft\commerce\services\TaxCategories::getAllTaxCategoriesAsList()`.

Expand Down
38 changes: 37 additions & 1 deletion src/controllers/CartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,43 @@ public function actionUpdateCart()
$this->_cart->addLineItem($lineItem);
}
}
};
}

// update multiple line items in the cart
if ($lineItems = $request->getParam('lineItems')) {
foreach ($lineItems as $key => $lineItem) {
$lineItemId = $key;
$note = $request->getParam("lineItems.{$key}.note");
$options = $request->getParam("lineItems.{$key}.options");
$qty = $request->getParam("lineItems.{$key}.qty");
$removeLine = $request->getParam("lineItems.{$key}.remove");

$lineItem = Plugin::getInstance()->getLineItems()->getLineItemById($lineItemId);

// Line item not found, or does not belong to their order
if (!$lineItem || ($this->_cart->id != $lineItem->orderId)) {
throw new NotFoundHttpException('Line item not found');
}

if ($qty) {
$lineItem->qty = $qty;
}

if ($note) {
$lineItem->note = $note;
}

if ($options) {
$lineItem->setOptions($options);
}

if ($removeLine) {
$this->_cart->removeLineItem($lineItem);
} else {
$this->_cart->addLineItem($lineItem);
}
}
}

$this->_setAddresses();

Expand Down
275 changes: 129 additions & 146 deletions templates/shop/cart.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,180 +13,163 @@
<th class="text-right">Price</th>
</tr>
</thead>
<form method="POST">
<input type="hidden" name="action"
value="commerce/cart/update-cart">
{{ redirectInput('shop/cart') }}
{{ csrfInput() }}
<tbody>
{% set lineItemHasErrors = false %}
{% for item in cart.lineItems %}
{% if item.hasErrors() %}
{# if the line item has errors lets record it so we can hide totals later (since they wont make sense) #}
{% set lineItemHasErrors = true %}
{% endif %}
<tr {% if item.hasErrors() %}style="background-color:rgba(255, 0, 0, .1);"{% endif %}>
<td>
<strong>{{ item.description }}</strong><br>
({{ item.sku }})
<br>
<code>{{ item.options|json_encode }}</code>
<form method="POST">
<input type="hidden" name="action"
value="commerce/cart/remove-line-item"/>
{{ redirectInput('shop/cart') }}
{{ csrfInput() }}
<input type="hidden" name="lineItemId"
value="{{ item.id }}"/>
<input type="submit" class="button link"
value="Remove"/>
</form>
</td>
<td>
<form method="POST">
<input type="hidden" name="action"
value="commerce/cart/update-line-item">
{{ redirectInput('shop/cart') }}
<input type="hidden" name="lineItemId"
value="{{ item.id }}">
{% set lineItemHasErrors = false %}
{% for item in cart.lineItems %}
{% if item.hasErrors() %}
{# if the line item has errors lets record it so we can hide totals later (since they wont make sense) #}
{% set lineItemHasErrors = true %}
{% endif %}
<tr {% if item.hasErrors() %}style="background-color:rgba(255, 0, 0, .1);"{% endif %}>
<td>
<strong>{{ item.description }}</strong><br>
({{ item.sku }})
<br>
<code>{{ item.options|json_encode }}</code>
<br>
<input type="checkbox"
name="lineItems[{{ item.id }}][remove]"
value="1"> Remove Line<br>
</td>
<td>
{% if item.options.giftWrapped is defined %}
<select name="lineItems[{{ item.id }}][options][giftWrapped]">
<option value="no"
{% if item.options.giftWrapped == 'no' %}selected{% endif %}>
No gift wrap
</option>
<option value="yes"
{% if item.options.giftWrapped == 'yes' %}selected{% endif %}>
Gift wrapped.
</option>
</select>
{% endif %}

<input type="text" placeholder="My Note"
size="20" name="note"
size="20"
name="lineItems[{{ item.id }}][note]"
value="{{ item.note }}">

<span {% if item.getFirstError('qty') %}class="has-error"{% endif %}>
<input type="number" name="qty" min="1"
value="{{ item.qty }}">
<input type="number"
name="lineItems[{{ item.id }}][qty]"
min="1" value="{{ item.qty }}">
</span>
{{ csrfInput() }}
{% if item.options.giftWrapped is defined %}
<select name="options[giftWrapped]">
<option value="no"
{% if item.options.giftWrapped == 'no' %}selected{% endif %}>
No gift wrap.
</option>
<option value="yes"
{% if item.options.giftWrapped == 'yes' %}selected{% endif %}>
Gift wrapped.
</option>
</select>
{% endif %}
<input type="submit" class="button"
value="Update"/>
</form>
</td>
<td class="text-right">
{% if not lineItemHasErrors %}
{% if item.onSale %}
<s>Price: {{ item.price|commerceCurrency(cart.currency) }}</s>
<br>
Sale Off: {{ item.saleAmount|commerceCurrency(cart.currency) }}
<br>
Sale Price {{ item.salePrice|commerceCurrency(cart.currency) }}
<br>
Sale Price Subtotal: {{ item.subtotal|commerceCurrency(cart.currency) }}
<br>
{% else %}
Price: {{ item.price|commerceCurrency(cart.currency) }}
<br>
Sale Price Subtotal: {{ item.subtotal|commerceCurrency(cart.currency) }}
<br>
{% endif %}
{% endif %}

{% for sale in item.purchasable.sales %}
{% if loop.first %}
Sales Applied:
</td>

<td class="text-right">
{% if not lineItemHasErrors %}
{% if item.onSale %}
<s>Price: {{ item.price|commerceCurrency(cart.currency) }}</s>
<br>
Sale Off: {{ item.saleAmount|commerceCurrency(cart.currency) }}
<br>
Sale Price {{ item.salePrice|commerceCurrency(cart.currency) }}
<br>
Sale Price Subtotal: {{ item.subtotal|commerceCurrency(cart.currency) }}
<br>
{% else %}
Price: {{ item.price|commerceCurrency(cart.currency) }}
<br>
Price Subtotal: {{ item.subtotal|commerceCurrency(cart.currency) }}
<br>
{% endif %}
{% endif %}
{{ sale.name }}<br>

{% for sale in item.purchasable.sales %}
{% if loop.first %}
Sales Applied:
{% endif %}
{{ sale.name }}<br>
{% endfor %}
</td>
</tr>
{% if not lineItemHasErrors %}
{% for adjustment in item.adjustments %}
<tr>
<td></td>
<td>
<strong>{{ adjustment.type }} {{ adjustment.name }}</strong><br>({{ adjustment.description }}
)
</td>
<td class="text-right">{{ adjustment.amount|commerceCurrency(cart.currency) }}</td>
</tr>
{% endfor %}
</td>
</tr>
{% endif %}

{% endfor %}

{% if not lineItemHasErrors %}
{% for adjustment in item.adjustments %}
{% for adjustment in cart.orderAdjustments %}
<tr>
<td></td>
<td>{{ adjustment.type }}
</td>
<td>
<strong>{{ adjustment.type }} {{ adjustment.name }}</strong><br>({{ adjustment.description }}
<strong>{{ adjustment.name }}</strong><br>({{ adjustment.description }}
)
</td>
<td class="text-right">{{ adjustment.amount|commerceCurrency(cart.currency) }}</td>
</tr>
{% endfor %}
{% endif %}
<tr>
<td>

{% endfor %}

{% if not lineItemHasErrors %}
{% for adjustment in cart.orderAdjustments %}
<tr>
<td>{{ adjustment.type }}
</td>
<td>
<strong>{{ adjustment.name }}</strong><br>({{ adjustment.description }}
)
</td>
<td class="text-right">{{ adjustment.amount|commerceCurrency(cart.currency) }}</td>
</tr>
{% endfor %}
{% endif %}
</td>
<td colspan="2" class="text-right">
{% if not lineItemHasErrors %}
Item Sub Total: {{ cart.itemSubTotal|commerceCurrency(cart.currency) }}
<br>

<br>
<strong>Built in Adjustment Totals</strong>
<br>
Total Discount: {{ cart.adjustmentsTotalByType('discount')|commerceCurrency(cart.currency) }}
<br>
Total Shipping: {{ cart.adjustmentsTotalByType('shipping')|commerceCurrency(cart.currency) }}
<br>
Total Tax: {{ cart.adjustmentsTotalByType('tax')|commerceCurrency(cart.currency) }}
<br>
Total Tax (inc): {{ cart.adjustmentsTotalByType('tax', true)|commerceCurrency(cart.currency) }}
<br>

<h4>Total
Price: {{ cart.totalPrice|commerceCurrency(cart.currency) }}</h4>
{% endif %}
</td>
</tr>
<tr>
<td colspan="2"> {# Update Coupon form uses the single update controller action: #}
{% if cart.getFirstError('couponCode') %}
<span class="flash">{{ cart.getFirstError('couponCode') }}</span>
{% endif %}

<tr>
<td>
{# Remove all line items to empty the cart: #}
<form method="POST">
<input type="hidden" name="action"
value="commerce/cart/remove-all-line-items"/>
{{ redirectInput('shop/cart') }}
{{ csrfInput() }}
<input class="button link" type="submit"
value="Empty the Cart"/>
</form>

{# Update Coupon form uses the single update controller action: #}
{% if cart.getFirstError('couponCode') %}<span
class="flash">{{ cart.getFirstError('couponCode') }}</span>{% endif %}
<form method="POST">
<input type="hidden" name="action"
value="commerce/cart/update-cart">
{{ redirectInput('shop/cart') }}
{{ csrfInput() }}
<span class="{% if cart.getFirstError('couponCode') %}has-error{% endif %}">
<input type="text" name="couponCode" width="11"
class="{% if cart.getFirstError('couponCode') %}has-error{% endif %}"
value="{{ cart.couponCode }}"
placeholder="{{ "Coupon Code"|t }}">
</span>
<input type="submit" class="button"
value="{% if cart.couponCode %}Change{% else %}Apply{% endif %} Coupon"/>
</form>

</td>
<td colspan="2" class="text-right">
{% if not lineItemHasErrors %}
Item Sub Total: {{ cart.itemSubTotal|commerceCurrency(cart.currency) }}
<br>

<br>
<strong>Built in Adjustment Totals</strong>
<br>
Total Discount: {{ cart.adjustmentsTotalByType('discount')|commerceCurrency(cart.currency) }}
<br>
Total Shipping: {{ cart.adjustmentsTotalByType('shipping')|commerceCurrency(cart.currency) }}
<br>
Total Tax: {{ cart.adjustmentsTotalByType('tax')|commerceCurrency(cart.currency) }}
<br>
Total Tax (inc): {{ cart.adjustmentsTotalByType('tax', true)|commerceCurrency(cart.currency) }}
<br>

<h4>Total
Price: {{ cart.totalPrice|commerceCurrency(cart.currency) }}</h4>
{% endif %}
</td>
</tr>

</span></td>
<td><input class="right" type="submit" value="Update Cart"/></td>
</tr>
<tr>
<td colspan="3">
{% if not lineItemHasErrors %}
<a class="button button-primary right"
href="{{ url('shop/checkout') }}">Checkout</a>
{% endif %}
</td>
</tr>
</tbody>
</form>
</table>

{% if not lineItemHasErrors %}
<a class="button button-primary"
href="{{ url('shop/checkout') }}">Checkout</a>
{% endif %}

{% endif %}

{% if not cart.lineItems|length %}
Expand Down
Loading

0 comments on commit af3361f

Please sign in to comment.