Take a custom taxonomy value and save as a standard Product Tag

Question

I’m trying to find a way to take a custom taxonomy value, and add it as a tag to that same product.

Long story short: a third party CRM only respects woo native categories & tags, so we’d like to take three custom taxonomy values and save them as tags. For instance, if a product is a Ganni dress in UK size 10 and has a ‘style profile’ of Romantic, then the three custom tax values retrieved will be ‘ganni’, ‘uk-10’, and ‘romantic’. These should then be saved as standard tags.

Here’s a stab at the ‘product_brand’ custom tax, which doesn’t work. Can anyone please point me to where it’s going wrong?

(This needs to happen any time a product is created or saved)

add_action( 'save_post', 'action_save_product_data', 20);
function action_save_product_data( $post_id ) {
    global $typenow;
    if ( 'product' === $typenow ) {

        $terms = wp_get_post_terms( $post_id, 'product_brand', array( 'fields' => 'all' ) );
                if ( $terms ) {
                    $prod_brand = $terms[0]->slug;
                }

        wp_set_object_terms( $post_id, $prod_brand, 'post_tag', true );
    }
}

0
Austen Osborne 3 months 2022-05-10T02:06:55-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse