Fatal error when using Product Stock Indicator or On Sale Badge blocks on a homepage (#45135)

* Prevent accessing product if there's no product yet in ProductStockIndicator block

* Prevent accessing product if there's no product yet in ProductSaleBadge block

* Add changelog

* Fix linter
This commit is contained in:
Karol Manijak 2024-02-28 10:34:37 +01:00 committed by GitHub
parent 382bcdf511
commit d833e92f05
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 4 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Products Stock Indicator and Sale Badge: prevent fatal when block were used in a homepage

View File

@ -97,8 +97,13 @@ class ProductSaleBadge extends AbstractBlock {
return $content;
}
$post_id = $block->context['postId'];
$product = wc_get_product( $post_id );
$post_id = $block->context['postId'];
$product = wc_get_product( $post_id );
if ( ! $product ) {
return null;
}
$is_on_sale = $product->is_on_sale();
if ( ! $is_on_sale ) {

View File

@ -84,8 +84,13 @@ class ProductStockIndicator extends AbstractBlock {
return $content;
}
$post_id = $block->context['postId'];
$product = wc_get_product( $post_id );
$post_id = $block->context['postId'];
$product = wc_get_product( $post_id );
if ( ! $product ) {
return '';
}
$is_in_stock = $product->is_in_stock();
$is_on_backorder = $product->is_on_backorder();