duanliangman5398 2017-04-10 16:54
浏览 63

如何在有限的时间后过期购物车

I need Help on how to expire a shopping cart
Suppose we have a shopping cart in a e-commerce website, and a user adds a product to his shopping cart the problem is that from the inventory we should decrement the quantity of that product because it’s being held by a user.
The idea is we implement a timer in the shopping cart when that timer expires, the user doesn’t hold that product anymore.

My questions are :

  • How to implement a timer that will work across all pages in the website
  • How to trigger the event of updating the inventory once the timer is done.

Hope my explanation was clear. Thank you for your time.

  • 写回答

1条回答 默认 最新

  • dtbsezxw28056 2017-04-10 17:01
    关注

    One example using session, don't forget that the session will expire when the browser is closed. If you want more persistant data storage, use cookies or localStorage.

    <?php
    
    /* Constants */
    define('EXPIRATION_TIME', 30); // minutes
    
    /* Dummy variables */
    $productAdded = true;
    
    /* Start session */
    session_start();
    
    /* Check timer */
    if (isset($_SESSION['timer']) && $_SESSION['timer'] < time()) {
    
        /* 
            30 min have gone by and the user has not added more products
            to the cart, lets empty the cart and reset the timer
        */
    
        unset($_SESSION['cart']);
        unset($_SESSION['timer']);
    }
    
    /* Add product */
    if ($productAdded) {
    
        /* Increase timer */
        $_SESSION['timer'] = (time() + (EXPIRATION_TIME * 60));
    
        /* Add product to cart, and all other tasks */
        if (!isset($_SESSION['cart']))
            $_SESSION['cart'] = array();
    
        $_SESSION['cart'][] = array(
            'id' => 17,
            'name' => 'Fancy shampoo',
            'quantity' => 1337
        );
    }
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持