• Resolved alfista

    (@alfista)


    Hi,

    please can anybody help me how can I get Tag ID’s from Tags added to a product?
    I need to get a separate numbers, which can be used in another function.

    Thanks.

Viewing 9 replies - 1 through 9 (of 9 total)
  • You can do it simply with:

    // Get the product tag IDs for a defined Product ID (or $post_id)
    $product_tags = wp_get_post_terms( $post_id, 'product_tag', array('fields' => 'ids') );

    Then you will get an array of the product tags set in your product

    Thread Starter alfista

    (@alfista)

    OK and how can I then work with the array, while I need then use the tag id’s separately for showing images which will be used as tag pictograms.
    And what are the table names for tag name and tag slug, that I then create full link with alt name.

    Sorry I learn only all about WP, PHP and CSS ??

    Thanks.

    • This reply was modified 7 years, 4 months ago by alfista.
    • This reply was modified 7 years, 4 months ago by alfista.

    With a foreach loop:

    // Get the product tag IDs for a defined Product ID (or $post_id)
    $term_ids = wp_get_post_terms( $post_id, 'product_tag', array('fields' => 'ids') );
    
    // Iterating through each Product tag ID
    foreach( $term_ids as $term_id ){
        // Do something with each product tag ID 
    }
    • This reply was modified 7 years, 4 months ago by LoicTheAztec.
    • This reply was modified 7 years, 4 months ago by LoicTheAztec.
    Thread Starter alfista

    (@alfista)

    Hi,

    I try to use it modified, and will also to add and use there tag name and slug, that I can build a complete img with alt name and link,

     	<?php 
     	 	// Get the product tag IDs for a defined Product ID (or $post_id)
    		$term_ids  = wp_get_post_terms( $post_id, 'product_tag', array('fields' => 'ids') );
    		$term_name = wp_get_post_terms( $post_id, 'product_tag', array('fields' => 'name') );
    		$term_slug = wp_get_post_terms( $post_id, 'product_tag', array('fields' => 'slug') );
    		$url = site_url();
    
    		// Iterating through each Product tag ID
    		foreach( $term_ids as $term_id )
    		{
      			  // Do something with each product tag ID
      			  <a href=$url"/product-tag/acid_resistant/"><img src=$url"wp-content/uploads/2017/07/"$term_id".gif" alt="text"$term_id"text" width="50" height="50" /></a>
      			  echo $term_id;
    		}
     	 ?>

    but it will not work.
    I try to put it in meta.php, that I can show tags as pictograms and not as text.

    Please can you tell me where I done a mistake?

    Thanks.

    • This reply was modified 7 years, 4 months ago by alfista.

    Hi @alfista

    Here is the completed and tested answer:

    <?php

    // The taxonomy for Product tags
    $taxonomy = 'product_tag';
    
    // Get the Product Tag WP_Term Object for a defined Product ID (or $post_id)
    $product_tags  = wp_get_post_terms( $post_id, $taxonomy, array('fields' => 'all') );
    
    // The image path
    $img_path = site_url( '/wp-content/uploads/2017/07/' );
    
    // Iterating through each Product tags WP_Term Object
    foreach( $product_tags as $term_obj )
    {
    	// Getting te data from each WP_Term Object
    	$term_id = $term_obj->term_id; // ID
    	$term_slug = $term_obj->slug; // Slug
    	$term_name = $term_obj->name; // Name
    
    	// The image link (src)
    	$img_src = $img_path . $term_id . '.gif';
    
    	// The html output (Added the tag name after the tag thumbnail)
    	?>
    	<a href="<?php echo $term_link; ?>" class="tag-<?php echo $term_slug; ?>" title="<?php echo $term_name; ?>">
    		<img src="<?php echo $img_src; ?>" alt="<?php echo $term_name; ?>" width="50" height="50" />
    		<span class="tag-name"><?php echo $term_name; ?></span>
    	</a>
    	<?php
    }
    
    ?>

    The only thing is to set correctly your product tag thumbnail names (here is based on the id and it should be better to use something like “tag-” + the tag name

    Cheers

    Thread Starter alfista

    (@alfista)

    Hi,

    thanks for it but it will not work. The function foreach() will not work. Anything that I put there doesnt show.

    Under the:

    $img_src = $img_path . $term_id . '.gif';

    I putted:

    echo $term_slug;
    echo $img_src;

    only for check the correct names and links but I don’t have there anything. I don’t see there any code from this function. You can see it at: Test Product

    You don’t know where can be the problem? I have inserted the code in the woocomerce meta.php file.

    Thanks.

    • This reply was modified 7 years, 4 months ago by alfista.
    Thread Starter alfista

    (@alfista)

    Hi,

    I correct it ??
    I found the problem. I needed to change the post id to product id and it started to work. And I have then also made there a some changes it the links and product names.

    Thanks, now all is working.

    PS: Maybe you can tell me when you know so good the WP and WC, how is possible to do add some extra fields to the shipping tab in backend in products, while I have about 12 fields which I will use in compare plugins to see. I tried to find any plugin or hook which can help me, but only way that I found was over attributes, but there is a problem that I need then for some attributes import so many parameters, while some of them are unique for each product like volume, buoyancy, int. and ext. dimensions. Please can you help me and show me the correct way how to do it?

    Again thanks ??

    Hello

    You should mark this tread as “solved” now then…

    I can’t help anymore on your PS (sorry) …

    You should ask for example in stackOverFlow forum, adding the code you got to your question and explaining with details what you would like to have or to get…

    Thread Starter alfista

    (@alfista)

    OK thnaks.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Help with Tag ID’s….’ is closed to new replies.