0
votes

I'm working on a Woocommerce shop that has its products organized by parent categories such as Brand, Color, Size, etc. Each of these top-level categories has a number of subcategories: for example, subcategories of Color include Red, Blue, Pink, etc.

Client wants to display each product's brand and size (the products are shoes) below each product thumbnail in the grid/loop view on the front and archive pages.

Each product is only one brand, but might have multiple sizes available.

So essentially, I need to know if there's a way to display the title of only a specific subcategory for a single product, like so:

[Product Thumbnail here]
[Price info here]
Brand: Nike
Size(s): 8, 8.5, 9

I just need the subcat titles, not links. I know the solution to this will likely involve calling the get_terms() function in content-product.php, but I haven't been able to figure out how to list only specific subcategories for a product.

The code I'm looking for will basically say:

If this product is in the Brand category,
    Then show its subcategory.

If this product is in the Size category,
    Then show its subcategories.
1

1 Answers

0
votes

check this link :

https://developer.wordpress.org/reference/functions/get_terms/

and paste this code in your shop page

global $product; 

// Get product attributes
$attributes = $product->get_attributes();

if ( ! $attributes ) {
    echo "No attributes";
}

foreach ( $attributes as $attribute ) {

        echo $attribute['name'] . ": ";
        $product_attributes = array();
        $product_attributes = explode('|',$attribute['value']);

        $attributes_dropdown = '<select>';

        foreach ( $product_attributes as $pa ) {
            $attributes_dropdown .= '<option value="' . $pa . '">' . $pa . '</option>';
        }

        $attributes_dropdown .= '</select>';

        echo $attributes_dropdown;
}