Forum

Thread tagged as: Problem, Shop

Perch Shop - "Update" buttons not working as expected in Cart

Hi guys,

I'm looking for a little help with the Shop App - I've got an "Update" button on products (for when a customer wants to update the quantity of products) in the cart, as well as an "Apply" button for discounts.

The issue is that if a customer has two products (product A and B) in their cart, and clicks "Update" on product A, the page reloads (as expected) with the new quantity of product A, but removes product B. When a customer clicks "Apply" for the discount code, the page reloads and wipes both products out of the cart.

Here is how the form renders on the front-end:

<form id="form2_cart" action="/shop/cart" method="post">
    <input value="Update" aria-hidden="true" hidden="true" type="submit"><input type="hidden" name="cms-form" value="Y2FydDpwZXJjaF9zaG9wOi90ZW1wbGF0ZXMvc2hvcC9jYXJ0L2NhcnQuaHRtbDoxNDg3MDczNDkz">
    <div class="cart-wrapper">
      <table class="cart">
        <tbody><tr>
          <th></th>
          <th>Code</th>
          <th>Item</th>
          <th>Quantity</th>
          <th>Price</th>
          <th>Total</th>
        </tr>

          <tr>
            <td><img src="/cms/resources/5905fa418-b5bf-4f12-9ebf-94f1ee3bca892048x2048-w80h80@1.6x.jpg" width="80" height="80"></td>
            <td>FK001DUF</td>
            <td><b>
              'coaler' duffle bag
              </b>
              <p><i>

                </i></p>
              <button type="submit" class="remove_cart" name="del:3
              " value="1">
              Remove from cart
              </button></td>
            <td><input id="form2_qty:3
              " name="qty:3
              " value="
              2
              " min="0" placeholder="2" type="number">
        <input value="Update" aria-hidden="true" type="submit"><input type="hidden" name="cms-form" value="Y2FydDpwZXJjaF9zaG9wOi90ZW1wbGF0ZXMvc2hvcC9jYXJ0L2NhcnQuaHRtbDoxNDg3MDczNDkz"></td>
            <td class="money">160.00</td>
            <td class="money">£320.00</td>
          </tr>

      </tbody></table>
    </div>
    <!-- /div.cart-wrapper -->

    <div class="left-col"> </div>
    <!-- /div.left-col -->

    <div class="right-col">
      <div class="discount-code-wrapper">
        <div class="discount">
          <h3>Have a discount code?</h3>
          <label for="form2_discount"> Discount code
            <input id="form2_discount_code" name="discount_code" type="text">
          </label>
          <input value="Apply" type="submit"><input type="hidden" name="cms-form" value="Y2FydDpwZXJjaF9zaG9wOi90ZW1wbGF0ZXMvc2hvcC9jYXJ0L2NhcnQuaHRtbDoxNDg3MDczNDkz">
        </div>
        <!-- /div.discount --> 

      </div>
      <!-- /div.discount-code-wrapper -->

      <div class="price-wrapper">
        <table>

            <tbody><tr class="total">
              <th colspan="5"> Total </th>
              <td class="money">£320.00</td>
            </tr>

          <tr class="total">
            <th colspan="5"> Tax </th>
            <td class="money">£0.00</td>
          </tr>
          <tr class="total">
            <th colspan="5"> <span class="grand-total">Grand total: </span> </th>
            <td class="money"><span class="grand-total">
              £320.00
              </span></td>
          </tr>
        </tbody></table>

      </div>
      <!-- div.price-wrapper -->

      <div class="cart-btns">
        <p> <a href="checkout" class="shop-btn">Checkout now</a> <a href="/shop/index" class="shop-btn">Continue shopping</a> </p>
      </div>
      <!-- /div.cart-btns --> 

    </div>
</form>
Harry Ray

Harry Ray 0 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Can you show us the template itself? I think you might have some repeated fields which could cause a problem.

Hi Drew,

Sure - no worries. Here's the template, cart.html:

