Hide Sold Out Variations
January 29, 2025 Β· AI Conversations
### Solution: Remove Sold-Out Variations from Dropdown
You can add the following code to your themeβs `functions.php` file:
add_filter('woocommerce_variation_is_active', 'hide_sold_out_variations', 10, 2);
function hide_sold_out_variations($is_active, $variation) {
if (!$variation->is_in_stock()) {
return false;
}
return $is_active;
}
### Explanation:
– This function checks if a variation is **out of stock** and removes it from the dropdown selection.
– If the variation is in stock, it remains selectable.
### Alternative: Use a Plugin
If you prefer not to modify code, you can use a plugin like:
– **WooCommerce Variation Swatches**
– **WooCommerce Stock Manager**
Let me know if you need further adjustments! π
—
### β
Fix: Ensure Sold-Out Variations Are Displayed but Greyed Out
Try **removing** any custom code that hides out-of-stock variations. If you have added something like this before:
add_filter('woocommerce_variation_is_active', function($is_active, $variation) {
return $variation->is_in_stock();
}, 10, 2);
Remove it, as it completely hides the variation.
—
### β
Proper Code to Keep Sold-Out Variations Greyed Out
If your theme or plugins are altering this behavior, try explicitly allowing WooCommerce to display **all** variations but keep them disabled when out of stock.
Add this to your `functions.php` file:
add_filter('woocommerce_variation_is_active', 'keep_sold_out_variations_greyed', 10, 2);
function keep_sold_out_variations_greyed($is_active, $variation) {
return true; // Ensures variations remain visible
}
This ensures that **all variations** remain in the dropdown, but WooCommerce will naturally grey out the ones that are out of stock.
—
### β
Check WooCommerce Settings
Go to:
**WooCommerce β Settings β Products β Inventory**
Make sure **βHide out-of-stock items from the catalogβ** is **unchecked**.
—
### β
Check for Theme or Plugin Conflicts
If the issue persists, test by:
1. Switching to the **default Storefront theme**.
2. Disabling **all plugins except WooCommerce** and checking the dropdown.
If the variations reappear, re-enable your theme and plugins one by one to identify the conflict.
—
### Let me know if you still face issues! π
—
### β
Solution: Disable Sold-Out Variations in the Dropdown
Add the following jQuery script to your theme. You can place it in:
**Appearance β Theme Editor β `footer.php` (before “)**
or
Use a plugin like **Code Snippets** to add the script.
jQuery(document).ready(function ($) {
$('select[name^="attribute_"] option').each(function () {
if ($(this).text().includes("(Out of stock)")) {
$(this).prop('disabled', true);
}
});
});
—
### β
Explanation:
– This script looks for variations in the dropdown.
– If the option **contains "Out of stock"**, it **disables** that option.
– The "(Out of stock)" text is automatically added by WooCommerce, so this method works dynamically.
—
### β
Alternative: Add a CSS Style (Optional)
If you want to visually **style the disabled variations**, add this to **Appearance β Customize β Additional CSS**:
.variations select option:disabled {
color: grey !important;
font-style: italic;
}
—
### β
Bonus: Ensure "(Out of stock)" Appears in Dropdown
If your theme doesnβt show "Out of stock" in the dropdown, add this PHP snippet to `functions.php`:
add_filter('woocommerce_variation_option_name', function ($name, $variation, $product) {
if (!$variation->is_in_stock()) {
return $name . ' (Out of stock)';
}
return $name;
}, 10, 3);
—
### π Final Result:
– Sold-out variations will **appear** in the dropdown.
– They will be **disabled (unclickable)**.
– The styling will indicate that they are **unavailable**.
Let me know if you need further refinements! π―