How to set a attribute to be optional and invisible?
-
Hello,
Thanks for the great plugin.
We would like to set value of gender to product like Male, Female, Unisex, and set product feeds to Google, that is recommend by Google.
So, I set gender globally:
and please note that pa_gender is included in the url :
https://mysite.com/wp-admin/edit-tags.php?taxonomy=pa_gender&post_type=product
For a specific variable product, some variations are Unisex, but some are Female, and please check the screenshot to understand what I mean:
There are blue, black green colors, and the gender of these colors varaitons should be unisex, but pink color one should be Female.
However, for user-friendly purpose, the Gender option are useless, and these option are sometimes cause the customer confused. The Gender here are actually only used for Google feeds.
So, I tried code snippet to set gender optional but not required before adding to cart, and tried to hide Gender option by CSS. Here are code snippts I tried:
// Make the gender attribute optional in the dropdown
add_filter('woocommerce_dropdown_variation_attribute_options_args', 'make_gender_optional', 10, 1);
function make_gender_optional($args) {
if (isset($args['attribute']) && $args['attribute'] === 'pa_gender') {
$args['show_option_none'] = __('No preference', 'woocommerce');
$args['option_none_value'] = ''; // Optionally set the value to empty
}
return $args;
}
// Ensure variations are active regardless of attribute selection
add_filter('woocommerce_variation_is_active', 'allow_empty_gender_variation', 10, 2);
function allow_empty_gender_variation($is_active, $variation) {
return true; // Always show variations as active
}Unfortunately, it does not work.
Considering this plugin is activated, and I tried the code below:
// Make the gender attribute optional in the dropdown
add_filter('woocommerce_dropdown_variation_attribute_options_args', 'make_gender_optional', 10, 1);
function make_gender_optional($args) {
if (isset($args['attribute']) && $args['attribute'] === 'pa_gender') {
$args['show_option_none'] = __('No preference', 'woocommerce');
$args['option_none_value'] = ''; // Optionally set the value to empty
}
return $args;
}
// Ensure variations are active regardless of attribute selection
add_filter('woocommerce_variation_is_active', 'allow_empty_gender_variation', 10, 2);
function allow_empty_gender_variation($is_active, $variation) {
return true; // Always show variations as active
}
// Adjusting for Variation Swatches for WooCommerce plugin
add_filter('woocommerce_variation_option_name', 'adjust_variation_option_name', 10, 1);
function adjust_variation_option_name($name) {
// Check if the attribute is 'pa_gender'
if ($name === 'pa_gender') {
$name = ''; // Or customize as needed
}
return $name;
}Unfortunately, it does not work either.
Could you please show a code snippet to better match this plugin and set gender to be optional and set it invisiable at frontend please?
Thank so much
- You must be logged in to reply to this topic.