How to use cart attributes in your hook

You can access cart attributes in your order-related hooks.
As an example, we'll show you how to retrieve a cart attribute called "Website URL".

Here is how the "Website URL" attribute looks on the cart page:

This is the hook code that logs the value of the cart attribute:

function findCartAttribute(payload, attributeName) {
  let result = null;

  if (payload.note_attributes && payload.note_attributes.length > 0) {
    payload.note_attributes.every( attrib => {
      if (attrib.name === attributeName) {
        result = attrib.value;
        return  false;
      }
      return true;
    });
  }

	return result;
}

module.exports = async function(payload, actions) {
	const ATTRIB_NAME = 'Website URL';

	const value = findCartAttribute(payload, ATTRIB_NAME);

	if (value) {
		console.log(`Cart attribute '${ATTRIB_NAME}' is '${value}'.`)
	} else {
		console.error(`Cart attribute '${ATTRIB_NAME}' is not in the payload.`)
	}
};

For more examples, you can visit our Gist page on Github.