Disable Variation Swatches for Specific Category in WooCommerce

[ad_1]

Hello WooCommerce Support Team,

I am currently using the WooCommerce Variation Swatches plugin to display product variations as swatches instead of dropdown menus. However, I need to make an exception for products in a specific category called “Anhängerkupplungen” (Towbars). For this category, I want the variations to be displayed as standard dropdown menus instead of swatches.

What I Have Tried So Far:

1. Functions.php Approach:

add_filter('wc_product_enable_variation_swatches', 'disable_swatches_for_towbar_category', 10, 2);

function disable_swatches_for_towbar_category($enabled, $product) {
if (has_term('Anhängerkupplungen', 'product_cat', $product->get_id())) {
return false; // Disable variation swatches
}
return $enabled; // Default behavior for other products
}

This approach did not disable the swatches for the specific category.

2. JavaScript Approach via Functions.php:

function custom_disable_variation_swatches_for_category() {
if (is_product() && has_term('Anhängerkupplungen', 'product_cat')) {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.variations_form').each(function() {
$(this).find('.swatch').hide();
$(this).find('select').show();
});
});
</script>
<?php
}
}
add_action('wp_footer', 'custom_disable_variation_swatches_for_category');

This approach also did not yield the desired result.

3. Custom JS File Enqueued via Functions.php:

function disable_variation_swatches_for_towbar_category() {
if (is_product() && has_term('Anhängerkupplungen', 'product_cat')) {
wp_enqueue_script('disable-swatches', get_stylesheet_directory_uri() . '/js/disable-swatches.js', array('jquery'), '1.0', true);
}
}
add_action('wp_enqueue_scripts', 'disable_variation_swatches_for_towbar_category');
This also did not work as expected.

Plugin Information:

Plugin Name: Variation Swatches for WooCommerce

Plugin URI: https://projectdmc.org/plugins/woo-variation-swatches/

Version: 2.1.0

WooCommerce Version: 7.5+

WordPress Version: 6.5

Desired Outcome:

I want the Variation Swatches plugin to be disabled for products in the “Anhängerkupplungen” category and for these products to use the standard dropdown menus for variations instead. The swatches should remain enabled for all other product categories.

Could you please provide guidance on how to achieve this? Any help or pointers would be greatly appreciated.

Thank you in advance for your support.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer