Get WooCommerce products tags for array of products

徘徊边缘 提交于 2019-12-23 16:30:12

问题


Is it possible to get a list of WooCommerce product tags, but only if they are associated with an array of specific products?

I have an array of product id's but can't fathom out where to start - I have looked at a lot of the get_tag functions in the Wordpress Codex documentation, but they all seem to be for individual post/pages/custom post types only and not for use in an array.


回答1:


Why not iterate through the array get all the tags for list of products.

Here is the Code which you can use:

function wdm_get_tags($products_array){
    echo "Tags: ";
    foreach($products_array as $single_product)
    {
        echo get_the_term_list($single_product, 'product_tag', '', ',' );
        echo ',';
    }

}

Here is the code by which you can test the function:

$products=array(378,356);
    wdm_get_tags($products);

Here is the Output for a demo setup which i tried on:

This code works fine. Let me know if this resolved you problem.




回答2:


With help from WisdmLabs, I've managed to put some code together that gets the products tags found for all products within a particular WooCommerce brand.

I have also added a little bit of functionality such as removing duplicate tag values and adding a total alongside each tag with the number of products having that tag so the output looks like…

• Product Tag 1 (12)
• Product Tag 3 (4)
• Product Tag 4 (7)
• Product Tag 8 (11)

Code is below for anyone that would like to use it or adapt it. I am outputting the results below the brand description using the WooCommerce Brand hook woocommerce_archive_description.

    function get_wc_product_brand_related_tags() {
    global $post; 

    // Access the objects in the current WooCommerce query loop
    $queried_object = get_queried_object();

    // Get the term_id and term_name values
    $term_id = $queried_object->term_id;
    $term_name = $queried_object->slug;

    // FOR TESTING ONLY - Echo the term_id and term_name values to see if they are what we're expecting
    //echo '<p>The <em>term_id</em> is <strong>' . $term_id . '</strong> and the <em>term_name</em> is <strong>' . $term_name . '</strong>.</p>';

    // Create our products array for use later
    $products_array = array();

    // Query the posts using the product custom post type and product_brand custom taxonomy terms
    $brand_post_args = array(
        'post_type' => 'product',
        'posts_per_page' => -1,
        'product_brand' => $term_name
    );

    // Run our WP_Query with the args above
    $brand_post_query = new WP_Query($brand_post_args);

    // If any products (posts using the product custom post type) are found…
    if( $brand_post_query->have_posts() ) {
        while ($brand_post_query->have_posts()) : $brand_post_query->the_post();
            // FOR TESTING ONLY - shows a lot more information including product id, name and tags
            $products_array[] = get_the_id() . ' - ' . get_the_title() . ' - ' . get_the_term_list($brand_post_query->ID, 'product_tag');

            // Populate the array with the tags found for the product
            $products_tags_array[] = get_the_term_list($brand_post_query->ID, 'product_tag');
        endwhile;
    }

    // Count the duplicate values found inside our array
    $count_tag_totals = array_count_values($products_tags_array);

    // FOR TESTING ONLY - Used below to show the product information such as id, name etc
    $show_product_values = array_unique($products_tags_array);

    // Show the tags found for all of the products for the current brand, along with the number found for each tag
    echo '<ul>';    
    foreach($count_tag_totals as $key=>$value) {
        echo '<li>' . $key . ' ('. $value . ')</li>';   
    }
    echo '</ul>';

    // FOR TESTING ONLY - Displays unique tag name(s) along with a total of the products that have that tag
    //echo '<pre><h3>Unique product tag(s) and associated totals for this brand&hellip; </h3>',print_r($count_tag_totals,1),'</pre>';   

    // FOR TESTING ONLY - Displays a list of all products (id and name) along with their tags
    //echo '<pre><h3>The products details for this brand page are&hellip; </h3>',print_r($products_array,1),'</pre>';       

    // Reset the WP_Query for use elsewhere
    wp_reset_query();

}

add_action('woocommerce_archive_description', 'get_wc_product_brand_related_tags', 15);


来源:https://stackoverflow.com/questions/31478603/get-woocommerce-products-tags-for-array-of-products

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!