Automattic\WooCommerce\Blocks\BlockTypes

AddToCartForm{}WC 1.0

CatalogSorting class.

Хуки из класса

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

$AddToCartForm = new AddToCartForm();
// use class methods

Методы

  1. protected get_block_type_script( $key = null )
  2. protected register_block_type_assets()
  3. protected render( $attributes, $content, $block )

Код AddToCartForm{} WC 7.7.2

class AddToCartForm extends AbstractBlock {
	/**
	 * Block name.
	 *
	 * @var string
	 */
	protected $block_name = 'add-to-cart-form';

	/**
	 * Render the block.
	 *
	 * @param array    $attributes Block attributes.
	 * @param string   $content Block content.
	 * @param WP_Block $block Block instance.
	 *
	 * @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 '';
			}
		}

		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' );

		$product = ob_get_clean();

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

		$classname          = $attributes['className'] ?? '';
		$classes_and_styles = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes );

		return sprintf(
			'<div class="wp-block-add-to-cart-form %1$s %2$s" style="%3$s">%4$s</div>',
			esc_attr( $classes_and_styles['classes'] ),
			esc_attr( $classname ),
			esc_attr( $classes_and_styles['styles'] ),
			$product
		);
	}

	/**
	 * Get the frontend script handle for this block type.
	 *
	 * @param string $key Data to get, or default to everything.
	 */
	protected function get_block_type_script( $key = null ) {
		return null;
	}

	/**
	 * It isn't necessary register block assets because it is a server side block.
	 */
	protected function register_block_type_assets() {
		return null;
	}
}