Automattic\WooCommerce\Blocks\BlockTypes\AddToCartWithOptions

VariationSelector{}WC 1.0└─ AbstractBlock

Block type for variation selector in add to cart with options.

No Hooks.

Usage

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

Methods

  1. protected render( $attributes, $content, $block )

VariationSelector{} code WC 10.8.1

class VariationSelector extends AbstractBlock {

	use EnableBlockJsonAssetsTrait;

	/**
	 * Block name.
	 *
	 * @var string
	 */
	protected $block_name = 'add-to-cart-with-options-variation-selector';

	/**
	 * Render the block.
	 *
	 * @param array    $attributes Block attributes.
	 * @param string   $content Block content.
	 * @param WP_Block $block Block instance.
	 * @return string Rendered block output.
	 */
	protected function render( $attributes, $content, $block ): string {
		global $product;

		if ( $product instanceof \WC_Product && $product->is_type( ProductType::VARIABLE ) && ! Utils::is_not_purchasable_product( $product ) ) {
			$p = new \WP_HTML_Tag_Processor( $content );

			if ( $p->next_tag( array( 'class_name' => 'wp-block-woocommerce-add-to-cart-with-options-variation-selector' ) ) ) {
				$p->set_attribute( 'data-wp-watch', 'callbacks.setSelectedVariationId' );
				$p->set_attribute( 'data-wp-watch--validate', 'callbacks.validateVariation' );
			}

			return $p->get_updated_html();
		}

		return '';
	}
}