OiO.lk Blog PHP Get WooCommerce product attribute term name from its slug in the current context
PHP

Get WooCommerce product attribute term name from its slug in the current context


I am building a custom table for products, I am attempting to add both simple products and variations to the same table. I have done this so far but I need to modify the title for variation products. I am attempting to do this format:
"Product Title
Black, 8 in."

I have this code:

foreach ($available_variations as $variation) { 
            //$attributes = $product->get_attributes();
            $new_attributes = $variation['attributes'];
            $data_array = array();
            $allowed_attr = ['pa_color', 'pa_gauge', 'pa_size', 'pa_dimensions', 'pa_quantity', 'pa_length', 'pa_material'];
            foreach ( $new_attributes as $meta_key => $meta ) {
                $attr_label   = wc_attribute_label( $meta_key, $product );
                $attr_value = $product->get_attribute($meta_key);
                array_push($data_array, $meta);
            }
            
            $attribute_sub = implode( ', ', $data_array );

This gets close to the desired format when echoed. It ends with:
"Product Title
24-black, 8-in"

I can only use the variation object in the current context, is there any function I can use that would only take the attribute group slug and the attribute value to get the Attribute label?
Something that might work like this:
get_attribute_lable("24-black", "pa_color"); –> Returns "Black"

I’ve attempted to use wc_attribute_label but it seems it only works with the attribute group. get_taxonomy also only gets the group from what I’ve attempted. If I do need to write a custom function what would be the quickest without having to open an SQL query?



You need to sign in to view this answers

Exit mobile version