Mark-Tomlinson/markup-by-attribute-for-woocommerce

Enhancement for Handling Custom manually added non-Taxonomy Terms in WooCommerce Product Attributes

Closed this issue · 3 comments

Description

We identified an area for improvement in our WooCommerce setup concerning the iteration over product attributes to retrieve and merge taxonomy terms. The initial approach was partially successful but required refinement to ensure robust functionality and to address issues with dynamic taxonomy term retrieval for product variations.

Original Approach

The original code aimed to iterate through all attributes of a product and retrieve terms for each associated taxonomy. It then intended to merge all these terms into a single array. However, this approach encountered limitations, particularly with the dynamic aspects of taxonomy handling.

Proposed Solution

Below is the refined code snippet that addresses the aforementioned issues:
markup-by-attribute-for-woocommerce
---src
------backend
---------product.php
Rows from 161 to 170

        // Loop through product attributes
        foreach (wc_get_product($product_id)->get_attributes() as $pa_attrb) {
            if($pa_attrb->is_taxonomy()){
                $taxonomy = $pa_attrb->get_name();
                // Retrieve all attribute terms
                $terms = get_terms(["taxonomy" => $taxonomy, "hide_empty" => false]);
                $all_terms = array_merge($all_terms, $terms);

            }
        }

I see you added,

            if($pa_attrb->is_taxonomy()){
                ...
            }

I'll test it out and release an update soon.

Can you give me an example of a "custom manually added non-taxonomy term"? I want to reproduce the problem you are encountering.

A non-taxonomy term is one added at product creation inside attributes but not as global attribute
Custom manually added
Custom manually added
Global created taxonomy attribute
Global created taxonomy attribute

Oh, that's simple enough - attributes that aren't global.

I'm running tests today and tomorrow. Expect an update by the end of the week.