Reputation: 681
I've got a question regarding woocommerce and its attributes. I've got about 10 attributes like: speed, weight, color, Engine etc. What I'm trying to do is to create a search form on one page, and user after filling/selecting proper options via select fields will search for products.
Yet I've stucked with a piece of code which is suppose to create such dropdown (the one which will display ALL VALUES of specific attribute).
Code:
<?php
$subheadingvalues = get_the_terms( $product->id, 'pa_naped');
if ($subheadingvalues): ?>
<select>
<?php foreach ( $subheadingvalues as $subheadingvalue ): ?>
<option value="<?php echo $subheadingvalue->name; ?>">
<?php echo $subheadingvalue->name; ?>
</option>
<?php endforeach; ?>
</select>
<?php endif; ?>
Main issue of this code? Simply doesn't work - displays nothing. I've double checked the attribute name in woocommerce dashboard and its name indeed is pa_naped (engine). Why its not working?
Secondly, what if I would like to make same thing but for ALL selected by me attributes (mentioned above). Should I make a kind of array or sth?
Upvotes: 0
Views: 3610
Reputation: 690
This is how I did for dietary-preference
taxonomy.
$attrargs = array(
'show_option_all' => '',
'show_option_none' => 'Dieta',
'orderby' => 'id',
'order' => 'ASC',
'show_count' => 1,
'hide_empty' => 1,
'child_of' => 0,
'exclude' => '',
'echo' => 1,
'selected' => 0,
'hierarchical' => 0,
'name' => 'pa_dietary-preference',
'id' => 'pa_dietary-preference',
'class' => 'pa_dietary-preference',
'depth' => 0,
'tab_index' => 0,
'taxonomy' => 'pa_dietary-preference',
'hide_if_empty' => false,
'option_none_value' => -1,
'value_field' => 'term_id',
'required' => true,
);
<?php wp_dropdown_categories( $attrargs ); ?>
Upvotes: 0
Reputation: 3562
To get all values of a term you need to use get_terms()
so your functions should look like the following:
$subheadingvalues = get_terms('pa_naped', array(
'hide_empty' => false,
));
?> <select>
<?php foreach ($subheadingvalues as $subheadingvalue): ?>
<option value="<?php echo $subheadingvalue->name; ?>">
<?php echo $subheadingvalue->name; ?>
</option>
<?php endforeach;?>
</select>
Upvotes: 1