Forum Replies Created

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter lizamarivb

    (@lizamarivb)

    Hello,

    Im using a plugin CPT UI to create a custom post type example Apples.

    So No i need to add the products to the woocommerce post type products and to the post type apples.

    Hi,

    What i could see its something to do with your scripts (JS). or setup of your paypal account.

    https://c2n.me/4exbsmI

    Some field or value is not configured – maybe have a look at your setup.
    When you hover over the button as well you get message that it’s in a iframe:
    https://c2n.me/4exbBqG

    Hope it helps somewhat

    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?

Viewing 3 replies - 1 through 3 (of 3 total)