Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 62697ad

Browse files
authored
Related Products: Fix hardcoded limit product (#9407)
* fix hardcoded limit product * improve readibility
1 parent 344f5b4 commit 62697ad

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

src/BlockTypes/RelatedProducts.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function build_query( $query ) {
8787
return $query;
8888
}
8989

90-
$related_products_ids = $this->get_related_products_ids();
90+
$related_products_ids = $this->get_related_products_ids( $query['posts_per_page'] );
9191
if ( count( $related_products_ids ) < 1 ) {
9292
return array();
9393
}
@@ -113,11 +113,12 @@ public function render_block( string $content, array $block ) {
113113
return $content;
114114
}
115115

116+
// If there are no related products, render nothing.
116117
$related_products_ids = $this->get_related_products_ids();
117-
118118
if ( count( $related_products_ids ) < 1 ) {
119119
return '';
120120
}
121+
121122
return $content;
122123
}
123124

@@ -142,14 +143,15 @@ private function is_related_products_block( $block ) {
142143
* Get related products ids.
143144
* The logic is copied from the core function woocommerce_related_products. https://github.com/woocommerce/woocommerce/blob/ca49caabcba84ce9f60a03c6d3534ec14b350b80/plugins/woocommerce/includes/wc-template-functions.php/#L2039-L2074
144145
*
146+
* @param number $product_per_page Products per page.
145147
* @return array Products ids.
146148
*/
147-
private function get_related_products_ids() {
149+
private function get_related_products_ids( $product_per_page = 5 ) {
148150
global $post;
149151

150152
$product = wc_get_product( $post->ID );
151153

152-
$related_products = array_filter( array_map( 'wc_get_product', wc_get_related_products( $product->get_id(), 5, $product->get_upsell_ids() ) ), 'wc_products_array_filter_visible' );
154+
$related_products = array_filter( array_map( 'wc_get_product', wc_get_related_products( $product->get_id(), $product_per_page, $product->get_upsell_ids() ) ), 'wc_products_array_filter_visible' );
153155
$related_products = wc_products_array_orderby( $related_products, 'rand', 'desc' );
154156

155157
$related_product_ids = array_map(

0 commit comments

Comments
 (0)