diff --git a/includes/wc-template-functions.php b/includes/wc-template-functions.php index a746fef3e07..2b2e52323fb 100644 --- a/includes/wc-template-functions.php +++ b/includes/wc-template-functions.php @@ -1269,6 +1269,10 @@ if ( ! function_exists( 'woocommerce_related_products' ) ) { function woocommerce_related_products( $args = array() ) { global $product, $woocommerce_loop; + if ( ! $product ) { + return; + } + $defaults = array( 'posts_per_page' => 2, 'columns' => 2, @@ -1278,10 +1282,6 @@ if ( ! function_exists( 'woocommerce_related_products' ) ) { $args = wp_parse_args( $args, $defaults ); - if ( ! $product ) { - return; - } - // Get visble related products then sort them at random. $args['related_products'] = array_filter( array_map( 'wc_get_product', wc_get_related_products( $product->get_id(), $args['posts_per_page'], $product->get_upsell_ids() ) ), 'wc_products_array_filter_visible' ); @@ -1309,6 +1309,10 @@ if ( ! function_exists( 'woocommerce_upsell_display' ) ) { function woocommerce_upsell_display( $limit = '-1', $columns = 4, $orderby = 'rand', $order = 'desc' ) { global $product, $woocommerce_loop; + if ( ! $product ) { + return; + } + // Handle the legacy filter which controlled posts per page etc. $args = apply_filters( 'woocommerce_upsell_display_args', array( 'posts_per_page' => $limit,