name="cart[customer][name]"
name="cart[billing_address][company_name]"
document.querySelectorAll
and forEach
to loop over them. Add the required
attribute to all of them and you're done.3) To ensure that the code is executed in the correct shopping cart view, we can listen for thevar inputs = document.querySelectorAll( 'input[name="cart[customer][name]"], input[name="cart[billing_address][company_name]"]' ); inputs.forEach(function(inputEl) { inputEl.setAttribute('required', ''); });
voog:shoppingcart:show
event and check the event.detail.view
value.
document.addEventListener('voog:shoppingcart:show', function(event) {
if (event.detail.view == 'shipping_form') {
// code goes here
}
});
Here's the final code that adds the required
attribute to the 'name' and 'company' fields, preventing submitting empty values in them.<script>
document.addEventListener('voog:shoppingcart:show', function(event) { if (event.detail.view == 'shipping_form') { var inputs = document.querySelectorAll( 'input[name="cart[customer][name]"], input[name="cart[billing_address][company_name]"]' ); inputs.forEach(function(inputEl) { inputEl.setAttribute('required', ''); }); } });
</script>