Automattic\WooCommerce\Blocks\BlockTypes

ProductButton::render()protectedWC 1.0

Include and render the block

{} Это метод класса: ProductButton{}

Хуки из метода

Возвращает

Строку. Rendered block type output.

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->render( $attributes, $content, $block );
$attributes(массив) (обязательный)
Block attributes.
По умолчанию: empty array
$content(строка) (обязательный)
Block content.
По умолчанию: empty string
$block(WP_Block) (обязательный)
Block instance.

Код ProductButton::render() WC 7.1.0

protected function render( $attributes, $content, $block ) {
	if ( ! empty( $content ) ) {
		parent::register_block_type_assets();
		$this->register_chunk_translations( [ $this->block_name ] );
		return $content;
	}

	$post_id = $block->context['postId'];
	$product = wc_get_product( $post_id );

	if ( $product ) {
		$cart_redirect_after_add = get_option( 'woocommerce_cart_redirect_after_add' ) === 'yes';
		$html_element            = ( ! $product->has_options() && $product->is_purchasable() && $product->is_in_stock() && ! $cart_redirect_after_add ) ? 'button' : 'a';
		$styles_and_classes      = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes, array( 'border_radius', 'font_size', 'text_color' ) );

		return apply_filters(
			'woocommerce_loop_add_to_cart_link',
			sprintf(
				'<div class="wp-block-button wc-block-components-product-button wc-block-grid__product-add-to-cart">
				<%s href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="wp-block-button__link %s wc-block-components-product-button__button product_type_%s %s" style="%s">%s</%s>
			</div>',
				$html_element,
				esc_url( $product->add_to_cart_url() ),
				esc_attr( $product->get_id() ),
				esc_attr( $product->get_sku() ),
				$product->is_purchasable() ? 'ajax_add_to_cart add_to_cart_button' : '',
				esc_attr( $product->get_type() ),
				$styles_and_classes['classes'],
				$styles_and_classes['styles'],
				esc_html( $product->add_to_cart_text() ),
				$html_element
			),
			$product
		);
	}
}