I want to 2 radio button on product view page when customer click on ‘Add To Cart’ button need to check does any radio button checked or not? If not then dont allow add to cart. How can I do this? Thanks in advance
2 Answers
you have to follow simple dom structure below,
<div class="options-list">
<div class="field choice">
<input type="radio" class="radio" id="bundle-option-3-5" data-validate="{'validate-one-required-by-name':true}" name="bundle_option[]" value="5">
<label class="label" for="bundle-option-3-5">
Test1
</label>
</div>
<div class="field choice">
<input type="radio" class="radio" id="bundle-option-3-6" data-validate="{'validate-one-required-by-name':true}" name="bundle_option[]" value="5">
<label class="label" for="bundle-option-3-6">
Test2
</label>
</div>
<div class="field choice">
<input type="radio" class="radio" id="bundle-option-3-7" data-validate="{'validate-one-required-by-name':true}" name="bundle_option[]" value="5">
<label class="label" for="bundle-option-3-7">
Test3
</label>
</div>
</div>
data-validate="{'validate-one-required-by-name':true}"
its used for validation.
-
Thanks for reply, This works and does not allow me to add product in cart without selecting radio but it does not show validation message like 'Please select one option'. How can I display this message? Commented May 26, 2016 at 4:12
As another reference, the following worked for me in M2.3.5-p1, including validation if no value is selected:
$customerGroups = $this->getCustomerGroups();
if ($customerGroups && count($customerGroups)) { ?>
<div class="field customer-group">
<label for="customer-group" class="label"><span><?php /* @escapeNotVerified */ echo __('Customer group') ?></span></label>
<div class="control">
<?php foreach ($customerGroups as $data) {
$customerGroupId = $data['value'];
$customerGroupLabel = $data['label'];
if ($customerGroupId) {
echo sprintf(' <input type="radio" class="required-entry" name="customer-group" id="customer-group-%s" value="%s" aria-required="true"/> <label for="customer-group-%s">%s</label>', $customerGroupId, $customerGroupId, $customerGroupId, $customerGroupLabel);
}
} ?>
</div>
</div>
<?php }