Reputation: 315
Actually I have custom attributes in product for after this create searches for these attributes , the case it´s I try show all attributes and values but no get finally
For example actually I have the attribute called colors, in this attribute I have different values, red, green yellow, blue
For show the attributes and his values i use this code :
<?php global $product; $terms=get_terms('pa_colors'); print "<select>"; foreach ($terms as $each_term) { echo '<option>'.$each_term->name.'</option>'; } print "</select>"; ?>
The case it´s this function only show me 2 colors and no the rest of colors, I see in the woocommerce backend I have only 2 products and have these colors that show with this function , but no the other colors, the case it´s in the backend of woocommerce for attributes I can see all attributes and colors and I want show the same as backend but in front, but this function no let me show all values for this attribute called colors.
How do I use only 2 color from attribute called colors only show me these 2 colors but no the others.
My question is, how can I show all colors or all values from one attribute in the front?
The result must be this :
<select>
<option>Red</option>
<option>Green</option>
<option>Yellow</option>
<option>Blue</option>
</select>
Thanks for the help. Regards
Upvotes: 2
Views: 12497
Reputation: 731
try:
function GetProductAllParamsById($_idProduct = null) {
if ( func_num_args() > 0 ) {
$result = Array();
$productAllAttr = get_post_meta( $_idProduct, '_product_attributes' );
foreach ($productAllAttr as $value) {
while (count($value) > 0) {
$_instValue = array_pop($value);
$_nameParam = $_instValue['name'];
$_nameProductAttr = wc_get_product_terms( $_idProduct, $_nameParam, array( 'fields' => 'names' ) );
array_push($result, [$_nameParam => $_nameProductAttr]);
}
}
}
return isset($result) ? $result : null;
}
Example:
echo "<pre>";
echo print_r( GetProductAllParamsById($id_product) );
echo "</pre>";
Return:
Array (
[0] => Array
(
[pa_colors] => Array
(
[0] => Blue
)
)
[1] => Array
(
[pa_customer] => Array
(
[0] => someparam
[1] => sometwoparam
)
)
)
Upvotes: 1
Reputation: 3615
Use this SQL Query to get all attributes with details
global $wpdb;
$attribute_taxonomies = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies WHERE attribute_name != '' ORDER BY attribute_name ASC;" );
set_transient( 'wc_attribute_taxonomies', $attribute_taxonomies );
$attribute_taxonomies = array_filter( $attribute_taxonomies ) ;
prin_r($attribute_taxonomies);
Upvotes: 1
Reputation: 1323
When using get_terms()
, WordPress filters out terms that aren't attached to published posts (and WooCommerce stores your products as posts). Luckily, WordPress will allow you to prevent this from happening. Try the following:
$options = array('hide_empty' => false);
$terms = get_terms('pa_colors', $options);
Many other options are explained in the codex: https://codex.wordpress.org/Function_Reference/get_terms
Upvotes: 5