• Resolved lizamarivb

    (@lizamarivb)


    Hello,

    I want to use my products attributes as the base or rule to add the products to categories dynamically. So what we want to achieve is:

    1. We import the products via a csv once and locate the attributes to the fields to allocate it as needed (via All WP Import).
    2. Based on the product attributes the product should be added to the categories. e.g. if an attribute is distance. all the products with distance A, D and Z should be added to product category AA, DD, ZZ.

    We want to use it then for filtering.

    I have already created the custom taxonomy category and could populate 2 attributes but struggle to wrap my head around to achive the above. I have googled (grouped products, how to use attributes as your category base…. no luck).

    Can someone please asssist.

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi @lizamarivb,

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the following places for more development-oriented questions:

    Thread Starter lizamarivb

    (@lizamarivb)

    Hi hi, ok I have found a solutions but not my final destination (maybe someone can benefit from this).

    I managed to write this code:
    So first I identified the specific attributes of the products I wanted.
    $show_attributes = $product->get_attribute('pa_attribute-name')

    Then I had echo-ed the attributes to ensure my function works as I wanted it to work (comparing 2 products).
    echo $show_attributes;

    After I determined the desired outcome I had to change the quary from attributes to terms of the product (newby developer here – just the way I figured it out).
    The terms is then used as the if statements recipe.
    e.g. If the product has term apples then its true and do the code.

    So I used this to determine the BASE of my category and then with the if statement I had the desired categories added to the product.
    $product->set_category_ids([888,889,890]);

    and saved the product via php.
    $product->save();

    I could only manage to get this function fired if the product is viewed.
    (woocommerce_before_single_product) I cannot figure out how to fire this function on page/website load to ensure all new products are added to their categories.

    Below the code in full.

    function myFunction() {
        global $product;
    
        $terms = get_the_terms( $product->id, 'pa_attribute-name');
        $show_attributes = $product->get_attribute('pa_attribute-name');
         foreach($terms as $term){
            if($term->name == 'term-name'){ //term name is apples as per explination
                <code>$product->set_category_ids([888,889,890]);</code>
                $product->save();
                echo $show_attributes;
    		}
    }
    add_action('woocommerce_before_single_product', 'myFunction', 1);

    But now I need to figure out how to change this line:
    $product->set_category_ids([888,889,890]);

    To add it to a custom post type and not the built in product categories of WP.

    I tried with a shot in the dark this:
    $product->set_category_ids(‘custom_post_type’, [888,889,890]);
    And wordpress ignored me like a stop sign.

    Hope someone can assist?

    Mirko P.

    (@rainfallnixfig)

    Hi @lizamarivb

    Thank you for sharing your solution!

    We’ve not seen much activity on this thread for a while, since you need further inputs I’m going to leave it open for a while in case anyone else wants to chime in to help you out.

    Cheers.

    Mirko P.

    (@rainfallnixfig)

    Hi there,

    This thread has been inactive for a bit, so I’m going to mark it as resolved now.

    Hopefully, you were able to find a solution and the above resources for developers were helpful. If you have further questions, please feel free to open a new topic.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘WooCommerce Products added to a category based on attributes’ is closed to new replies.