Automattic\WooCommerce\Blocks\BlockTypes

CheckoutExpressPaymentBlock::sync_express_payment_attrs()publicWC 1.0

Synchorize the express payment attributes between the Cart and Checkout pages.

Method of the class: CheckoutExpressPaymentBlock{}

No Hooks.

Return

null. Nothing (null).

Usage

$CheckoutExpressPaymentBlock = new CheckoutExpressPaymentBlock();
$CheckoutExpressPaymentBlock->sync_express_payment_attrs( $post_id, $post );
$post_id(int) (required)
Post ID.
$post(WP_Post) (required)
Post object.

CheckoutExpressPaymentBlock::sync_express_payment_attrs() code WC 9.6.1

public function sync_express_payment_attrs( $post_id, $post ) {
	if ( wc_get_page_id( 'cart' ) === $post_id ) {
		$cart_or_checkout = 'cart';
	} elseif ( wc_get_page_id( 'checkout' ) === $post_id ) {
		$cart_or_checkout = 'checkout';
	} else {
		return;
	}

	// This is not a proper save action, maybe an autosave, so don't continue.
	if ( empty( $post->post_status ) || 'inherit' === $post->post_status ) {
		return;
	}

	$block_name    = 'woocommerce/' . $cart_or_checkout;
	$page_id       = 'woocommerce_' . $cart_or_checkout . '_page_id';
	$template_name = 'page-' . $cart_or_checkout;

	// Check if we are editing the cart/checkout page and that it contains a Cart/Checkout block.
	// Cast to string for Cart/Checkout page ID comparison because get_option can return it as a string, so better to compare both values as strings.
	if ( ! empty( $post->post_type ) && 'wp_template' !== $post->post_type && ( false === has_block( $block_name, $post ) || (string) get_option( $page_id ) !== (string) $post_id ) ) {
		return;
	}

	// Check if we are editing the Cart/Checkout template and that it contains a Cart/Checkout block.
	if ( ( ! empty( $post->post_type ) && ! empty( $post->post_name ) && $template_name !== $post->post_name && 'wp_template' === $post->post_type ) || false === has_block( $block_name, $post ) ) {
		return;
	}

	if ( empty( $post->post_content ) ) {
		return;
	}

	try {
		// Parse the post content to get the express payment attributes of the current page.
		$blocks = parse_blocks( $post->post_content );
		$attrs  = CartCheckoutUtils::find_express_checkout_attributes( $blocks, $cart_or_checkout );

		if ( ! is_array( $attrs ) ) {
			return;
		}
		$updated_attrs = array_merge( $this->default_styles, $attrs );

		// We need to sync the attributes between the Cart and Checkout pages.
		$other_page = 'cart' === $cart_or_checkout ? 'checkout' : 'cart';

		$this->update_other_page_with_express_payment_attrs( $other_page, $updated_attrs );
	} catch ( Exception $e ) {
		wc_get_logger()->log( 'error', 'Error updating express payment attributes: ' . $e->getMessage() );
	}
}