I have a function that is displaying a shopping cart and products, and so after a product is added (which happens via GET) I want it to return to regular domain.

This is the code that updates the cart:

$page = $_SERVER['PHP_SELF'];

// if the product is added to the cart
if (isset($_GET['add'])) {
    header('Location: '.$page);
    // echo 'added';

Right now the cart is displayed on my homepage, which is http://localhost/tester/ , and after a product is added the url becomes http://localhost/tester/?add=1, but then I want it to revert back to http://localhost/tester/ but I cannot get it to do that.

Any ideas?

  1. Do you have exit(); after the header(... ?
  2. Do you issue the header() before any headers are sent already?
  3. If that's a WooCommerce then I believe you are doing it wrong. Instead, you should enable AJAX adding to cart (that's not straightforward on the individual product listings, and you may need to modify the default WC template for that). If you redirect after "?add", you may lose the WC "added to cart" notice. See how it's done, for example here.
  • I did not have exit, but I also used another function as suggested to me on stackoverflow which solved it. this is the solution exit(header('Location: '.parse_url($_SERVER['PHP_SELF'],PHP_URL_PATH)));
    – BlekStena
    Mar 20 '14 at 8:38
  • OK, so absence of exit() was the problem, right? I thought so, and put that as #1 in my answer.
    – tivnet
    Mar 20 '14 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.