How can I hide sold out variants in Dawn theme 11.0?

How can I hide sold out variants in Dawn theme 11.0?

Guifonte
Tourist
25 0 2

Hi Everyone,

 

Does anybody know how to build a code to hide the sold out variants on dawn theme?

 

I need something like this image:

Screenshot 2023-12-14 at 1.54.18 pm.png

Thanks in advance,

Gui.

Replies 12 (12)

Dan-From-Ryviu
Shopify Partner
10768 2128 2249

Could you drop your product page link to check? 

- Solved it? Hit Like and Accept solution! ❤️Buy Me Coffee❤️
- Reton: Loyalty & Rewards - Earn points through tasks, redeem for discounts, and enjoy exclusive VIP rewards!
- Ryviu - Reviews & QA app: Collect product reviews, import reviews from AliExpress, Amazon, Etsy, Walmart, Shopee, Dhgate and CSV.
- Lookfy Gallery: Lookbook Image: Easy and fast to create Photo Gallery, Lookbook, Shop The Look.
- Reelfy‑Shoppable Videos+Reels: Create shoppable videos to engage customers and drive more sales.
- Enjoy 1 month of Shopify for $1. Sign up now.

EasifyApps
Shopify Partner
635 19 52

Hi @Guifonte,

mk.png

b.png

To hide unavailable variants, you'll have to modify your theme code. Navigate to your Product template, and insert the necessary custom code into the Custom liquid section:

 

<script language="javascript" type="text/javascript">
const pickerType = (document.querySelector('variant-radios')) ? 'radios' : 'selects';
const variantSelects = (pickerType == 'radios') ? document.querySelector('variant-radios') : document.querySelector('variant-selects');
const fieldsets = (pickerType == 'radios') ? Array.from(variantSelects.querySelectorAll('fieldset')) : Array.from(variantSelects.querySelectorAll('.product-form__input--dropdown'));
const productJson = JSON.parse(variantSelects.querySelector('[type="application/json"]').textContent);
let selectedOptions = [];

variantSelects.addEventListener('change', rebuildOptions);
this.rebuildOptions();

// gather a list of valid combinations of options, check to see if the input passed to it matches in a chain of valid options.
function validCombo(inputValue, optionLevel) {
  for(let i = 0; i < productJson.length; i++) {
    if(optionLevel == 1){
      if (productJson[i].option1 == selectedOptions[0] && productJson[i].option2 == inputValue) { return true; }
    } else {
      if (productJson[i].option1 == selectedOptions[0] && productJson[i].option2 == selectedOptions[1] && productJson[i].option3 == inputValue) { return true; }
    }
  }
  return false;
}

function rebuildOptions() {
  selectedOptions = fieldsets.map((fieldset) => {
    return (pickerType == 'radios') ? Array.from(fieldset.querySelectorAll('input')).find((radio) => radio.checked).value : Array.from(fieldset.querySelectorAll('select'), (select) => select.value);
  });
  //loop through the option sets starting from the 2nd set and disable any invalid options
  for(let optionLevel = 1, n = fieldsets.length; optionLevel < n; optionLevel++) {
    const inputs = (pickerType == 'radios') ? fieldsets[optionLevel].querySelectorAll('input') : fieldsets[optionLevel].querySelectorAll('option');

    inputs.forEach(input => {
      input.disabled = (validCombo(input.value,optionLevel)) ? false : true;
      if(pickerType == 'radios'){
        //get the label for the current input (this is what the user clicks, the "pill")
        const label = fieldsets[optionLevel].querySelector(`label[for="${input.id}"]`);

        label.style.display = (input.disabled) ? "none" : ""; //Hide the option, or comment this line out and use the following lines to style it..
        //label.style.opacity = (input.disabled) ? 0.5 : 1;
        //label.style.borderStyle = (input.disabled) ? "dashed" : "solid";
        //label.style.textDecoration = (input.disabled) ? "none" : "";
      } else {
        input.hidden = (validCombo(input.value,optionLevel)) ? false : true;
      }
    });
  }

  //if the default selected option is disabled with the function above, select the first available option instead
  for (let optionLevel = 1, fieldsetsLength = fieldsets.length, change = false; optionLevel < fieldsetsLength && !change; optionLevel++) {
    if(pickerType == 'radios'){
      if(fieldsets[optionLevel].querySelector('input:checked').disabled === true) {
        change = (fieldsets[optionLevel].querySelector('input:not(:disabled)').checked = true);
      }
    } else {
      if(fieldsets[optionLevel].querySelector('option:checked').disabled === true) {
        change = (fieldsets[optionLevel].querySelector('option:not(:disabled)').selected = "selected");
      }
    }
    //if a new option has been selected, restart the whole process
    if(change) variantSelects.dispatchEvent(new Event('change', { bubbles: true }));
  }
}
</script>

 

Easify Product Options: Any product options app can help you create custom options, but Easify turns those options into sales and conversions!
Easify Inventory Sync: Automate inventory syncing for Bundles, Duplicates, and Raw Materials. Say goodbye to manual tracking headaches.
Easify Product Attachments: Enrich your store with downloadable content. Easily add PDFs and other files to product or any page.
>>> Try Apps for Free | 24/7 Live Chat Support
Guifonte
Tourist
25 0 2

Hi, I've just tried it but that did not work 😞

EasifyApps
Shopify Partner
635 19 52

Could you please provide a screenshot? I'm currently relying on that code, and it seems to be functioning correctly. It's worth noting that after adding the code to the custom liquid, you have to click "Save" and refresh your theme editing page to see the results.

Just to clarify, this is a demo.

EasifyApps_0-1702528066934.pngEasifyApps_1-1702528078089.png

 

Easify Product Options: Any product options app can help you create custom options, but Easify turns those options into sales and conversions!
Easify Inventory Sync: Automate inventory syncing for Bundles, Duplicates, and Raw Materials. Say goodbye to manual tracking headaches.
Easify Product Attachments: Enrich your store with downloadable content. Easily add PDFs and other files to product or any page.
>>> Try Apps for Free | 24/7 Live Chat Support
Agreeableagony
Explorer
41 2 11

I am trying to accomplish the same thing, but in the Craft 13 theme (latest version of shopify Craft theme)

I tried this code as suggested here, but without any luck. 

Agreeableagony
Explorer
41 2 11

I see that in my theme the Variant Picker type is different:

"variant_picker": {
"type": "variant_picker",
"settings": {
"picker_type": "button",
"swatch_shape": "circle"

---
Store url is www.agreeableagony.com 

Earp
Tourist
5 0 1

This doesnt work in Dawn 13.0 unfortunately as im struggling with this issue now. Customers are able to filter for M and are presented with items that have a M variant but are out of stock and is causing major headache

 

Naren1
Shopify Partner
74 2 57

Hi @Earp 
We've launched an app to hide sold out variants to solve this common problem

HiVar : Hide Sold Out Variants

https://apps.shopify.com/hivar-hide-sold-out-variants

currently it supports all Online Store 2.0 themes 
App is free for single-option free themes and paid if you have 2-3 options
Check it out and feel free to reach out to customer support if you have any queries 

Naren1
Shopify Partner
74 2 57

Hi @Guifonte 

We've launched an app to hide sold out variants to solve this common problem

HiVar : Hide Sold Out Variants

https://apps.shopify.com/hivar-hide-sold-out-variants

currently it supports all Online Store 2.0 themes 
App is free for single-option free themes and paid if you have 2-3 options
Check it out and feel free to reach out to customer support if you have any queries 

jbags
Shopify Partner
2 0 2

I don't know if you found a solution to this, but I use the Dawn theme, and I followed this article and it actually worked!  I could've cried I was so happy.  

https://community.shopify.com/c/shopify-design/hide-variants-that-has-0-stock/td-p/2499383

Earp
Tourist
5 0 1

Yes this code works, i think i did cry. Now its just to find a solution for the fact of the sold out/hidden item showing as an option when filtering