-1

I have this code in my child theme functions.php file:

// add hover image to woo category page
    add_action( 'woocommerce_before_shop_loop_item_title', 'mem_add_on_hover_shop_loop_image' ) ; 
        function mem_add_on_hover_shop_loop_image() {
        $image_id = wc_get_product()->get_gallery_image_ids()[0] ; 
        if ( $image_id ) {
            echo wp_get_attachment_image( $image_id, 'woocommerce_thumbnail' ) ; 
        } else { 
            //echo wp_get_attachment_image( wc_get_product()->get_image_id() ) ; 
            echo wp_get_attachment_image( wc_get_product()->get_image_id(), 'woocommerce_thumbnail' ) ; 
        }
    }

It works and it switches the category image on hover.

The problem is that a PHP error is showing related to this line:
$image_id = wc_get_product()->get_gallery_image_ids()[0] ;

The error is PHP Warning: Undefined array key 0

How can I fix this please?

Thank you Tamsin

I have not tried a fix yet.

1 Answers1

0

You can first check if get_gallery_image_ids returns an array. If it does then check key 0 (first element) exists. If it does then you're free to use it.

...

// Get all IDs
$idList = wc_get_product()->get_gallery_image_ids(); 

// Check if the IDs are an array and key 0 (first element) exists
if (is_array($idList) && array_key_exists(0, $idList)) {
    // Get the first element
    $image_id = $idList[0];

    echo wp_get_attachment_image($image_id, 'woocommerce_thumbnail' ) ; 
} else { 
    echo wp_get_attachment_image(wc_get_product()->get_image_id(), 'woocommerce_thumbnail' ) ; 
}

Edit,

Should I add it to the functions file

You should edit your mem_add_on_hover_shop_loop_image function with this code. Final code should look like this,

add_action('woocommerce_before_shop_loop_item_title', 'mem_add_on_hover_shop_loop_image');
function mem_add_on_hover_shop_loop_image()
{
    // Get all IDs
    $idList = wc_get_product()->get_gallery_image_ids();

    // Check if the IDs are an array and key 0 (first element) exists
    if (is_array($idList) && array_key_exists(0, $idList)) {
        // Get the first element
        $image_id = $idList[0];

        echo wp_get_attachment_image($image_id, 'woocommerce_thumbnail');
    } else {
        echo wp_get_attachment_image(wc_get_product()->get_image_id(), 'woocommerce_thumbnail');
    }
}
waterloomatt
  • 3,662
  • 1
  • 19
  • 25
  • Thank you for your answer. I am sorry not to understand what to do with this code. Should I add it to the functions file? – Tamsin Carter Mar 20 '23 at 13:59
  • I did try and it wouldn't save and showed this: – Tamsin Carter Mar 20 '23 at 13:59
  • Your PHP code changes were rolled back due to an error on line 360 of file wp-content/themes/astra-child/functions.php. Please fix and try saving again. Uncaught Error: Call to a member function get_gallery_image_ids() on bool in wp-content/themes/astra-child/functions.php:360 Stack trace: #0 wp-settings.php(585): include() #1 wp-config.php(82): require_once('/www/xxxxxxxx...') #2 wp-load.php(50): require_once('/www/xxxxxxxx...') #3 wp-admin/admin.php(34): require_once('/www/xxxxxx..') #4 wp-admin/theme-editor.php(10): require_once('/www/xxxxxxx...') #5 {main} thrown – Tamsin Carter Mar 20 '23 at 13:59
  • See edit above. – waterloomatt Mar 20 '23 at 14:06
  • Brilliant, thank you. That code seems to work and I assume the Undefined key error is resolved now. Thank you so much for your help. – Tamsin Carter Mar 20 '23 at 14:10
  • @water please close all newly asked questions seeking advice on how to check if an array key exists. This topic is well covered on Stack Overflow. – mickmackusa Mar 20 '23 at 23:49
  • Will do next time. – waterloomatt Mar 21 '23 at 00:43