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 ...
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
One word (true) and does wonders ????
Thank you very much. It works as I expected.