woocommerce_related_products

Output the related products.

Description

woocommerce_related_products( (array) $args = array() ); 

Parameters (1)

0. $args — Optional. (array) => array()
Provided arguments

Usage

  1. if ( !function_exists( 'woocommerce_related_products' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-template-functions.php'; 
  3.  
  4. // Provided arguments 
  5. $args = array(); 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = woocommerce_related_products($args); 
  9.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-template-functions.php  
  1. function woocommerce_related_products( $args = array() ) { 
  2. global $product, $woocommerce_loop; 
  3.  
  4. if ( ! $product ) { 
  5. return; 
  6.  
  7. $defaults = array( 
  8. 'posts_per_page' => 2,  
  9. 'columns' => 2,  
  10. 'orderby' => 'rand',  
  11. 'order' => 'desc',  
  12. ); 
  13.  
  14. $args = wp_parse_args( $args, $defaults ); 
  15.  
  16. // Get visble related products then sort them at random. 
  17. $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' ); 
  18.  
  19. // Handle orderby. 
  20. $args['related_products'] = wc_products_array_orderby( $args['related_products'], $args['orderby'], $args['order'] ); 
  21.  
  22. // Set global loop values. 
  23. $woocommerce_loop['name'] = 'related'; 
  24. $woocommerce_loop['columns'] = apply_filters( 'woocommerce_related_products_columns', $args['columns'] ); 
  25.  
  26. wc_get_template( 'single-product/related.php', $args );