WC_Cart_Session::dedupe_cookies()privateWC 1.0

Remove duplicate cookies from the response.

Method of the class: WC_Cart_Session{}

No Hooks.

Return

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->dedupe_cookies();

WC_Cart_Session::dedupe_cookies() code WC 9.2.3

private function dedupe_cookies() {
	$all_cookies    = array_filter(
		headers_list(),
		function( $header ) {
			return stripos( $header, 'Set-Cookie:' ) !== false;
		}
	);
	$final_cookies  = array();
	$update_cookies = false;
	foreach ( $all_cookies as $cookie ) {

		list(, $cookie_value)             = explode( ':', $cookie, 2 );
		list($cookie_name, $cookie_value) = explode( '=', trim( $cookie_value ), 2 );

		if ( stripos( $cookie_name, 'woocommerce_' ) !== false ) {
			$key = $this->find_cookie_by_name( $cookie_name, $final_cookies );
			if ( false !== $key ) {
				$update_cookies = true;
				unset( $final_cookies[ $key ] );
			}
		}
		$final_cookies[] = $cookie;
	}

	if ( $update_cookies ) {
		header_remove( 'Set-Cookie' );
		foreach ( $final_cookies as $cookie ) {
			// Using header here preserves previous cookie args.
			header( $cookie, false );
		}
	}
}