Variant metafield Issue Product Page

SmallDeveloper
Visitor
1 0 0

I wanted to display a custom input, according to the metafields (true/false). when true is selected, then only that product variant should display the input field.

SmallDeveloper_0-1710939838892.png

 

the code that I tried to use -

{% assign current_variant = product.selected_or_first_available_variant %}
{% assign show_name_inclusion = current_variant.metafields.custom.show_name_inclusion %}
{{ show_name_inclusion }}

{% if show_name_inclusion == true %}
  
  <style>
    .custom.form__label { margin-bottom: 0.6rem; }
    .field.custom { margin-top: 0; }
    .custom .field__input { padding-top: 0.8rem; }
  </style>
  <label class="form__label custom" for="personalization">Personalization</label>
  <div class="field custom">
    <input class="field__input" form="{{ 'product-form-' | append: section.id }}" type="text" id="personalization" name="properties[Personalization]" required maxlength="10"> 
    <script>
      document.addEventListener("DOMContentLoaded", () => {
        document.querySelector("form[novalidate]").removeAttribute("novalidate");
      });
      document.querySelector('#personalization').addEventListener('input', (e) => {
        if (!document.querySelector('label[for="personalization"] span')) {
          const span = document.createElement("span");
          span.style.marginLeft = '10px';
          document.querySelector('label[for="personalization"]').appendChild(span);
        }
        document.querySelector('label[for="personalization"] span').style.display = 'inline';
        document.querySelector('label[for="personalization"] span').textContent = e.target.value.length + ' | ' + 10;
        if (e.target.value.length === 0) {
          document.querySelector('label[for="personalization"] span').style.display = 'none';
        }
      });
    </script>
  </div>
{% endif %}

when I select standard - 

SmallDeveloper_1-1710940065743.png

 when Select Custom - 

SmallDeveloper_2-1710940117825.png

when I refresh/reload on custom variant - 

SmallDeveloper_3-1710940158043.png

 

then I select the Standard - 

SmallDeveloper_4-1710940207142.png

I believe something need to be added in global.js to make this issue solve. I have to reload to see the result but this not how a website should work. 

 

Can anyone help me with this?

Replies 0 (0)