Wordpress: saving a taxonomy based on the value of a different taxonomy

164 Views Asked by At

We're building a clothing store. The products that will be uploaded could have label sizes from various countries, and as such we have two size taxonomies: product_size and product_size_uk.

I'm trying to set the uk size based upon the international size at the point the product is saved/updated, and running a switch to set a variable to the id of the uk size taxonomy and using wp_set_object_terms. But this isn't working:

add_action( 'save_post', 'save_uk_size' );
function save_uk_size( $post_id ) {
   if ( $post->post_type == 'product' ) {

        $post = get_post($post_id);
        $terms = wp_get_post_terms( $post->ID, 'product_size', array( 'fields' => 'all' ) );
            if ( $terms ) {
                $prod_size_int = $terms[0]->slug;
            }

            switch ($prod_size_int) {
                ...
                case "FR-36":
                  $prod_size_uk = 805;
                  break;
                case "FR-38":
                  $prod_size_uk = 806;
                  break;
                ...
                }

     wp_set_object_terms($post_id, $prod_size_uk, 'product_size_uk');
   }

}

Can anyone steer me in the right direction? Thanks!

1

There are 1 best solutions below

3
On
  1. First you need to install Advanced Custom Fields plugin

  2. Then for taxonomy product_size we create Taxonomy field (product_size_uk).

Now in the pages of taxonomy product_size in admin panel there is a field where we can choose which product_size_uk corresponds to this taxonomy.

3.Then we have to choose product_size_uk matching for all sizes.

And then this code

add_action( 'save_post_product', 'product_save_new_term' );
function product_save_new_term($post_id) {
    
        remove_action( 'save_post_product', 'product_save_new_term' );    

        $terms = wp_get_object_terms( $post_id, 'product_size' );
        
        $term_id = $terms[0]->term_id;
        
        if($term_id != "") {        

        $key_for_field = 'product_size_' . $term_id;
        
        $product_size_uk_value = get_field( 'product_size_uk', $key_for_field );
        
        wp_set_object_terms($post_id, $product_size_uk_value, 'product_size_uk');
        
        clean_post_cache( $post_id );
        
        } else {
            
        wp_delete_object_term_relationships( $post_id, 'product_size_uk' );
        
        }
        
        
        add_action( 'save_post_product', 'product_save_new_term' );
}

Or, if you don't want to have controls for size matching in the admin panel and it's easier for you to write everything in the code.

add_action( 'save_post_product', 'product_save_new_term' );
function product_save_new_term($post_id) {

       $data_array = array(
       "product_size_term_id" => "product_size_uk_term_id",
       "product_size_term_id2" => "product_size_uk_term_id2",
       "product_size_term_id3" => "product_size_uk_term_id3",
       );
       
        remove_action( 'save_post_product', 'product_save_new_term' );


        $terms = wp_get_object_terms( $post_id, 'product_size' );
        
        $term_id = $terms[0]->term_id;
        
        if($term_id != "") {    

        
        
        $product_size_uk_value = $data_array[$term_id];
        if($product_size_uk_value !="") {
        wp_set_object_terms($post_id, $product_size_uk_value, 'product_size_uk');
        }
        clean_post_cache( $post_id );
        
        } else {
            
        wp_delete_object_term_relationships( $post_id, 'product_size_uk' );
        
        }
        
        
        add_action( 'save_post_product', 'product_save_new_term' );
}