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

Commit

Permalink
fix: related products and grouped products result #8589
Browse files Browse the repository at this point in the history
  • Loading branch information
gigitux committed Mar 7, 2023
1 parent 50f6500 commit 6c84ed7
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 6 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
80 changes: 74 additions & 6 deletions src/BlockTypes/RelatedProducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ protected function initialize() {
2
);

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

}

/**
Expand Down Expand Up @@ -70,8 +77,6 @@ public function update_query( $pre_render, $parsed_block ) {
}
}



/**
* Return a custom query based on attributes, filters and global WP_Query.
*
Expand All @@ -80,18 +85,81 @@ 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',
);
}

/**
* Render the block.
* 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 $product;

$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 6c84ed7

Please sign in to comment.