WordPress как на ладони
Недорогой хостинг для сайтов на WordPress: wordpress.jino.ru
функция не описана

WC_Cart_Session::get_cart_from_session() WC 3.2.0

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

Это метод класса: WC_Cart_Session

Возвращает

Null. Ничего.

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

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

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

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

Код WC Cart Session::get cart from session: woocommerce/includes/class-wc-cart-session.php WC 3.6.5

<?php
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;
	}

	// 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.
		 */
		if ( apply_filters( 'woocommerce_pre_remove_cart_item_from_session', false, $key, $values ) ) {
			$update_cart_session = true;
			do_action( 'woocommerce_remove_cart_item_from_session', $key, $values );

		} elseif ( ! $product->is_purchasable() ) {
			$update_cart_session = true;
			/* translators: %s: product name */
			wc_add_notice( 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() ), '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 */
			wc_add_notice( 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() ), '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 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_page_permalink( 'cart' ) );
		exit;
	}
}