Jump to content

Set default variant?

Go to solution Solved by SpencerC,

Recommended Posts

  • 10 months later...
  • 3 months later...
  • SpencerC locked this topic
  • SpencerC unlocked this topic
  • 2 years later...

You don't need to pay for an app: use this code

<script>

function setValue() 
  
{
     var dropdown = document.querySelectorAll('[data-variant-option-name="Type"]')[0];
    dropdown.value = 'Summer Flap';
    Y.one(dropdown).simulate('change');
  
  var dropdown2 = document.querySelectorAll('[data-variant-option-name="Colour"]')[0];
    dropdown2.value = 'Orange $89';
    Y.one(dropdown).simulate('change');
  
}
  
  window.onload = setValue;

</script>

Link to comment
  • 1 month later...
<script>
function setValue() {
document.querySelectorAll('div.variant-option select').forEach(function(dropdown){
  dropdown.selectedIndex = '1';
  Y.one(dropdown).simulate('change');
});
}
 window.onload = setValue ;
</script>

The above code can put in `Page Header Code Injection` and it will select the first variant option and displays its price as well

I modified @DomBrady answer to work with any product variant without having to specify variant details in code

Edited by AshAb
Link to comment
  • 5 months later...
On 8/8/2023 at 6:28 AM, AshAb said:
<script>
function setValue() {
document.querySelectorAll('div.variant-option select').forEach(function(dropdown){
  dropdown.selectedIndex = '1';
  Y.one(dropdown).simulate('change');
});
}
 window.onload = setValue ;
</script>

The above code can put in `Page Header Code Injection` and it will select the first variant option and displays its price as well

I modified @DomBrady answer to work with any product variant without having to specify variant details in code

Thanks for this!! 🙌🏼

Link to comment
  • 5 months later...
On 8/8/2023 at 7:28 AM, AshAb said:
<script>
function setValue() {
document.querySelectorAll('div.variant-option select').forEach(function(dropdown){
  dropdown.selectedIndex = '1';
  Y.one(dropdown).simulate('change');
});
}
 window.onload = setValue ;
</script>

The above code can put in `Page Header Code Injection` and it will select the first variant option and displays its price as well

I modified @DomBrady answer to work with any product variant without having to specify variant details in code

Hey Ash, this one doesn't seem to be working for me. Would you be able to take a look for me?

product example: https://clementine-salamander-j4rd.squarespace.com/producten/p/eiken-tafel-oluf

password: JELSAN2024

Link to comment
  • 4 weeks later...
13 minutes ago, austinalltheway21 said:

What if I'm not using the variant dropdown but rather squarespace's variant "button" [how can I set the default variant]

You should be able to select the first variant button using JavaScript. Try this:

<!-- Select first variant when using variant buttons -------------------->
<!-- Copyright Soundfocus Digital [https://sf.digital] ------------------>
<!-- Use freely in your code injection. Do NOT re-publish. -------------->
<script>
document.addEventListener('DOMContentLoaded', function() {
  var sfpv = document.querySelector('.product-variants');
  if (sfpv) {
    var sfvo = sfpv.querySelectorAll('.variant-option');
    sfvo.forEach(function(option) {
      // Find the first element with the class variant-radiobtn-wrapper within the current variant-option
      var sfrbw = option.querySelector('.variant-radiobtn-wrapper .sqs-button-element--secondary');
      if (sfrbw) {
        sfrbw.click();
      }
    });
  }
});
</script>

Did this help? Please give feedback by clicking an icon below  ⬇️

Me: I'm Paul, a SQSP user for >18 yrs & Circle Leader since 2017. I value honesty, transparency, diversity and good design ♥.
Work: Founder of SF.DIGITAL. We provide high quality original extensions to supercharge your Squarespace website. 
Content: Views and opinions are my own. Links in my posts may refer to my own SF.DIGITAL products or may be affiliate links.
Forum advice is completely free. You can thank me by selecting a feedback emoji. Buying a coffee is generous but optional.

Would you like your customers to be able to mark their favourite products in your Squarespace store?

Link to comment
48 minutes ago, paul2009 said:

You should be able to select the first variant button using JavaScript. Try this:

<!-- Select first variant when using variant buttons -------------------->
<!-- Copyright Soundfocus Digital [https://sf.digital] ------------------>
<!-- Use freely in your code injection. Do NOT re-publish. -------------->
<script>
document.addEventListener('DOMContentLoaded', function() {
  var sfpv = document.querySelector('.product-variants');
  if (sfpv) {
    var sfvo = sfpv.querySelectorAll('.variant-option');
    sfvo.forEach(function(option) {
      // Find the first element with the class variant-radiobtn-wrapper within the current variant-option
      var sfrbw = option.querySelector('.variant-radiobtn-wrapper .sqs-button-element--secondary');
      if (sfrbw) {
        sfrbw.click();
      }
    });
  }
});
</script>

Did this help? Please give feedback by clicking an icon below  ⬇️

Thank you so much! That worked!

Link to comment
48 minutes ago, austinalltheway21 said:

Thank you so much! That worked!

Great! It's looking good.

Me: I'm Paul, a SQSP user for >18 yrs & Circle Leader since 2017. I value honesty, transparency, diversity and good design ♥.
Work: Founder of SF.DIGITAL. We provide high quality original extensions to supercharge your Squarespace website. 
Content: Views and opinions are my own. Links in my posts may refer to my own SF.DIGITAL products or may be affiliate links.
Forum advice is completely free. You can thank me by selecting a feedback emoji. Buying a coffee is generous but optional.

Would you like your customers to be able to mark their favourite products in your Squarespace store?

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.