30分钟后从购物车中取出物品 - Magento

时间:2013-12-16 13:12:55

标签: magento shopping-cart

我想从登录和购物者的购物车中删除商品。

我已将Cookie会话超时设置为1800,以解决客户购物者,但在报价生命周期内,我只能选择1天。无论如何要达到30分钟?

我改变了:

$lifetimes = Mage::getConfig()->getStoresConfigByPath('checkout/cart/delete_quote_after');

foreach ($lifetimes as $storeId=>$lifetime) {
    $lifetime *= 86400;
}

$lifetimes = Mage::getConfig()->getStoresConfigByPath('checkout/cart/delete_quote_after');

foreach ($lifetimes as $storeId=>$lifetime) {
    $lifetime *= 1800;
}

然后在报价生活时间内设置1天。仍然没有快乐。

4 个答案:

答案 0 :(得分:1)

这是您可以尝试的另一种解决方案

将此代码粘贴到sidebar.phtml

<?php if ($this->helper('customer')->isLoggedIn()) {
$session = Mage::getSingleton('checkout/cart');
if ($session['quote']) {
    $cart = Mage::getModel('checkout/cart')->getQuote()->getData();
    $qtyCart = (int) $cart['items_qty'];
    if ($qtyCart > 0) {

        $updateAt = $session['quote']->getUpdatedAt(); // cart update date
        $currentDate = strtotime($updateAt);
        $futureDate = $currentDate + (60 * 20); //target date
        $date1 = date("Y-m-d H:i:s");
        $date1 = strtotime($date1); // current date
        //$futureDate = date();

        $dateFormat = "d F Y -- g:i a";
        $targetDate = $futureDate; //Change the 25 to however many minutes you want to countdown
        $actualDate = $date1;
        $secondsDiff = $targetDate - $actualDate;
        $remainingDay = floor($secondsDiff / 60 / 60 / 24);
        $remainingHour = floor(($secondsDiff - ($remainingDay * 60 * 60 * 24)) / 60 / 60);
        $remainingMinutes = floor(($secondsDiff - ($remainingDay * 60 * 60 * 24) - ($remainingHour * 60 * 60)) / 60);
        $remainingSeconds = floor(($secondsDiff - ($remainingDay * 60 * 60 * 24) - ($remainingHour * 60 * 60)) - ($remainingMinutes * 60));
        $actualDateDisplay = date($dateFormat, $actualDate);
        $targetDateDisplay = date($dateFormat, $targetDate);
        ?>
        <script type="text/javascript">
          var days = <?php echo $remainingDay; ?>  
          var hours = <?php echo $remainingHour; ?>  
          var minutes = <?php echo $remainingMinutes; ?>  
          var seconds = <?php echo $remainingSeconds; ?>

        function setCountDown(statusfun)
        {//alert(seconds);
             var SD;
            if(days >= 0 && minutes >= 0){
                    var dataReturn =  jQuery.ajax({
                          type: "GET",
                          url: "<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB) . 'index.php/countdowncont/'; ?>",
                          async: true,
                          success: function(data){
                              var data = data.split("/");
                              day =  data[0];
                              hours =  data[1];
                              minutes =  data[2];
                              seconds =  data[3];

                          }
                      });
                    seconds--;
                    if (seconds < 0){
                        minutes--;
                        seconds = 59
                    }
                    if (minutes < 0){
                        hours--;
                        minutes = 59
                    }
                    if (hours < 0){
                        days--;
                        hours = 23
                    }
                     document.getElementById("remain").style.display = "block";
                      document.getElementById("remain").innerHTML = " Items reversed for <span> "+minutes+":"+seconds+"</span> minutes.";

                    SD=window.setTimeout( "setCountDown()", 1000 );
            }else{
            document.getElementById("remain").innerHTML = "";
            seconds = "00"; window.clearTimeout(SD);
            jQuery.ajax({
                type: "GET",
                url: "<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB) . 'index.php/countdown/'; ?>", // this the path of your controller
                async: false,
                success: function(html){

                }
            });
            document.getElementById("remain").innerHTML = "";
            //window.location = document.URL; // Add your redirect url*/

        }
        }
        </script>
        <?php if ($date1 < $futureDate && ($qtyCart > 0)) { ?>
            <script type="text/javascript">
                setCountDown();
            </script>
        <?php } else { 
  foreach( 
 Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection()
  as      $item ){
 Mage::getSingleton('checkout/cart')->removeItem( $item->getId() )->save();}
     ?>
            <style>

            #remain{display:none;}
            </style>
            <?php
        }
    }
}
} ?>

