3

Building on top of the Commerce Example Templates. I've made an order field called 'Customer Reference'. I'm submitting data like this:

<form class="shopForm" method="POST">
<input type="hidden" name="action" value="commerce/cart/updateCart">
<div class="field">
    <label for="fields[customerReference]">Your Reference: {{cart.customerReference}}</label>
    <input type="text" name="fields[customerReference]" value="">
</div>
<input class="btn btn-outline" type="submit" value="Update Reference">

This code seems to work ok. But after updating the cart the data doesn't seem to be saved, it doesn't show up in the Commerce back end. I'm also not able to be display the data later on in the ordering process, on the receipt for example.

I'm using this to display the data from the field:

{{cart.customerReference}} or {{order.customerReference}}

Can somebody tell me, possibly with some examples, how to properly submit and retrieve the content for custom order fields?

This is related to: How can I update custom order fields in the frontend

Working with: mamp

  • 2
    Have you added the custom field to your order field layout in Commerce > Settings > Order Fields? Can you see the field in an order edit screen? – Luke Holder May 2 '16 at 17:47
  • Yes, I have and there I am able to save data to the field, no problem. (Maybe relevant: this site only allows guest customers). – oxford May 3 '16 at 6:35
3

The text that is submitted using your <input> field will display as expected with Your Reference: {{ cart.customerReference }}.

However, I have a feeling that because the page is re-rendered with the inputs value as empty, when the /updateCart action is submitted again the blank value will override and erase the old value.

To solve this use the customerReference stored on the cart in the value attribute of your text <input>.

<input ... value="{{ cart.customerReference|length ?? cart.customerReference }}">
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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