Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove featured image from the WooCommerce gallery

I tried using suggestions from other posts but they do not work. I am looking to not show the featured product image in my product images area/image gallery because I am using the featured image in the header as a background image and its too wide to be in the gallery.

So far this is the closest thing to working but I get an error. It does however do what I need.

Any way to fix this so i do not get the error?

Here is my code:

add_filter('woocommerce_single_product_image_thumbnail_html', 'remove_featured_image', 10, 3);
function remove_featured_image($html, $attachment_id, $post_id) {
    $featured_image = get_post_thumbnail_id($post_id);
    if ($attachment_id != $featured_image) {
        return $html;
    }
    return '';
}

And here is the error:

Missing argument 3 for remove_featured_image() in /home/…/plugins/my-custom-functions/inc/php/functional.php(93) : eval()'d code on line 4

Warning: Missing argument 3 for remove_featured_image() in /home…/plugins/my-custom-functions/inc/php/functional.php(93) : eval()'d code on line 4

like image 327
Jp Schultz Avatar asked Jun 24 '17 04:06

Jp Schultz


2 Answers

There is only 2 arguments for woocommerce_single_product_image_thumbnail_html filter hook.

So you have to change a little bit your code to avoid the error, this way:

add_filter('woocommerce_single_product_image_thumbnail_html', 'remove_featured_image', 10, 2);
function remove_featured_image($html, $attachment_id ) {
    global $post, $product;

    $featured_image = get_post_thumbnail_id( $post->ID );

    if ( $attachment_id == $featured_image )
        $html = '';

    return $html;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.


References for filter hook woocommerce_single_product_image_thumbnail_html:

  • woocommerce templates: single-product/product-image.php
  • woocommerce templates: single-product/product-thumbnails.php
like image 58
LoicTheAztec Avatar answered Nov 03 '22 14:11

LoicTheAztec


This is what I did, hope it's helpful. I used the Meta Box plugin to create a custom checkbox in the product edit page (in the Wordpress backend). The checkbox is basically "Hide featured image from product gallery" and here's the code (to be placed in the functions.php file):

function hide_first_img( $meta_boxes ) {

$prefix = 'meta_box';

$meta_boxes[] = array(
    'id' => 'hide_img',
    'title' => esc_html__( 'Hide first image', 'custom_meta_boxes' ),
    'post_types' => array('product'),
    'context' => 'advanced',
    'priority' => 'default',
    'autosave' => 'false',
    'fields' => array(
        array(
            'id' => $prefix . '_hide_first_image',
            'name' => esc_html__( 'Hide first image in product gallery', 'custom_meta_boxes' ),
            'type' => 'checkbox',
            'desc' => esc_html__( 'Check to hide the first image in the gallery', 'custom_meta_boxes' ),
        ),
    ),
);

return $meta_boxes;
}
add_filter( 'rwmb_meta_boxes', 'hide_first_img' );

Then I used LoicTheAztec's code, which works great, to remove the image ONLY if the checkbox is checked, like this (to be placed in the functions.php file):

function remove_featured_image($html, $attachment_id ) {

    if( rwmb_meta( 'meta_box_hide_first_image' )){
 
        global $post, $product;

        $featured_image = get_post_thumbnail_id( $post->ID );

        if ( $attachment_id == $featured_image )
            $html = '';

    }

    return $html;
}
add_filter('woocommerce_single_product_image_thumbnail_html', 'remove_featured_image', 10, 2);

Finally, only for FLATSOME THEME users, to also hide the first thumbnail in the gallery, I directly edited the following file in my child theme folder: \flatsome-child\woocommerce\single-product\product-gallery-thumbnails.php

Maybe there's some similar file for the default Woocomerce gallery or for other themes:

// show first thumbnail only if meta box is NOT checked
<?php if( !rwmb_meta( 'meta_box_hide_first_image' )) : ?>

    <div class="col is-nav-selected first">
      <a>
        <?php
          $image_id = get_post_thumbnail_id($post->ID);
          $image =  wp_get_attachment_image_src( $image_id, apply_filters( 'woocommerce_gallery_thumbnail_size', 'woocommerce_'.$image_size ) );
        $image_alt = get_post_meta( $image_id, '_wp_attachment_image_alt', true );
          $image = '<img src="'.$image[0].'" alt="'.$image_alt.'" width="'.$gallery_thumbnail['width'].'" height="'.$gallery_thumbnail['height'].'" class="attachment-woocommerce_thumbnail" />';

          echo $image;
        ?>
      </a>
    </div>

  <?php endif; ?>

I can confirm that this is working live on my site right now.

like image 21
Matteo Padovan Avatar answered Nov 03 '22 16:11

Matteo Padovan