WC_AJAX::update_order_review │ public static │ WC 1.0

AJAX update order review on checkout.

Метод класса: WC_AJAX{}

Возвращает

void. Ничего (null).

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

$result = WC_AJAX::update_order_review();

Код WC_AJAX::update_order_review() WC 11.2.2

public static function update_order_review() {
	check_ajax_referer( 'update-order-review', 'security' );

	wc_maybe_define_constant( 'WOOCOMMERCE_CHECKOUT', true );

	if ( WC()->cart->is_empty() && ! is_customize_preview() && apply_filters( 'woocommerce_checkout_update_order_review_expired', true ) ) {
		self::update_order_review_expired();
	}

	do_action( 'woocommerce_checkout_update_order_review', isset( $_POST['post_data'] ) ? wp_unslash( $_POST['post_data'] ) : '' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized

	$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
	$posted_shipping_methods = isset( $_POST['shipping_method'] ) ? wc_clean( wp_unslash( $_POST['shipping_method'] ) ) : array();

	if ( is_array( $posted_shipping_methods ) ) {
		foreach ( $posted_shipping_methods as $i => $value ) {
			if ( ! is_string( $value ) || ! ShippingUtil::is_shipping_rate_listed( $i, $value ) ) {
				continue;
			}
			$chosen_shipping_methods[ $i ] = $value;
		}
	}

	WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods );
	WC()->session->set( 'chosen_payment_method', empty( $_POST['payment_method'] ) ? '' : wc_clean( wp_unslash( $_POST['payment_method'] ) ) );
	WC()->customer->set_props(
		array(
			'billing_country'   => isset( $_POST['country'] ) ? wc_clean( wp_unslash( $_POST['country'] ) ) : null,
			'billing_state'     => isset( $_POST['state'] ) ? wc_clean( wp_unslash( $_POST['state'] ) ) : null,
			'billing_postcode'  => isset( $_POST['postcode'] ) ? wc_clean( wp_unslash( $_POST['postcode'] ) ) : null,
			'billing_city'      => isset( $_POST['city'] ) ? wc_clean( wp_unslash( $_POST['city'] ) ) : null,
			'billing_address_1' => isset( $_POST['address'] ) ? wc_clean( wp_unslash( $_POST['address'] ) ) : null,
			'billing_address_2' => isset( $_POST['address_2'] ) ? wc_clean( wp_unslash( $_POST['address_2'] ) ) : null,
		)
	);

	if ( wc_ship_to_billing_address_only() ) {
		WC()->customer->set_props(
			array(
				'shipping_country'   => isset( $_POST['country'] ) ? wc_clean( wp_unslash( $_POST['country'] ) ) : null,
				'shipping_state'     => isset( $_POST['state'] ) ? wc_clean( wp_unslash( $_POST['state'] ) ) : null,
				'shipping_postcode'  => isset( $_POST['postcode'] ) ? wc_clean( wp_unslash( $_POST['postcode'] ) ) : null,
				'shipping_city'      => isset( $_POST['city'] ) ? wc_clean( wp_unslash( $_POST['city'] ) ) : null,
				'shipping_address_1' => isset( $_POST['address'] ) ? wc_clean( wp_unslash( $_POST['address'] ) ) : null,
				'shipping_address_2' => isset( $_POST['address_2'] ) ? wc_clean( wp_unslash( $_POST['address_2'] ) ) : null,
			)
		);
	} else {
		WC()->customer->set_props(
			array(
				'shipping_country'   => isset( $_POST['s_country'] ) ? wc_clean( wp_unslash( $_POST['s_country'] ) ) : null,
				'shipping_state'     => isset( $_POST['s_state'] ) ? wc_clean( wp_unslash( $_POST['s_state'] ) ) : null,
				'shipping_postcode'  => isset( $_POST['s_postcode'] ) ? wc_clean( wp_unslash( $_POST['s_postcode'] ) ) : null,
				'shipping_city'      => isset( $_POST['s_city'] ) ? wc_clean( wp_unslash( $_POST['s_city'] ) ) : null,
				'shipping_address_1' => isset( $_POST['s_address'] ) ? wc_clean( wp_unslash( $_POST['s_address'] ) ) : null,
				'shipping_address_2' => isset( $_POST['s_address_2'] ) ? wc_clean( wp_unslash( $_POST['s_address_2'] ) ) : null,
			)
		);
	}

	if ( isset( $_POST['has_full_address'] ) && wc_string_to_bool( wc_clean( wp_unslash( $_POST['has_full_address'] ) ) ) ) {
		WC()->customer->set_calculated_shipping( true );
	} else {
		WC()->customer->set_calculated_shipping( false );
	}

	WC()->customer->save();

	// Calculate shipping before totals. This will ensure any shipping methods that affect things like taxes are chosen prior to final totals being calculated. Ref: #22708.
	WC()->cart->calculate_shipping();
	WC()->cart->calculate_totals();

	// Get order review fragment.
	ob_start();
	woocommerce_order_review();
	$woocommerce_order_review = ob_get_clean();

	// Get checkout payment fragment.
	ob_start();
	woocommerce_checkout_payment();
	$woocommerce_checkout_payment = ob_get_clean();

	// Get messages if reload checkout is not true.
	$reload_checkout = isset( WC()->session->reload_checkout );
	if ( ! $reload_checkout ) {
		// Capture the error count before printing, because wc_print_notices() clears the queue.
		// A filter on `woocommerce_notice_types` can keep queued errors off the page, so the
		// flag only reports errors that were rendered. Record the types that filter settles on
		// rather than applying it a second time; a non-array iterable can't be inspected after
		// wc_print_notices() consumes it, so it falls back to trusting the rendered output.
		$error_notice_count = wc_notice_count( 'error' );
		$rendered_types     = array();
		$record_types       = static function ( $types ) use ( &$rendered_types ) {
			$rendered_types = $types;
			return $types;
		};
		add_filter( 'woocommerce_notice_types', $record_types, PHP_INT_MAX );
		$messages = wc_print_notices( true );
		remove_filter( 'woocommerce_notice_types', $record_types, PHP_INT_MAX );
		$has_error_notices = 0 < $error_notice_count && ( ! is_array( $rendered_types ) || in_array( 'error', $rendered_types, true ) ) && '' !== $messages;
	} else {
		$has_error_notices = false;
		$messages          = '';
	}

	unset( WC()->session->refresh_totals, WC()->session->reload_checkout );

	/**
	 * Filter the HTML fragments returned with a checkout update, keyed by the selector each one replaces.
	 *
	 * @since 2.3.0
	 *
	 * @param array $fragments Checkout fragments keyed by selector.
	 */
	$fragments = apply_filters(
		'woocommerce_update_order_review_fragments',
		array(
			'.woocommerce-checkout-review-order-table' => $woocommerce_order_review,
			'.woocommerce-checkout-payment'            => $woocommerce_checkout_payment,
		)
	);

	/*
	 * `result` is the legacy signal and only reports whether the response carries any rendered
	 * notice, so a success or info notice still reads as `failure`. Third-party checkout scripts
	 * and `updated_checkout` listeners have consumed it that way since 2014, so it keeps that
	 * meaning. Use `has_errors` to tell a real failure apart from a notice that merely has
	 * something to show.
	 */
	wp_send_json(
		array(
			'result'     => empty( $messages ) ? 'success' : 'failure',
			'has_errors' => $has_error_notices,
			'messages'   => $messages,
			'reload'     => $reload_checkout,
			'fragments'  => $fragments,
		)
	);
}