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

Commit

Permalink
Fix the related products block result when used along a grouped produ…
Browse files Browse the repository at this point in the history
…ct (#8656)

* fix: related products and grouped products result #8589

* fix crash
  • Loading branch information
gigitux authored Mar 14, 2023
1 parent 86595f4 commit 87a2d7f
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ export const BLOCK_ATTRIBUTES = {
};

export const INNER_BLOCKS_TEMPLATE: InnerBlockTemplate[] = [
[
'core/heading',
{
level: 2,
content: __( 'Related products.', 'woo-gutenberg-products-block' ),
},
],
[
'core/post-template',
{ __woocommerceNamespace: PRODUCT_TEMPLATE_ID },
Expand Down
82 changes: 78 additions & 4 deletions src/BlockTypes/RelatedProducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ protected function initialize() {
10,
2
);

add_filter(
'render_block',
array( $this, 'render_block' ),
10,
2
);

}

/**
Expand Down Expand Up @@ -75,16 +83,82 @@ public function update_query( $pre_render, $parsed_block ) {
*/
public function build_query( $query ) {
$parsed_block = $this->parsed_block;
if ( ! ProductQuery::is_woocommerce_variation( $parsed_block ) && 'woocommerce/related-products' !== $parsed_block['attrs']['namespace'] ) {
if ( ! $this->is_related_products_block( $parsed_block ) ) {
return $query;
}
$product = wc_get_product();
$related_products = wc_get_related_products( $product->get_id() );

$related_products_ids = $this->get_related_products_ids();
if ( count( $related_products_ids ) < 1 ) {
return array();
}

return array(
'post_type' => 'product',
'post__in' => $related_products,
'post__in' => $related_products_ids,
'post_status' => 'publish',
);
}

/**
* If there are no related products, return an empty string.
*
* @param string $content The block content.
* @param array $block The block.
*
* @return string The block content.
*/
public function render_block( string $content, array $block ) {
if ( ! $this->is_related_products_block( $block ) ) {
return $content;
}

$related_products_ids = $this->get_related_products_ids();

if ( count( $related_products_ids ) < 1 ) {
return '';
}
return $content;
}



/**
* Determines whether the block is a related products block.
*
* @param array $block The block.
*
* @return bool Whether the block is a related products block.
*/
private function is_related_products_block( $block ) {
if ( ProductQuery::is_woocommerce_variation( $block ) && isset( $block['attrs']['namespace'] ) && 'woocommerce/related-products' === $block['attrs']['namespace'] ) {
return true;
}

return false;
}

/**
* Get related products ids.
* 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
*
* @return array Products ids.
*/
private function get_related_products_ids() {
global $post;

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

$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' );
$related_products = wc_products_array_orderby( $related_products, 'rand', 'desc' );

$related_product_ids = array_map(
function( $product ) {
return $product->get_id();
},
$related_products
);

return $related_product_ids;
}

}

0 comments on commit 87a2d7f

Please sign in to comment.