Skip to content

Instantly share code, notes, and snippets.

@AugustMiller
Created November 2, 2020 19:11
Show Gist options
  • Save AugustMiller/1742fb0b87e8fcead3fcb5aaee51e80b to your computer and use it in GitHub Desktop.
Save AugustMiller/1742fb0b87e8fcead3fcb5aaee51e80b to your computer and use it in GitHub Desktop.
Increment or decrement quantity of product in cart
{% set incrementable = incrementable ?? true %}
{% set decrementable = decrementable ?? true %}
<div class="line-item__controls">
{% if incrementable or decrementable %}
<div class="line-item__quantity">
<form
method="POST"
data-ajax>
{{ csrfInput() }}
{{ actionInput('commerce/cart/update-cart') }}
{{ hiddenInput("lineItems[#{lineItem.id}][qty]", lineItem.qty - 1) }}
<button
class="line-item__quantity-button line-item__quantity-button--decrement"
type="submit"
title="Remove One"
{% if not decrementable %}disabled{% endif %}>&minus;</button>
</form>
<div class="line-item__quantity-readout">{{ lineItem.qty }}</div>
<form
method="POST"
data-ajax>
{{ csrfInput() }}
{{ actionInput('commerce/cart/update-cart') }}
{{ hiddenInput("lineItems[#{lineItem.id}][qty]", lineItem.qty + 1) }}
<button
class="line-item__quantity-button line-item__quantity-button--increment"
type="submit"
title="Add One"
{% if not incrementable %}disabled{% endif %}>&plus;</button>
</form>
</div>
{% endif %}
<div class="line-item__remove">
<form
method="POST"
data-ajax>
{{ csrfInput() }}
{{ actionInput('commerce/cart/update-cart') }}
{{ hiddenInput("lineItems[#{lineItem.id}][remove]", 'Yes, please!') }}
<button
class="line-item__remove-button"
type="submit">Remove</button>
</form>
</div>
</div>
{# Info about your line item... #}
{# Then, include the partial (optionally passing `incrementable` or `decrementable` booleans, depending on whether it's possible to modify quantity) #}
{% include '__blocks/line-item__controls' with {
lineItem: lineItem
} only %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment