From a8becb24ba8edf85363b41c9f38532992f28cc5f Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Tue, 19 Jul 2016 08:21:06 -0300 Subject: [PATCH] Check if product exists to avoid fatal errors on emails order schema markup, closes #11459 --- includes/class-wc-emails.php | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/includes/class-wc-emails.php b/includes/class-wc-emails.php index edbedef061b..49c78becfa3 100644 --- a/includes/class-wc-emails.php +++ b/includes/class-wc-emails.php @@ -289,16 +289,23 @@ class WC_Emails { continue; } - $product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item ); - $is_visible = $product && $product->is_visible(); + $product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item ); + $product_exists = is_object( $product ); + $is_visible = $product_exists && $product->is_visible(); $item_offered = array( '@type' => 'Product', - 'name' => apply_filters( 'woocommerce_order_item_name', $item['name'], $item, $is_visible ) + 'name' => apply_filters( 'woocommerce_order_item_name', $item['name'], $item, $is_visible ), ); - if ( $sku = $product->get_sku() ) { - $item_offered['sku'] = $sku; + if ( $product_exists ) { + if ( $sku = $product->get_sku() ) { + $item_offered['sku'] = $sku; + } + + if ( $image_id = $product->get_image_id() ) { + $item_offered['image'] = wp_get_attachment_image_url( $image_id, 'thumbnail' ); + } } if ( $is_visible ) { @@ -307,10 +314,6 @@ class WC_Emails { $item_offered['url'] = get_home_url(); } - if ( $image_id = $product->get_image_id() ) { - $item_offered['image'] = wp_get_attachment_image_url( $image_id, 'thumbnail' ); - } - $accepted_offer = (object) array( '@type' => 'Offer', 'itemOffered' => $item_offered, @@ -320,7 +323,7 @@ class WC_Emails { '@type' => 'QuantitativeValue', 'value' => apply_filters( 'woocommerce_email_order_item_quantity', $item['qty'], $item ) ), - 'url' => get_home_url(), + 'url' => get_home_url(), ); $accepted_offers[] = $accepted_offer;