WC_Cart_Session::get_cart_from_session()publicWC 3.2.0

Get the cart data from the PHP session and store it in class variables.

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

Возвращает

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

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

$WC_Cart_Session = new WC_Cart_Session();
$WC_Cart_Session->get_cart_from_session();

Список изменений

С версии 3.2.0 Введена.

Код WC_Cart_Session::get_cart_from_session() WC 9.4.2

public function get_cart_from_session() {
	do_action( 'woocommerce_load_cart_from_session' );
	$this->cart->set_totals( WC()->session->get( 'cart_totals', null ) );
	$this->cart->set_applied_coupons( WC()->session->get( 'applied_coupons', array() ) );
	$this->cart->set_coupon_discount_totals( WC()->session->get( 'coupon_discount_totals', array() ) );
	$this->cart->set_coupon_discount_tax_totals( WC()->session->get( 'coupon_discount_tax_totals', array() ) );
	$this->cart->set_removed_cart_contents( WC()->session->get( 'removed_cart_contents', array() ) );

	$update_cart_session = false; // Flag to indicate the stored cart should be updated.
	$order_again         = false; // Flag to indicate whether this is a re-order.
	$cart                = WC()->session->get( 'cart', null );
	$merge_saved_cart    = (bool) get_user_meta( get_current_user_id(), '_woocommerce_load_saved_cart_after_login', true );

	// Merge saved cart with current cart.
	if ( is_null( $cart ) || $merge_saved_cart ) {
		$saved_cart          = $this->get_saved_cart();
		$cart                = is_null( $cart ) ? array() : $cart;
		$cart                = array_merge( $saved_cart, $cart );
		$update_cart_session = true;

		delete_user_meta( get_current_user_id(), '_woocommerce_load_saved_cart_after_login' );
	}

	// Populate cart from order.
	if ( isset( $_GET['order_again'], $_GET['_wpnonce'] ) && is_user_logged_in() && wp_verify_nonce( wp_unslash( $_GET['_wpnonce'] ), 'woocommerce-order_again' ) ) { // WPCS: input var ok, sanitization ok.
		$cart                = $this->populate_cart_from_order( absint( $_GET['order_again'] ), $cart ); // WPCS: input var ok.
		$order_again         = true;
		$update_cart_session = true;
	}

	// Prime caches to reduce future queries.
	if ( is_callable( '_prime_post_caches' ) ) {
		_prime_post_caches( wp_list_pluck( $cart, 'product_id' ) );
	}

	$cart_contents = array();

	foreach ( $cart as $key => $values ) {
		if ( ! is_customize_preview() && 'customize-preview' === $key ) {
			continue;
		}

		$product = wc_get_product( $values['variation_id'] ? $values['variation_id'] : $values['product_id'] );

		if ( empty( $product ) || ! $product->exists() || 0 >= $values['quantity'] ) {
			continue;
		}

		/**
		 * Allow 3rd parties to validate this item before it's added to cart and add their own notices.
		 *
		 * @since 3.6.0
		 *
		 * @param bool       $remove_cart_item_from_session If true, the item will not be added to the cart. Default: false.
		 * @param string     $key Cart item key.
		 * @param array      $values Cart item values e.g. quantity and product_id.
		 * @param WC_Product $product The product being added to the cart.
		 */
		if ( apply_filters( 'woocommerce_pre_remove_cart_item_from_session', false, $key, $values, $product ) ) {
			$update_cart_session = true;
			/**
			 * Fires when cart item is removed from the session.
			 *
			 * @since 3.6.0
			 *
			 * @param string     $key Cart item key.
			 * @param array      $values Cart item values e.g. quantity and product_id.
			 * @param WC_Product $product The product being added to the cart.
			 */
			do_action( 'woocommerce_remove_cart_item_from_session', $key, $values, $product );

			/**
			 * Allow 3rd parties to override this item's is_purchasable() result with cart item data.
			 *
			 * @param bool       $is_purchasable If false, the item will not be added to the cart. Default: product's is_purchasable() status.
			 * @param string     $key Cart item key.
			 * @param array      $values Cart item values e.g. quantity and product_id.
			 * @param WC_Product $product The product being added to the cart.
			 *
			 * @since 7.0.0
			 */
		} elseif ( ! apply_filters( 'woocommerce_cart_item_is_purchasable', $product->is_purchasable(), $key, $values, $product ) ) {
			$update_cart_session = true;
			/* translators: %s: product name */
			$message = sprintf( __( '%s has been removed from your cart because it can no longer be purchased. Please contact us if you need assistance.', 'woocommerce' ), $product->get_name() );
			/**
			 * Filter message about item removed from the cart.
			 *
			 * @since 3.8.0
			 * @param string     $message Message.
			 * @param WC_Product $product Product data.
			 */
			$message = apply_filters( 'woocommerce_cart_item_removed_message', $message, $product );
			wc_add_notice( $message, 'error' );
			do_action( 'woocommerce_remove_cart_item_from_session', $key, $values );

		} elseif ( ! empty( $values['data_hash'] ) && ! hash_equals( $values['data_hash'], wc_get_cart_item_data_hash( $product ) ) ) { // phpcs:ignore PHPCompatibility.PHP.NewFunctions.hash_equalsFound
			$update_cart_session = true;
			/* translators: %1$s: product name. %2$s product permalink */
			$message = sprintf( __( '%1$s has been removed from your cart because it has since been modified. You can add it back to your cart <a href="%2$s">here</a>.', 'woocommerce' ), $product->get_name(), $product->get_permalink() );
			$message = apply_filters( 'woocommerce_cart_item_removed_because_modified_message', $message, $product );
			wc_add_notice( $message, 'notice' );
			do_action( 'woocommerce_remove_cart_item_from_session', $key, $values );

		} else {
			// Put session data into array. Run through filter so other plugins can load their own session data.
			$session_data = array_merge(
				$values,
				array(
					'data' => $product,
				)
			);

			$cart_contents[ $key ] = apply_filters( 'woocommerce_get_cart_item_from_session', $session_data, $values, $key );

			// Add to cart right away so the product is visible in woocommerce_get_cart_item_from_session hook.
			$this->cart->set_cart_contents( $cart_contents );
		}
	}

	// If it's not empty, it's been already populated by the loop above.
	if ( ! empty( $cart_contents ) ) {
		$this->cart->set_cart_contents( apply_filters( 'woocommerce_cart_contents_changed', $cart_contents ) );
	}

	do_action( 'woocommerce_cart_loaded_from_session', $this->cart );

	if ( $update_cart_session || is_null( WC()->session->get( 'cart_totals', null ) ) ) {
		WC()->session->set( 'cart', $this->get_cart_for_session() );
		$this->cart->calculate_totals();

		if ( $merge_saved_cart ) {
			$this->persistent_cart_update();
		}
	}

	// If this is a re-order, redirect to the cart page to get rid of the `order_again` query string.
	if ( $order_again ) {
		wp_safe_redirect( wc_get_cart_url() );
		exit;
	}
}