Schema markup fatal error
-
Hi Scott,
The function added in the latest version (0.5.0) throws a fatal error when it is called on pages that are not product single pages, the functions try to get the
global $product
variable which is not available in those contexts.Below is the corrected function, I hope to see an update soon :). The changes were:
– Added function access keyword to
public
– Added documentation to the function
– Added the parameter$product
which is passed by WooCommerce
– Added a double safe check to return an unchanged$markup
if the product variable is not available for some reason, which is unlikely to happen as WooCommerce will only call this function if the$product
variable exists.
– Removed theglobal $product
declaration that was getting the product variable from the global context, hence why the error happened on not-product pages./** * Adds GTIN field to product JSON-LD. * * @since 0.5.0 * @return array Modified WooCommerce product structured data. */ public function add_gtin_to_structured_data( $markup, $product ) { // Bail if product variable not available if ( ! $product ) { return $markup; } $product_id = $product->get_id(); $gtin = get_post_meta( $product_id, 'hwp_product_gtin', 1 ); $markup['gtin'] = trim($gtin); return $markup; }
Best,
Diego
- The topic ‘Schema markup fatal error’ is closed to new replies.