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

Price markup by attribute term for WooCommerce
GNU General Public License v3.0
3 stars 3 forks source link

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

Closed g-alfieri closed 2 months ago

g-alfieri commented 5 months ago

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);

            }
        }
Mark-Tomlinson commented 5 months ago

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.

g-alfieri commented 5 months ago

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

Mark-Tomlinson commented 5 months ago

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.