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

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 );
    }
}

Topic custom-taxonomy Wordpress

Category Web


Got it worked out, working code as follows:

function action_save_product_data( $post_id ) {
  if ( 'product' !== $post->post_type ) {
    $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, 'product_tag', true );
    }
  }
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.