1

I wrote my own shortcode for woocommerce.

The code works fine on content-single-product.php. Any other page though, and my site breaks. Why?

Fatal error: Call to a member function get_gallery_image_ids() on null in /mysite/wp-content/plugins/woocommerce/templates/single-product/product-thumbnails.php on line 27

This is the function i wrote:

/* My custom shortcode code*/
function get_woocommerce_gallery_image_thumbnails ( $atts ) {

     // Buffer our contents
     ob_start();

     /* Line to get the gallery thumbnails */
wc_get_template( 'single-product/product-thumbnails.php');

     // Return buffered contents
     return ob_get_clean();
}

add_shortcode( 'wc_get_thumbs', 'get_woocommerce_gallery_image_thumbnails' );
WillingLearner
  • 7,106
  • 6
  • 34
  • 51
  • 1
    There is a missing object which holds the `get_gallery_image_ids()` method. – Tyr Feb 21 '19 at 21:37
  • 1
    lets say `$foo->get_gallery_image_ids()` is the function then `$foo` is null – Scuzzy Feb 21 '19 at 21:38
  • im still not understanding. Why would the code work perfectly fine on my product page, but not fine everywhere else? What exactly do i write and where would i write it to overcome the error? – WillingLearner Feb 21 '19 at 21:55

1 Answers1

0

According to the given information, every page asks for *something*->get_gallery_image_ids(), but *something* only exists on your product page. Looking at the available data, this *something* is presumably created in the product-thumbnails template.

With the information from this answer, we can see that the *something* is actually a global $product, which is required for the product-thumbnails.

The best solution, I suppose, is to remove product-thumbnails from every page that is not a product page.

As an alternative, you could add a safeguard in your shortcode definition:

function get_woocommerce_gallery_image_thumbnails ( $atts ) {
     global $product;
     if (!isset($product)) {
         return;
     }

     // Buffer our contents
     ob_start();

     /* Line to get the gallery thumbnails */
     wc_get_template( 'single-product/product-thumbnails.php');

     // Return buffered contents
     return ob_get_clean();
}
Stratadox
  • 1,291
  • 8
  • 21
  • Ooops you are right, I didn't notice `$product` was already global in `product-thumbnails.php`. In that case my answer won't work, I deleted it. Good catch! – Don't Panic Feb 21 '19 at 22:13
  • Heh, I don't think it was necessary to delete it.. I wouldn't have guessed that the something I'd been describing was a `global $product` if it hadn't been for your answer. – Stratadox Feb 21 '19 at 22:17
  • I mean...i don't know if that will work either. i first tried it on a page that had nothing related to products, and it still broke. So i don't know what's the fix here – WillingLearner Feb 21 '19 at 22:23
  • 1
    Well, that's exactly the point: because you want to get the product thumbnails on a page that has nothing to do with products, wordpress breaks down. As for the fix, refer to the last paragraph of the answer. – Stratadox Feb 21 '19 at 22:28
  • Ah nvm, I just thought of an alternative. I'll update the answer. – Stratadox Feb 21 '19 at 22:30
  • The best thing is still to not put the tag for the thumbnails of a single product on every page, even those that have "nothing related to products". – Stratadox Feb 21 '19 at 22:35