Skip to content

Commit

Permalink
Merge pull request #3476 from the-events-calendar/tweak/ET-2269-enabl…
Browse files Browse the repository at this point in the history
…e-way-to-customize-cart-cookie-name

[ET-2269] Tickets Commerce: Add filter for cart hash cookie name
  • Loading branch information
codingmusician authored Jan 9, 2025
2 parents e37ea20 + 2a09851 commit a601060
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: tweak

Add filter to customize the cart hash cookie name for Tickets Commerce. [ET-2269]
38 changes: 31 additions & 7 deletions src/Tickets/Commerce/Cart.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
// phpcs:disable WordPressVIPMinimum.Variables.RestrictedVariables.cache_constraints___COOKIE, WordPressVIPMinimum.Functions.RestrictedFunctions.cookies_setcookie

namespace TEC\Tickets\Commerce;

Expand Down Expand Up @@ -230,11 +231,12 @@ public function get_cart_hash( $generate = false ) {

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

$hash_from_cookie = sanitize_text_field( $_COOKIE[ static::get_cart_hash_cookie_name() ] ?? '' );

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

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

Expand Down Expand Up @@ -288,7 +290,7 @@ public function clear_cart() {
$this->set_cart_hash_cookie( null );
$this->get_repository()->clear();

unset( $_COOKIE[ static::$cart_hash_cookie_name ] );
unset( $_COOKIE[ static::get_cart_hash_cookie_name() ] );

return delete_transient( static::get_current_cart_transient() );
}
Expand All @@ -298,7 +300,7 @@ public function clear_cart() {
*
* @since 5.1.9
*
* @parem string $value Value used for the cookie or empty to purge the cookie.
* @param string $value Value used for the cookie or empty to purge the cookie.
*
* @return boolean
*/
Expand All @@ -321,11 +323,11 @@ public function set_cart_hash_cookie( $value = '' ) {
$expire = 1;
}

$is_cookie_set = setcookie( static::$cart_hash_cookie_name, $value ?? '', $expire, COOKIEPATH ?: '/', COOKIE_DOMAIN, is_ssl(), true );
$is_cookie_set = setcookie( static::get_cart_hash_cookie_name(), $value ?? '', $expire, COOKIEPATH ?: '/', COOKIE_DOMAIN, is_ssl(), true );

if ( $is_cookie_set ) {
// Overwrite local variable, so we can use it right away.
$_COOKIE[ static::$cart_hash_cookie_name ] = $value;
$_COOKIE[ static::get_cart_hash_cookie_name() ] = $value;
}

return $is_cookie_set;
Expand Down Expand Up @@ -655,4 +657,26 @@ public function get_cart_total() {
public function get_cart_subtotal(): float {
return $this->get_repository()->get_cart_subtotal();
}

/**
* Get cart has cookie name.
*
* @since TBD
*
* @return string
*/
public static function get_cart_hash_cookie_name(): string {
/**
* Filters the cart hash cookie name.
*
* @since TBD
*
* @param string $cart_hash_cookie_name The cart hash cookie name.
*
* @return string
*/
$filtered_cookie_name = apply_filters( 'tec_tickets_commerce_cart_hash_cookie_name', static::$cart_hash_cookie_name );

return sanitize_title( $filtered_cookie_name );
}
}
19 changes: 19 additions & 0 deletions tests/commerce_integration/TEC/Tickets/Commerce/CartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,23 @@ public function test_cart_total_is_valid() {
$assertion_msg = 'Cart->get_total() should return 0 when the cart contains only free tickets.';
$this->assertEquals( 0, $cart->get_cart_total(), $assertion_msg );
}

/**
* @test
*
* @covers \TEC\Tickets\Commerce\Cart::get_cart_hash_cookie_name
*/
public function test_cart_hash_cookie_name() {
$original_cookie_name = Cart::get_cart_hash_cookie_name();
$this->assertEquals( Cart::$cart_hash_cookie_name, $original_cookie_name );

add_filter( 'tec_tickets_commerce_cart_hash_cookie_name', function() {
return 'different_cookie_name';
} );

$different_cookie_name = Cart::get_cart_hash_cookie_name();
$this->assertEquals( 'different_cookie_name', $different_cookie_name );

$this->assertNotEquals( $original_cookie_name, $different_cookie_name );
}
}

0 comments on commit a601060

Please sign in to comment.