• I’m working on one client site, the previous developer uses WP custom post type build a product collection page, kind like woocommerce, but only can add to cart to request a quote to the company, no transaction on the cart application.

    I have trouble with I can add to cart is fine, but when I view the cart page. the item won’t show up unless I refresh the page. same problem with delete item in the cart.

    There’s a cookie set in the code, which I not familiar with, thinking might be the issue.

    I thought I move the to the top will fix some issue, but the cart won’t work. I got some unexpected end error on WP debug tool, so I tried to use PHP checker to see how many } I miss and add it back, and still not working.

    Thank you so much for the help!

    —–– More Info ———-

    Wordpress version: 4.9.7 PHP server: 5.6 Theme development date: unknown (Twenty thirteen theme modify version, never update)

    —–– code ———-

    <?php
        if(get_query_var('remove') > 0){
          $r = get_query_var('remove');
          $d = get_query_var('d');
          if (isset($_COOKIE['cart'])) {
            $cart = json_decode(base64_decode($_COOKIE['cart']), true);
            unset($cart[$r][$d]);
            if(empty($cart[$r])){
                unset($cart[$r]);
            }
            if(empty($cart)){
                setcookie("cart", base64_encode(json_encode($cart)), time() - 3600, '/');
            } else {
                setcookie("cart", base64_encode(json_encode($cart)), time() + 2592000, '/');
            }
            header('Location: '.get_the_permalink());
            die();
        } else {
            header('Location: '.get_the_permalink());
            die();
        }
     }
    ?>
    
    <?php get_header();?>
    <?php if(isset($ok) && $ok == 1){?>
          <p class='after_cart_text'>Thank you for the opportunity to quote your project!  We will endeavour to respond within 24 hours.</p>
    <?php } else { ?>
    <?php if(isset($ok) && $ok == -1){?>
        <div class="alert alert-danger" role="alert">Please check the the captcha form.</div>
    
    <?php } ?>
    
        <div class="baners_categories">
          <?php
            $rows = get_field('banery_categories', 'options');
                    if($rows){
                    shuffle( $rows );
                    $image = $rows[0]['picture_categories'];
                    ?>
                    <img src="<?php echo $image; ?>" />
                    <?php
                    }
                    ?>
                    </div>
    
    </fieldset>
    <fieldset>
    
    <!-- Form Name -->
    <p class="legend_pe"><legend>Product Information</legend></p>
    <?php if(isset($_COOKIE['cart']) && !empty($_COOKIE['cart']) && $post->post_name == 'cart'){
    $cart = json_decode(base64_decode($_COOKIE['cart']), true);
    //var_dump($cart);
    $i = 0;
    if (is_array($cart)) {
    foreach($cart as $k => $row){
      foreach($row as $k2 => $item){
            //var_dump($item);
    ?>
    
    <!--  form section code I comment it out -->
    
          <?php }?>
            <?php }?>
        <div class="col-sm-4" style="margin-bottom:15px;">
            <p><strong>Your cart is currently empty.</strong></p>
         </div>
    
          <?php }?>
          <?php }?>
    <?php }?>
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    Thank you so much for any helps or tips!

    • This topic was modified 6 years, 4 months ago by madebymt.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    The code appears to be attempting to reload the cart page by sending location headers. (TBH, I didn’t study your code too closely). Apparently the problem is the browser is loading the Location request from cache instead of getting the updated version. Then the solution is to not allow the page to be cached. Sending these headers should do that:

    header('Cache-Control: no-cache, no-store, must-revalidate');
    header('Pragma: no-cache');
    header('Expires: 0');
Viewing 1 replies (of 1 total)
  • The topic ‘WordPress cookie for cart page (PHP cookie)’ is closed to new replies.