Skip to content
Advertisement

Uncaught Error: Call to a member function get_attributes() on null

Imports several thousand thumbnail images into existing products. For this purpose, I used the attribute that each product “featured-images” has, in which there is a link to the product.

function alterImageSRC($image, $attachment_id, $size, $icon){ 
    global $product;
    $optimex_featured = $product->get_attribute( 'pa_featured-images' );
    $image[0] = "https://website.com/wp-content/uploads/thumbs/$optimex_featured";
    return $image;
}
add_filter('wp_get_attachment_image_src', 'alterImageSRC', 10, 4);

The code works fine and sets the thumbnails, but when I log into the admin system I get an error:

Uncaught Error: Call to a member function get_attributes() on null.

what’s going on? What am I doing wrong?

Advertisement

Answer

I can’t see a call to get_attributes() in your code, which is what your error is referring to, however, if that is a typo then the error is referring to the use of $product->get_attribute(...) – in this instance your $product is apparently null. You could account for null values by doing something like the following:

function alterImageSRC($image, $attachment_id, $size, $icon){ 
    global $product;
    $url = 'https://some.default/placeholder/image_url';

    if (!is_null($product)) {
        $optimex_featured = $product->get_attribute( 'pa_featured-images' );
        $url = "https://website.com/wp-content/uploads/thumbs/$optimex_featured";
    }

    $image[0] = $url;
    return $image;
}
add_filter('wp_get_attachment_image_src', 'alterImageSRC', 10, 4);
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement