I’m using the code below in my theme’s functions.php file to be able to output the Product SKU in various places all over my templates with [woo_sku]
shortcode. While this works fine, I can’t access the template editor (Elementor) without uncommenting the function, as there is no product context in the template and the error prevents the editor from loading.
Is there a way to catch the error and return an empty value when not in a product context?
function display_woo_sku() {
global $product;
return $product->get_sku();
}
add_shortcode( 'woo_sku', 'display_woo_sku' );
2
Answers
I tried the below code now, it appears to work (though Loic's code above seemed to make more sense, imho :)
Ah, @LoicTheAztec, there was just a typo in your solution:
method_exists
with an s:Thanx a lot!