Add Notification to Magento 2.3 Customer Registration About Shipping Restriction to California
-
13-04-2021 - |
Question
I want to add a notification that appears on the registration page - only when California is selected as a state. Otherwise it will not display. I am unsure of the parameters. I am using Magento 2.3
The current code on the registration.phtml is the following:
<div class="field region required">
<label for="region_id" class="label"><span><?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('region') ?></span></label>
<div class="control">
<select id="region_id" name="region_id" title="<?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('region') ?>" class="validate-select" style="display:none;">
<option value=""><?= $block->escapeHtml(__('Please select a region, state or province.')) ?></option>
</select>
<input type="text" id="region" name="region" value="<?= $block->escapeHtml($block->getRegion()) ?>" title="<?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('region') ?>" class="input-text <?= $block->escapeHtmlAttr($this->helper('Magento\Customer\Helper\Address')->getAttributeValidationClass('region')) ?>" style="display:none;">
</div>
</div>
and my PHP is this, which isn't working:
<?php if ($region_id == '12') {echo '<div class="no-cali">Please note: We do not ship to California.</div>';} else { echo '';} ?>
Solution
To display dynamic content like that You should use JS. I mean, add for example hidden div like this
<div class="no-cali hidden">Please note: We do not ship to California.</div>
then using js / jq, add observer to this select region_id
on change value.
Then check if value is specific one and change visibility. In that case remove or add hidden
class.
Here You can check how to do that https://stackoverflow.com/questions/5416767/get-selected-value-text-from-select-on-change/47495878
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange