Sana Assistant (online)
Table of Contents

UseProductLineField hook

const productLineField = useProductLineField(fieldName);

When content element add-on is located on the shopping cart page it may need to access and update custom field values for shopping cart product lines.

The useProductLineField hook provides info and API for changing shopping cart line field values for content elements. This hook returns an object with data and API for changing the field value or null if it's not being used on suitable pages, the field with the specified name is not available, or the field line is non-editable.

Parameters

Parameter Type Description
fieldName string The name of the product line field.

Return Value

The hook returns an object with the following properties or null:

Property Type Description
value unknown The current value of the field.
forceValidation boolean A flag indicating whether validation should be forced for a field. This may be useful in scenarios when user did not interact with the form control, but validation should be triggered.
onFieldChange (value: unknown, isValid: boolean) => void Function which is invoked to update the field value. value is the new value of the field. isValid is the validity status of the field.
invalidFieldClassName string The invalid field class name to be used on field form control element in case it failed validation. This class name will be used among other class names to determine first invalid form control element on shopping cart page to be scrolled into view.

Example:

const productLineField = useProductLineField('StringField');

if (!productLineField)
  return null;

const {
  value,
  forceValidation,
  onFieldChange,
  invalidFieldClassName,
} = productLineField;

const handleFieldChange = (newValue: string, isValid: boolean) => {
  onFieldChange(newValue, isValid);
};