Jump to content

Set default variant?

Go to solution Solved by SpencerC,

Recommended Posts

  • 10 months later...
  • 3 months later...
  • Solution
Posted

Hi everyone,

Thanks for the valuable input! For any feedback that you would like to be logged, contacting our Customer Support team would be the best approach. As per section 6 of the forum guidelines we don't track feature requests in this forum.

Thanks!

cg1q_g5kO_xGfCuu4Be7izVV_NRhqDc4ve0tGQPjM9KcJEsGk7_r2aOC3TvFDREPcEBeqkVyVYkbcd2f5SqEBwjMR67QYD2Kew-OhW65fVNWvTGdP1cmXHWUSbycirsSKTcnHL_G
SPENCER CHRISTIE
CUSTOMER OPERATIONS ADVISOR
HE/HIM

  • SpencerC locked this topic
  • SpencerC unlocked this topic
  • 2 years later...
Posted

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>

  • 1 month later...
Posted (edited)
<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
  • 5 months later...
Posted
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!! 🙌🏼

  • 5 months later...
Posted
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

  • 4 weeks later...
Posted
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.

Posted
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!

Posted
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.

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.