<div class="content-wrapper">
  <perch:if id="product_count" match="gt" value="0">
    <h1>Your cart (<perch:shop id="item_count" />)</h1>
    <perch:form id="cart" app="perch_shop">
    <perch:input type="submit" value="Update" hidden="true" aria-hidden="true" />
    <div class="cart-wrapper">
      <table class="cart">
        <tr>
          <th></th>
          <th>Code</th>
          <th>Item</th>
          <th>Quantity</th>
          <th>Price</th>
          <th>Total</th>
        </tr>
        <perch:cartitems>
          <tr>
            <td><perch:cartitem id="image" type="image" width="80" height="80" density="1.6" crop="true" output="tag" /></td>
            <td><perch:cartitem id="sku" /></td>
            <td><b>
              <perch:cartitem id="title" />
              </b>
              <p><i>
                <perch:cartitem id="variant_desc" />
                </i></p>
              <button type="submit" class="remove_cart" name="del:<perch:cartitem id="identifier" />
              " value="1">
              Remove from cart
              </button></td>
            <td><perch:input id="qty:<perch:cartitem id="identifier" />
              " value="
              <perch:cartitem id="quantity" />
              " type="number" min="0" placeholder="<perch:cartitem id="quantity" />" />
        <perch:input type="submit" value="Update" aria-hidden="true" /></td>
            <td class="money"><perch:cartitem id="price_without_tax" /></td>
            <td class="money"><perch:cartitem id="total_without_tax_formatted" /></td>
          </tr>
        </perch:cartitems>
      </table>
    </div>
    <!-- /div.cart-wrapper -->

    <div class="left-col"> </div>
    <!-- /div.left-col -->

    <div class="right-col">
      <div class="discount-code-wrapper">
        <div class="discount">
          <h3>Have a discount code?</h3>
          <perch:label for="discount"> Discount code
            <perch:input type="text" id="discount_code" />
          </perch:label>
          <perch:input type="submit" value="Apply" />
        </div>
        <!-- /div.discount --> 

      </div>
      <!-- /div.discount-code-wrapper -->

      <div class="price-wrapper">
        <table>
          <perch:if id="total_discounts" match="gt" value="0.00">
            <tr class="total">
              <th colspan="5"> Discount (
                <perch:shop id="discount_code" />
                ) </th>
              <td class="money"><perch:shop id="total_discounts_formatted" /></td>
            </tr>
            <tr class="total">
              <th colspan="5"> Total </th>
              <td class="money"><perch:shop id="total_items_discounted_formatted" /></td>
            </tr>
            <perch:else />
            <tr class="total">
              <th colspan="5"> Total </th>
              <td class="money"><perch:shop id="total_items_formatted" /></td>
            </tr>
          </perch:if>
          <tr class="total">
            <th colspan="5"> Tax </th>
            <td class="money"><perch:shop id="total_tax_formatted" /></td>
          </tr>
          <tr class="total">
            <th colspan="5"> <span class="grand-total">Grand total: </span> </th>
            <td class="money"><span class="grand-total">
              <perch:shop id="grand_total_formatted" />
              </span></td>
          </tr>
        </table>
        </perch:form>
      </div>
      <!-- div.price-wrapper -->

      <div class="cart-btns">
        <p> <a href="checkout" class="shop-btn">Checkout now</a> <a href="/shop/index" class="shop-btn">Continue shopping</a> </p>
      </div>
      <!-- /div.cart-btns --> 

    </div>
    <!-- div.right-col -->

    <perch:else />
    <h1>Your cart</h1>
    <p>Your cart is empty.</p>
  </perch:if>
</div>
<!-- /div.content-wrapper --> 

Hi Drew,

Did you find anything in here? I've slightly altered the template so that now each cart item has its own ID, and that's gone some way to fixing the issue.

The new issue is that the quantity is no longer updateable by a customer in the cart.

Any ideas?

Thanks, Harry

Drew McLellan

Drew McLellan 2638 points
Perch Support

You have this for each cart item:

<perch:input type="submit" value="Update" aria-hidden="true" />

What's the purpose of the hidden button in this case?

Hi Drew!

Sure, that button is the 'Update' button to update the quantity per item - it's not actually hidden.

Thanks, Harry

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok. Does it work without those duplicate buttons?

What's the point where it stops working vs the default template?

I've replaced this template with the default one, and that seems to work just fine at this stage.

Is it not possible to have one "Update" button per cart item, then? I've tried adding unique names and ids to each Update button (using the product SKU and "_update"), but that results in the same "refreshing without updating" behaviour that I've already noted.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, that should be possible, just watch the HTML output you're getting. It's just a form at the end of the day.

That's true - I suppose I was just forgetting the basics!

Thanks for the help here Drew, I've got it working now. There is another, bigger issue with the checkout process, but I'll create a new thread later today regarding that.