R. GOKSU
R. GOKSU

Reputation: 13

How to programmatically set the featured image of the product based on the image of the product category to which it belongs

I have many products but the pictures will actually be category pictures. I want the product featured images to be automatically the category image they are in.

There is a problem with the code below.

function default_category_featured_image() {
global $post;
$featured_image_exists = has_post_thumbnail($post->ID);
              
if (!$featured_image_exists)  {
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );

if ($attached_image) {
                          
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment);
}}
else if ( in_category('61') ) {
    set_post_thumbnail($post->ID, '4436'); //arçelik
}
else if ( in_category('64') ) {
    set_post_thumbnail($post->ID, '4435'); //beko
}
else if ( in_category('59') ) {
    set_post_thumbnail($post->ID, '4434'); //LG
}
else if ( in_category('60') ) {
    set_post_thumbnail($post->ID, '4458'); //Philips
    }
else if ( in_category('66') ) {
    set_post_thumbnail($post->ID, '4160'); //Profilo
        }
else if ( in_category('58') ) {
    set_post_thumbnail($post->ID, '4159'); //samsung
            }
else if ( in_category('65') ) {
    set_post_thumbnail($post->ID, '4160'); //seg
                        }
else if ( in_category('62') ) {
    set_post_thumbnail($post->ID, '4433'); //sony
                                    }
else if ( in_category('63') ) {
    set_post_thumbnail($post->ID, '4160'); //vestel
                                                }
else {
set_post_thumbnail($post->ID, '4435'); //genel
wp_reset_postdata();
                               }
                               
                           }
                        
      }
add_action('the_post', 'default_category_featured_image');

Upvotes: 1

Views: 2484

Answers (1)

Vincenzo Di Gaetano
Vincenzo Di Gaetano

Reputation: 4110

You can set product thumbnails using the init hook.

Make sure you remove the programmatically_set_thumbnail_to_product function from the functions.php file once it's done.

DATA TO BE REPLACED:

  • Enter the product category id "Genel" in place of the genel key of the $thumbnail_ids array.

Add it to your active theme's functions.php.

// updates the thumbnails of all products based on the product category image
add_action( 'init', 'programmatically_set_thumbnail_to_product' );
function programmatically_set_thumbnail_to_product() {

    // returns the ids of all products
    $ids = wc_get_products(
        array(
            'return' => 'ids',
            'limit' => -1
        )
    );

    // set the ids of the product categories
    $thumbnail_ids = array(
        58      => 4159, // Samsung
        59      => 4434, // LG
        60      => 4458, // Philips
        61      => 4436, // Arçelik
        62      => 4433, // Sony
        63      => 4160, // Vestel
        64      => 4435, // Beko
        65      => 4160, // Seg
        66      => 4160, // Profilo
        'genel' => 4435  // Genel (set the id of the product category "Genel" as a key)
    );

    // for each product
    foreach ( $ids as $product_id ) {
        // for each product category
        foreach ( $thumbnail_ids as $cat_id => $thumbnail_id ) {
            // check if the product belongs to a product category
            if ( has_term( $cat_id, 'product_cat', $product_id ) ) {
                // if yes, set the product thumbnail
                set_post_thumbnail( $product_id, $thumbnail_id);
                break;
            }
        }
    }

}

This function will set the thumbnails of all products based on the thumbnail of the respective product category.

Once added to functions.php refresh the product catalog page in the frontend (for example) and you will notice that all images will be changed/set.

Then remove it from functions.php and add this other function.

The function below will be performed every time a product is saved or updated (to automatically set the image of new products).

DATA TO BE REPLACED:

  • Enter the product category id "Genel" in place of the genel key of the $thumbnail_ids array.

Always add it to functions.php.

// when a product is saved or updated it sets the image based on the product category
add_action( 'save_post', 'programmatically_set_thumbnail_to_new_product' );
function programmatically_set_thumbnail_to_new_product( $product_id ) {

    // set the ids of the product categories
    $thumbnail_ids = array(
        58      => 4159, // Samsung
        59      => 4434, // LG
        60      => 4458, // Philips
        61      => 4436, // Arçelik
        62      => 4433, // Sony
        63      => 4160, // Vestel
        64      => 4435, // Beko
        65      => 4160, // Seg
        66      => 4160, // Profilo
        'genel' => 4435  // Genel (set the id of the product category "Genel" as a key)
    );
    
    // for each product category
    foreach ( $thumbnail_ids as $cat_id => $thumbnail_id ) {
        // check if the product belongs to a product category
        if ( has_term( $cat_id, 'product_cat', $product_id ) ) {
            // retrieve product thumbnail id
            $current_thumbnail_id = get_post_thumbnail_id( $product_id );
            // if it is different from that of the product category
            if ( $current_thumbnail_id == false || $current_thumbnail_id != $thumbnail_id ) {
                // set the product thumbnail
                set_post_thumbnail( $product_id, $thumbnail_id );
                break;
            }
        }
    }
}

Both codes have been tested and work.

Upvotes: 1

Related Questions