Gags
Gags

Reputation: 3829

Clear cart contents if different ID PHP

I have a cart on my website on item detail page. The URL of item detail page is http://example.com/location/987/name where 987 is the ID of restaurant and is treated as r_id in htaccess rewrite.

Now i want to

  1. Clear the cart if r_id in the URL has been changed.

Example:

  1. User added items in cart for URL http://example.com/location/987/name
  2. Now if User chnaged ID as http://example.com/location/123/name then all the content in cart shall be cleared.

I tried doing as below approach but no luck. Cart is getting cleared even on same URL.

if(empty($_SESSION['re_in_ss']) && isset($_GET['r_id'])) {
      $_SESSION['re_in_ss'] = $_GET['r_id'];
} 

else if($_SESSION['re_in_ss'] != $_GET['r_id']){
      $cart->empty_cart();
      $_SESSION['re_in_ss'] = $_GET['r_id'] ;
}

Upvotes: 0

Views: 68

Answers (1)

Mario A
Mario A

Reputation: 3363

I would do it like this:

$rid = isset($_GET['r_id']) ? $_GET['r_id'] : null;

if(empty($rid)) die('r_id is missing');

if(!isset($_SESSION['re_in_ss']) || $_SESSION['re_in_ss'] != $rid) {
    $cart->empty_cart();
    $_SESSION['re_in_ss'] = $rid;
}

Upvotes: 1

Related Questions