Closed. This question is off-topic. It is not currently accepting answers.admin管理员组文章数量:1122846
Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 4 months ago.
Improve this questionadd_filter('woocommerce_dropdown_variation_attribute_options_args','fun_select_default_option',10,1);
function fun_select_default_option($args)
{
if(count($args['options']) === 1) //Check the count of available options in dropdown
$args['selected'] = $args['options'][0];
return $args;
}
I am using the above code to autoselect options when there is only 1 option available. Any ideas on how I can also mark that single option as selected? now it looks like if it is not selected.
Closed. This question is off-topic. It is not currently accepting answers.Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 4 months ago.
Improve this questionadd_filter('woocommerce_dropdown_variation_attribute_options_args','fun_select_default_option',10,1);
function fun_select_default_option($args)
{
if(count($args['options']) === 1) //Check the count of available options in dropdown
$args['selected'] = $args['options'][0];
return $args;
}
I am using the above code to autoselect options when there is only 1 option available. Any ideas on how I can also mark that single option as selected? now it looks like if it is not selected.
Share Improve this question asked Sep 19, 2024 at 13:00 AnandaAnanda 111 bronze badge1 Answer
Reset to default 0First, you need to ensure that your count($args['options']
is actually 1.
Check the following example:
You can see that when I print_r($args['options'])
, I got 4 elements, so count($args['options']
here is 4. But if you look at the dropdown beneath it, you see that it only has 1 option.
Why? Because variations (and their attributes) that do not have prices will not be shown in your store!
So how can you select the first option in this case (or in any cases), based on the number of options in the generated HTML? You can use jQuery with the following code:
jQuery(document).ready(function($) {
// Loop through each select element within the table with class .variations
$('.variations select').each(function() {
var $select = $(this); // Cache the current select element
var numberOfOptions = $select.find('option').length; // Get the number of options, including the first, placeholder option
// Check if the select contains exactly 2 options
if (numberOfOptions === 2) {
// Select the last option
$select.val($select.find('option:last').val()).change(); // Change the value and trigger change event
}
});
});
Note: In this case, the variation table has class .variations
. Check your code and change the class if needed.
本文标签: woocommerce offtopicProduct Variation Auto Select when only one 1 and mark the option
版权声明:本文标题:woocommerce offtopic - Product Variation Auto Select when only one 1 and mark the option 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736290779a1928584.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论