1

I have added an order custom field to the checkout process named "Would you like this order gift wrapped?" and there is one choice (check box) which is passing value, which is marking that order as a gift, but now store owner has decided to have added some $ amount to the total when that options is checked.

My code looks like:

          <td>Would you like this order gift wrapped?</td>
            <td colspan="2"><input type="checkbox" value="yes" name="order_custom1" /> Yes</td>
            <td style="text-align:right"><input type="hidden" value="5.00" name="order_custom2" /> $5.00</td>
        </tr>     

Now how I can update total with value of order_custom2 when order_custom1 is not empty on Update Total (or auto update total w/ jQuery on check box checked event)?

Thanks

1 Answer 1

2

You will need to use a product instead of a custom order field.

In Store 2, the checkout tag and add to cart tag both submit to the same URL, so you can use this to add optional products to the cart during checkout.

To do this, the high level overview is that you simply submit a hidden field with the entry_id along with the standard update cart fields.

For example:

<input type="hidden" name="items[0][entry_id]" value="123" />
<!-- hidden input so that unselecting the checkbox submits 0 quantity -->
<input type="hidden" name="items[0][update_qty]" value="0" />
<input type="checkbox" name="items[0][update_qty]" value="1" selected /> Gift wrapping?

update_qty is exactly like item_qty, except that it overwrites any existing quantity in the cart rather than adding to it.

The only difference between this and the regular add to cart form is that you have to wrap the inputs with items[num][]. This allows multiple products to be added/updated at once.

This is similar to how the product form tag works (they all submit to the same endpoint).

7
  • So I have create a product named "Gift Wrapping" with given amount value (price), then during the checkout process I can use example above to add it to the process. Am I right? Also do I need to count items[num] already in the cart to get proper value for that? Not sure about that part. May 8, 2014 at 16:49
  • Correct. You shouldn't need to worry whether the item is already in the cart when using update_qty, because it will overwrite the existing quantity. Just make sure you don't display the product in the regular {items} list (you can exclude it using {if entry_id != "123"}. May 8, 2014 at 16:56
  • The num you use in items[num][] shouldn't matter, as long as it doesn't clash with the {key} used by one of the other items in the checkout. I used 0 above which should work fine. May 8, 2014 at 16:57
  • All looks fine, but how can I subtract value when "Gift Wrapping" was checked off after cart update? May 8, 2014 at 17:16
  • 1
    Never mind - got it to work by adding <input type="hidden" name="items[0][update_qty]" value="0" /> before the checkbox. May 8, 2014 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.