• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WooCommerce a2z

WooCommerce a2z

WooCommerce

  • Home
  • Plugins
  • Blocks
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / wc_get_related_products() – Get related products based on product category and tags.

You appear to be a bot. Output may be restricted

Description

Get related products based on product category and tags.

Usage

$array = wc_get_related_products( $product_id, $limit, $exclude_ids );

Parameters

$product_id
( int ) required – Product ID.
$limit
( int ) optional default: 5 – Limit of results.
$exclude_ids
( array ) optional – Exclude IDs from the results.

Returns

array

Source

File name: woocommerce/includes/wc-product-functions.php
Lines:

1 to 55 of 55
function wc_get_related_products( $product_id, $limit = 5, $exclude_ids = array() ) {

  $product_id     = absint( $product_id );
  $limit          = $limit >= -1 ? $limit : 5;
  $exclude_ids    = array_merge( array( 0, $product_id ), $exclude_ids );
  $transient_name = 'wc_related_' . $product_id;
  $query_args     = http_build_query(
    array(
      'limit'       => $limit,
      'exclude_ids' => $exclude_ids,
    )
  );

  $transient     = get_transient( $transient_name );
  $related_posts = $transient && isset( $transient[ $query_args ] ) ? $transient[ $query_args ] : false;

  // We want to query related posts if they are not cached, or we don't have enough.
  if ( false === $related_posts || count( $related_posts ) < $limit ) {

    $cats_array = apply_filters( 'woocommerce_product_related_posts_relate_by_category', true, $product_id ) ? apply_filters( 'woocommerce_get_related_product_cat_terms', wc_get_product_term_ids( $product_id, 'product_cat' ), $product_id ) : array();
    $tags_array = apply_filters( 'woocommerce_product_related_posts_relate_by_tag', true, $product_id ) ? apply_filters( 'woocommerce_get_related_product_tag_terms', wc_get_product_term_ids( $product_id, 'product_tag' ), $product_id ) : array();

    // Don't bother if none are set, unless woocommerce_product_related_posts_force_display is set to true in which case all products are related.
    if ( empty( $cats_array ) && empty( $tags_array ) && ! apply_filters( 'woocommerce_product_related_posts_force_display', false, $product_id ) ) {
      $related_posts = array();
    } else {
      $data_store    = WC_Data_Store::load( 'product' );
      $related_posts = $data_store->get_related_products( $cats_array, $tags_array, $exclude_ids, $limit + 10, $product_id );
    }

    if ( $transient ) {
      $transient[ $query_args ] = $related_posts;
    } else {
      $transient = array( $query_args => $related_posts );
    }

    set_transient( $transient_name, $transient, DAY_IN_SECONDS );
  }

  $related_posts = apply_filters(
    'woocommerce_related_products',
    $related_posts,
    $product_id,
    array(
      'limit'        => $limit,
      'excluded_ids' => $exclude_ids,
    )
  );

  if ( apply_filters( 'woocommerce_product_related_posts_shuffle', true ) ) {
    shuffle( $related_posts );
  }

  return array_slice( $related_posts, 0, $limit );
}
 

 View on GitHub View on Trac

Published: 26th November 2019 | Last updated: 8th February 2020

Primary Sidebar

Information

Function name: wc_get_related_products
Plugin ref: WooCommerce
Version: 4.8.0
Sourcefile: includes/wc-product-functions.php
File ref: includes/wc-product-functions.php
Deprecated?: No
API Letters: G,P,R,W

Footer

WooCommerce a2z
WooCommerce a2z
WooCommerce
WordPress 5.6
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  woocommerce.wp-a2z.org
© Copyright WooCommerce a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites