Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions src/BlockTypes/AddToCartForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,28 @@ class AddToCartForm extends AbstractBlock {
* @return string | void Rendered block output.
*/
protected function render( $attributes, $content, $block ) {
global $product;

$post_id = $block->context['postId'];

if ( ! isset( $post_id ) ) {
return '';
}

if ( ! $product instanceof \WC_Product ) {
$product = wc_get_product( $post_id );
if ( ! $product instanceof \WC_Product ) {
return '';
}
$single_product = wc_get_product( $post_id );
if ( ! $single_product instanceof \WC_Product ) {
return '';
}

ob_start();

/**
* Trigger the single product add to cart action for each product type.
*
* @since 9.7.0
*/
do_action( 'woocommerce_' . $product->get_type() . '_add_to_cart' );
do_action( 'woocommerce_' . $single_product->get_type() . '_add_to_cart' );

$product = ob_get_clean();
$single_product = ob_get_clean();

if ( ! $product ) {
if ( ! $single_product ) {
return '';
}

Expand All @@ -63,7 +58,7 @@ protected function render( $attributes, $content, $block ) {
esc_attr( $classes_and_styles['classes'] ),
esc_attr( $classname ),
esc_attr( $classes_and_styles['styles'] ),
$product
$single_product
);
}

Expand Down