现在创建一个控制器并将其分配给ajax url并将以下代码传递给该控制器

public function indexAction() {
       $cartHelperAjax = Mage::helper('checkout/cart');
       $cart = Mage::getModel('checkout/cart')->getQuote()->getData();
       $qtyCart = (int)$cart['items_qty'];
       if($qtyCart > 0){
           $updateAt = $cartHelperAjax->getQuote()->getUpdatedAt(); // cart update date
           $currentDate = strtotime($updateAt);
           $futureDate = $currentDate+(60*20);//target date
           $date1 = date("Y-m-d H:i:s"); 
           $date1 = strtotime($date1); // current date

           //$futureDate = date();

           $dateFormat = "d F Y -- g:i a";
           $targetDate = $futureDate;//Change the 25 to however many minutes you want to countdown
           $actualDate = $date1;
           $secondsDiff = $targetDate - $actualDate;
           $remainingDay     = floor($secondsDiff/60/60/24);
           $remainingHour    = floor(($secondsDiff-($remainingDay*60*60*24))/60/60);
           $remainingMinutes = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))/60);
           $remainingSeconds = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))-($remainingMinutes*60));
           $actualDateDisplay = date($dateFormat,$actualDate);
           $targetDateDisplay = date($dateFormat,$targetDate);

        echo   $total_remainTime =  $remainingDay ."/".$remainingHour."/".$remainingMinutes."/".$remainingSeconds;
       }         
   }
通过这些代码行可以创建一个倒数计时器。在特定时间之后,购物车中的所有物品都将被移除。

答案 1 :(得分:1)

我想说创建一个模块,为报价项添加到期时间,然后检查每个到期时间。

的内容
class My_Module_Model_Observer
{
    /* observes sales_quote_add_item */
    public function addItemExpiration(Varien_Event_Observer $event)
    {
        $item = $event->getItem();
        $itemExpiration = Mage::getModel('catalog/session')->getQuoteItemExpiration();
        if (!$itemExpiration)$itemExpiration = array();
        $itemExpiration[$item->getId()] = (time() + (30 * 60));
        Mage::getModel('catalog/session')->setQuoteItemExpiration($itemExpiration);
    }

    /* observes sales_quote_load_after */
    public function removeExpiredItems(Varien_Event_Observer $event)
    {
        $itemExpiration = Mage::getModel('catalog/session')->getQuoteItemExpiration();
        foreach ($event->getQuote()->getAllItems() as $item) {
            if ($itemExpiration[$item->getId()] < time()) {
                $event->getQuote()->removeItem($item->getId());
            }
        }
    }

}

请注意,我没有对此进行测试,只是通过内存进行编码,因此如果没有一些修改,它可能无法正常工作

答案 2 :(得分:0)

Admin > System > Configuration > Web > Session Cookie Management > Cookie Lifetime

设置超时

答案 3 :(得分:0)

请尝试使用此代码删除最后一天的购物车项目:

<?php

mb_internal_encoding('UTF-8');
error_reporting(E_ALL);
ini_set("display_errors", 1);
require_once '../app/Mage.php';
Mage::app('admin');

$STORE_ID = 0;

Mage::app()->setCurrentStore($STORE_ID);

$lifetime = 3600;

$quoteCollection = Mage::getModel('sales/quote')
        ->getCollection()
        ->addFieldToFilter('created_at', array('to' => date("Y-m-d H:i:s", time() - $lifetime)))
        ->addFieldToFilter('is_active', 1);

//print_r($quoteCollection);
//echo count($quoteCollection);
//die;
// $date = date('Y-m-d H:i:s');
//echo date("Y-m-d H:i:s", time() - $lifetime) . '<br><br>';

foreach ($quoteCollection as $item) {
    $item->delete();
}

echo PHP_EOL . "All process completed now." . PHP_EOL;
?>