Select two attributes to display on the Category page of Woocommcere

Question

I am using the code below to display the in-stock sizes of clothing items on the category page.

The issue I am trying to resolve is that on the footwear category, they use a different attribute. So ideally I am looking to add an additional attribute to the code below.

Maybe something like:

$taxonomy = 'pa_size, pa_shoesize'; // The product attribute taxonomy

add_action( 'woocommerce_after_shop_loop_item_title', 'display_instock_sizes', 5 );
function display_instock_sizes() {
    global $product;

    if ( $product->is_type('variable') ) {
        $taxonomy    = 'pa_size'; // The product attribute taxonomy
        $sizes_array = []; // Initializing

        // Loop through available variation Ids for the variable product
        foreach( $product->get_children() as $child_id ) {
            $variation = wc_get_product( $child_id ); // Get the WC_Product_Variation object

            if( $variation->is_purchasable() && $variation->is_in_stock() ) {
                $term_name = $variation->get_attribute( $taxonomy );
                $sizes_array[$term_name] = $term_name;
            }
        }

        echo '<span class="attribute-size">' . implode( ', ', $sizes_array ) . '</span>';
    }
}

Finally, displaying the results as a pop-up when hovering over the product image would top things off perfectly if at all possible?

This is my first ever post, please be gentle 🙂

0
, Plus Internet 4 years 2020-03-24T08:52:02-05:00 0 Answers 109 views 0

Leave an answer

Browse
Browse