Cart::get_cart_hash( $generate = false )

Reads the cart hash from the cookies.


Return

(string|null) The cart hash or null if not found.


Top ↑

Source

File: src/Tickets/Commerce/Cart.php

	public function get_cart_hash( $generate = false ) {
		$cart_hash_length = 12;

		$cart_hash = $this->get_repository()->get_hash();

		if (
			! empty( $_COOKIE[ static::$cart_hash_cookie_name ] )
			&& strlen( $_COOKIE[ static::$cart_hash_cookie_name ] ) === $cart_hash_length
		) {
			$cart_hash = $_COOKIE[ static::$cart_hash_cookie_name ];

			$cart_hash_transient = get_transient( static::get_transient_name( $cart_hash ) );

			if ( empty( $cart_hash_transient ) ) {
				$cart_hash = null;
			}
		}

		if ( empty( $cart_hash ) && $generate ) {
			$tries     = 1;
			$max_tries = 20;

			$this->clear_cart();
			// While we dont find an empty transient to store this cart we loop, but avoid more than 20 tries.
			while (
				( ! empty( $cart_hash_transient ) || empty( $cart_hash ) )
				&& $max_tries >= $tries
			) {
				$cart_hash           = wp_generate_password( $cart_hash_length, false );
				$cart_hash_transient = get_transient( static::get_transient_name( $cart_hash ) );

				// Make sure we increment.
				$tries ++;
			}
		}

		$this->set_cart_hash( $cart_hash );

		return $this->get_repository()->get_hash();
	}

Top ↑

Changelog

Changelog
Version Description
5.1.9 Introduced.