Skip Navigation

[Resolved] CRED – adding CPT terms

This support ticket is created 7 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by Bochnacki 7 years, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#557942

I have the ability to highlight posts.
This is done by matching the corresponding products with the CPT "Payment" terms.

add_action( 'woocommerce_order_status_completed', 'add_featured_option', 10, 1 );
function add_featured_option( $order_id ) { 
    $cred_form_id = get_post_meta($order_id, '_cred_form_id', true);
    $cred_form_ids = array(39);// here you can add more CRED form IDs    
    if( in_array($cred_form_id, $cred_form_ids) ){
        $order = wc_get_order( $order_id ); 
        $post_id = get_post_meta($order_id, '_cred_post_id', true); 
        $terms = array();
        foreach ( $order->get_items() as $item ) {
            $product_id = $item['product_id'];
            if($product_id == 1174){ // this is product ID of "home"
                $terms[] = 795; // this is term ID of "inhome"
            }
            if($product_id == 981){ // this is product ID of "featured"
                $terms[] = 796; // this is term ID of "featured"
            }
            if($product_id == 1175){ // this is product ID of "intop"
                $terms[] = 797; // this is term ID of "intop"
            }
        } 
        if(!empty($terms)){
            $term_taxonomy_ids = wp_set_object_terms( $post_id, $terms, 'payment' );
        }
    }
}

Works well, but ...
If I have already added a "Payment" option and want to add another, then this one added earlier is deactivated. For example: I have added the "featured" option and added the "intop" option, the "featured" option disappears, no longer assigned to the post.
It should be possible to add all three options.

I'm trying to change that, but I'm not capable ...

#557983

Dear Bochnacki,

I assume the CRED form (ID 39) is for editing post, you are going to append a taxonomy term to the post after the woocommerce order is completed.

If it is, please try this, edit this line from:

$term_taxonomy_ids = wp_set_object_terms( $post_id, $terms, 'payment' );

To:

$term_taxonomy_ids = wp_set_object_terms( $post_id, $terms, 'payment', true );

More help:
https://codex.wordpress.org/Function_Reference/wp_set_object_terms
$append
(bool) (optional) If true, terms will be appended to the object. If false, terms will replace existing terms
Default: False

#558110

One word (true) and does wonders ????
Thank you very much. It works as I expected.