Hi there,
I am working with a client’s existing Shopify store and I am trying to add a “Gift” option at Checkout, on the Customer Details page. They are a Shopify Plus store using a checkout.liquid file (which I’m aware is being deprecated) which has heavy customisations. For the sake of getting the Gift feature rolled out quickly, the plan is currently to keep it this way, so I am not looking for a Checkout Extension-based solution.
I’ve successfully added a checkbox that reveals the recipient’s name and email fields, which are named like so:
name="attributes[gift_first_name]"
When a customer changes any of the fields related to gifts, listening to the JavaScript change event, I collect all those fields values into a giftFields object, and update the cart via the API:
Checkout.$.ajax({
type: "POST",
url: '/cart.js',
dataType: 'json',
data: { attributes: giftFields },
success: function(res) { console.log(res); },
error: function(err) { console.error(err); }
});
I can verify the API call is successful as it returns the cart info with the attributes property, containing the fields I provided.
After this, when a user goes to the shipping then payment page, the cart API still returns this gift data in the attributes, as I can verify in my browser’s DevTools. However, test orders performed by the client have been unsuccessful and the Notes section is empty.
Our first attempt did have the info but stored as a sub-property of attributes (attributes.gift.first_name, attributes.gift.last_name, and so on), which got encoded into a single string that could not be parsed by our post-checkout suite of tools. This semi-functional code has been lost and we’ve been trying to get this to work since, and all attempts have failed so far. The only difference is that this first attempt used fetch, versus new attempts are using jQuery’s ajax method, but
The latest test did have an attribute from another script to register the “funnel” the user took, and I feared it was overwriting all of the attributes, but disabling that code had no effect, and did not affect the first test we did.
I am a developer so I am very comfortable with all of this process, but cannot figure out why Shopify is dropping the attributes, which are clearly registered into the cart as the DevTools show when I run the following code:
fetch(window.location.origin + '/cart.js');
// Returns the following (truncated):
{
...
"note": null,
"attributes": {
"is_gift": "true",
"anticipated_gifting_date": "2023-12-22",
"gift_first_name": "John",
"gift_last_name": "Doe",
"gift_email": "jd@example.com"
},
...
}
I would love some help to get this sorted out as it seems like a very simple addition, and I feel I must be missing something that might be common knowledge among Shopify devs.
Thank you in advance for any and all help, and happy holidays!
Chris