Automattic\WooCommerce\Blocks\BlockTypes

Cart::render()protectedWC 1.0

Append frontend scripts when rendering the Cart block.

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

Хуков нет.

Возвращает

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

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

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

Код Cart::render() WC 7.1.0

protected function render( $attributes, $content, $block ) {
	// Deregister core cart scripts and styles.
	wp_dequeue_script( 'wc-cart' );
	wp_dequeue_script( 'wc-password-strength-meter' );
	wp_dequeue_script( 'selectWoo' );
	wp_dequeue_style( 'select2' );

	/**
	 * We need to check if $content has any templates from prior iterations of the block, in order to update to the latest iteration.
	 * We test the iteration version by searching for new blocks brought in by it.
	 * The blocks used for testing should be always available in the block (not removable by the user).
	 */

	$regex_for_filled_cart_block = '/<div[\n\r\s\ta-zA-Z0-9_\-=\'"]*data-block-name="woocommerce\/filled-cart-block"[\n\r\s\ta-zA-Z0-9_\-=\'"]*>/mi';
	// Filled Cart block was added in i2, so we search for it to see if we have a Cart i1 template.
	$has_i1_template = ! preg_match( $regex_for_filled_cart_block, $content );

	if ( $has_i1_template ) {
		/**
		 * This fallback structure needs to match the defaultTemplate variables defined in the block's edit.tsx files,
		 * starting from the parent block and going down each inner block, in the order the blocks were registered.
		 */
		$inner_blocks_html = '$0
		<div data-block-name="woocommerce/filled-cart-block" class="wp-block-woocommerce-filled-cart-block">
			<div data-block-name="woocommerce/cart-items-block" class="wp-block-woocommerce-cart-items-block">
				<div data-block-name="woocommerce/cart-line-items-block" class="wp-block-woocommerce-cart-line-items-block"></div>
			</div>
			<div data-block-name="woocommerce/cart-totals-block" class="wp-block-woocommerce-cart-totals-block">
				<div data-block-name="woocommerce/cart-order-summary-block" class="wp-block-woocommerce-cart-order-summary-block"></div>
				<div data-block-name="woocommerce/cart-express-payment-block" class="wp-block-woocommerce-cart-express-payment-block"></div>
				<div data-block-name="woocommerce/proceed-to-checkout-block" class="wp-block-woocommerce-proceed-to-checkout-block"></div>
				<div data-block-name="woocommerce/cart-accepted-payment-methods-block" class="wp-block-woocommerce-cart-accepted-payment-methods-block"></div>
			</div>
		</div>
		<div data-block-name="woocommerce/empty-cart-block" class="wp-block-woocommerce-empty-cart-block">
		';

		$content = preg_replace( '/<div class="[a-zA-Z0-9_\- ]*wp-block-woocommerce-cart[a-zA-Z0-9_\- ]*">/mi', $inner_blocks_html, $content );
		$content = $content . '</div>';
	}

	/**
	 * Cart i3 added inner blocks for Order summary. We need to add them to Cart i2 templates.
	 * The order needs to match the order in which these blocks were registered.
	 */
	$order_summary_with_inner_blocks = '$0
		<div data-block-name="woocommerce/cart-order-summary-heading-block" class="wp-block-woocommerce-cart-order-summary-heading-block"></div>
		<div data-block-name="woocommerce/cart-order-summary-subtotal-block" class="wp-block-woocommerce-cart-order-summary-subtotal-block"></div>
		<div data-block-name="woocommerce/cart-order-summary-fee-block" class="wp-block-woocommerce-cart-order-summary-fee-block"></div>
		<div data-block-name="woocommerce/cart-order-summary-discount-block" class="wp-block-woocommerce-cart-order-summary-discount-block"></div>
		<div data-block-name="woocommerce/cart-order-summary-coupon-form-block" class="wp-block-woocommerce-cart-order-summary-coupon-form-block"></div>
		<div data-block-name="woocommerce/cart-order-summary-shipping-form-block" class="wp-block-woocommerce-cart-order-summary-shipping-block"></div>
		<div data-block-name="woocommerce/cart-order-summary-taxes-block" class="wp-block-woocommerce-cart-order-summary-taxes-block"></div>
	';
	// Order summary subtotal block was added in i3, so we search for it to see if we have a Cart i2 template.
	$regex_for_order_summary_subtotal = '/<div[\n\r\s\ta-zA-Z0-9_\-=\'"]*data-block-name="woocommerce\/cart-order-summary-subtotal-block"[\n\r\s\ta-zA-Z0-9_\-=\'"]*>/mi';
	$regex_for_order_summary          = '/<div[\n\r\s\ta-zA-Z0-9_\-=\'"]*data-block-name="woocommerce\/cart-order-summary-block"[\n\r\s\ta-zA-Z0-9_\-=\'"]*>/mi';
	$has_i2_template                  = ! preg_match( $regex_for_order_summary_subtotal, $content );

	if ( $has_i2_template ) {
		$content = preg_replace( $regex_for_order_summary, $order_summary_with_inner_blocks, $content );
	}

	return $content;
}