1

When I follow the documented process of populating custom fields with an update cart call, I'm able to get values for lightswitches and text fields to populate just fine. I cannot, however, get the submitted fields to carry over to the order/cart when I try to populate them into a Matrix field.

Is this possible with Commerce?

In this case, I'm trying to capture a manifest of individuals (name, email, phone) based on the quantity.

If I try to add:

    <div class="form-group">
      {% tag 'input' with {
          type: 'email',
          name: 'fields[manifest][blocks][new1][fields][firstName]',
          value: "",
          placeholder: 'First Name'|t,
          class: ['form-control']
      } %}{% endtag %}
      <label for="fields[manifest][blocks][new1][fields][firstName]">{{ 'First Name'|t }}</label>
    </div>
    <div class="form-group">
      {% tag 'input' with {
          type: 'email',
          name: 'fields[manifest][blocks][new1][fields][lastName]',
          value: "",
          placeholder: 'First Name'|t,
          class: ['form-control']
      } %}{% endtag %}
      <label for="fields[manifest][blocks][new1][fields][lastName]">{{ 'Last Name'|t }}</label>
    </div>
    <div class="form-group">
      {% tag 'input' with {
          type: 'email',
          name: 'fields[manifest][blocks][new1][fields][email]',
          value: "",
          placeholder: 'Email'|t,
          class: ['form-control']
      } %}{% endtag %}
      <label for="fields[manifest][blocks][new1][fields][email]">{{ 'Email'|t }}</label>
    </div>
    <div class="form-group">
      {% tag 'input' with {
          type: 'email',
          name: 'fields[manifest][blocks][new1][fields][phone]',
          value: "",
          placeholder: 'Email'|t,
          class: ['form-control']
      } %}{% endtag %}
      <label for="fields[manifest][blocks][new1][fields][phone]">{{ 'Phone #'|t }}</label>
    </div>

If I replace new1 with a unique key to identify each one, I would expect it to add each of the values to a matching block for the manifest custom field but it doesn't seem to be capturing the input at all.

Craft CMS: 4.4.6.1
Craft Commerce: 4.2.6

1 Answer 1

2

Figured out the issue. In order to work with a matrix field, you also have to include the block type and the sortOrder in the POST data.

{{ input('hidden', 'fields[manifest][sortOrder][]', '__counter__') }}
{{ input('hidden', 'fields[manifest][blocks][new1][type]', 'manifest') }}
    <div class="form-group">
      {% tag 'input' with {
          type: 'email',
          name: 'fields[manifest][blocks][new1][fields][firstName]',
          value: "",
          placeholder: 'First Name'|t,
          class: ['form-control']
      } %}{% endtag %}
      <label for="fields[manifest][blocks][new1][fields][firstName]">{{ 'First Name'|t }}</label>
    </div>
    <div class="form-group">
      {% tag 'input' with {
          type: 'email',
          name: 'fields[manifest][blocks][new1][fields][lastName]',
          value: "",
          placeholder: 'First Name'|t,
          class: ['form-control']
      } %}{% endtag %}
      <label for="fields[manifest][blocks][new1][fields][lastName]">{{ 'Last Name'|t }}</label>
    </div>
    <div class="form-group">
      {% tag 'input' with {
          type: 'email',
          name: 'fields[manifest][blocks][new1][fields][email]',
          value: "",
          placeholder: 'Email'|t,
          class: ['form-control']
      } %}{% endtag %}
      <label for="fields[manifest][blocks][new1][fields][email]">{{ 'Email'|t }}</label>
    </div>
    <div class="form-group">
      {% tag 'input' with {
          type: 'email',
          name: 'fields[manifest][blocks][new1][fields][phone]',
          value: "",
          placeholder: 'Email'|t,
          class: ['form-control']
      } %}{% endtag %}
      <label for="fields[manifest][blocks][new1][fields][phone]">{{ 'Phone #'|t }}</label>
    </div>

Adding those fields solved the problem.

Your Answer

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

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