Getting the 'thumbnail ID' from last product category term in WooCommerce

zeynab farzaneh

I am trying to add new portfolio pragmatically When I add new category in WooCommerce.

My code is :

function programmatically_create_post() {

$author_id = 1;
$taxonomy     = 'product_cat';
$orderby      = 'id';
$show_count   = 0;      // 1 for yes, 0 for no
$pad_counts   = 0;      // 1 for yes, 0 for no
$hierarchical = 1;      // 1 for yes, 0 for no
$title        = '';
$empty        = 0;

$args = array(
    'taxonomy'     => $taxonomy,
    'orderby'      => $orderby,
    'show_count'   => $show_count,
    'pad_counts'   => $pad_counts,
    'hierarchical' => $hierarchical,
    'title_li'     => $title,
    'hide_empty'   => $empty
);
$all_categories = get_categories( $args );
$lastCategory=end($all_categories);
$slug =$lastCategory->slug;
$title=$lastCategory->name;
$thumbnail_id= get_post_thumbnail_id($lastCategory->id );

// If the page doesn't already exist, then create it

if( null == get_page_by_title( $title ) ) {

// Set the post ID so that we know the post was created successfully

    $post_id = wp_insert_post(

        array(

            'post_author'   => $author_id,
            'post_name'   => $slug,
            'post_title'    => $title,
            'post_status'   => 'publish',
            'post_type'   => 'us_portfolio',
            'post_parent' =>11,
            'page_template' =>'custumcat.php',
            'post_slug'=> $slug

        )

    );


    update_post_meta($post_id, '_wp_page_template', 'custumcat.php' );
    update_post_meta( $post_id, '_thumbnail_id', $thumbnail_id );

// Otherwise, we'll stop

} else {

    // Arbitrarily use -2 to indicate that the page with the title already exists

    $post_id = -2;

} // end if
} // end programmatically_create_post


add_action('create_product_cat', 'programmatically_create_post', 10,2);

I want set portfolio thumbnail from category thumbnail, and I have used $thumbnail_id= get_post_thumbnail_id($lastCategory->id );for get category thumbnail.

After that I used update_post_meta( $post_id, '_thumbnail_id', $thumbnail_id ); to set portfolio's thumbnail.

But it doesn't set anything.

How can I fix it?

LoicTheAztec

Update 2.1

I have been testing the code below and I got the correct $thumbnail_id with no errors:

$categories = get_categories( array(
    'taxonomy'     => 'product_cat',
    'orderby'      => 'id',
    'show_count'   => 0,
    'pad_counts'   => 0,
    'hierarchical' => 1,
    'title_li'     => '',
    'hide_empty'   => 0
) );

$last_cat = end($categories); // last category

$last_cat_term_id = $last_cat->term_id; // Value is

$thumbnail_id = get_woocommerce_term_meta( $last_cat_term_id, 'thumbnail_id', true );
echo 'Term ID is: ' . $last_cat_term_id . '<br>';
echo 'Thumbnail ID is: ' . $thumbnail_id;

It displays the last category (with this data related to my product categories settings):

Term ID is: 48
Thumbnail ID is: 443

And here the corresponding screenshot of the DB table "wp_termmeta":

// Displays 443 (the correct 'thumbnail_id'set in DB table "wp_termmeta")

So this is tested and works.

This time, update_post_meta( $post_id, '_thumbnail_id', $thumbnail_id ); will set correctly a value.


Update 1:

Product categories are a WordPress custom taxonomy that uses WP_terms

This doesn't work because $lastCategory->id doesn't exist (and output a null value):

$thumbnail_id= get_post_thumbnail_id($lastCategory->id );

Instead you need to use $lastCategory->term_id that will work with WP_Term object and get_woocommerce_term_meta() this way:

$thumbnail_id= get_woocommerce_term_meta( $lastCategory->term_id, 'thumbnail_id', true );

The WP_Term object properties are:

term_id 
name
slug
term_group
term_taxonomy_id
taxonomy
description 
parent
count

Related to product category term: WooCommerce get attribute thumbnail - Variation Swatches and Photos plugin

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get a product from specific product attribute term name in WooCommerce

Get a list of siblings term ids from current product category in WooCommerce

Get "primary" category image from WooCommerce product

Display products from specific product category in Woocommerce

Get the parent product categories ids from a sub product category ID in Woocommerce

Exclude a product category from the loop in Woocommerce

Get all Woocommerce products from current product category term Id in a WP_Query

Add the term slug as a class to the product category div tag in Woocommerce

Display a custom thumbnail for a specific product ID in Woocommerce cart

Set a product category term in a product on Woocommerce

Check a product category for a product in Woocommerce

Exclude a product category from Woocommerce related products

Exclude a product category from Related products in WooCommerce

If product category has children remove permalink from parent term in WooCommerce

Get the category name from a WooCommerce product using the product id or the category id

Get also WooCommerce product category thumbnail Id using a SQL query

Woocommerce: Product Category Widget

Get only one product category term for a WooCommerce product

WooCommerce: function that returns all product ID's in a particular category

woocommerce get product category id

Woocommerce - remove product from category

Woocommerce Product child category from current parent category

Woocommerce - add last category name to product name

Get WooCommerce attribute thumbnail URL by attribute ID (WooCommerce Product Search plugin)

SQL Fetching Woocommerce Product Thumbnail From DB

Woocommerce display product category thumbnail above product thumbnail

Woocommerce is setting the Term ID rather than the Term name on Product Attribute

WooCommerce database query : Get product category image path+name from thumbnail_id

How to Improve WooCommerce Product Category Thumbnail Quality?