开发者

delete cart since cronjob in Magento

开发者 https://www.devze.com 2023-03-18 20:12 出处:网络
Hi i thy to delete all cart of customer, i don´t get it since cornjob, i put this in a controller and work fine

Hi i thy to delete all cart of customer, i don´t get it since cornjob, i put this in a controller and work fine

    require_once($_SERVER['DOCUMENT_ROOT'].'/app/Mage.php');
$app = Mage::app();
Mage::getSingleton('core/session', array('name' => 'frontend'));

Mage::getSingleton('checkout/session')->clear(); // Try this

foreach( Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection() as $item ){
 Mage::getSingleton('checkout/cart')->removeItem( $item->getId() )->save(); // or try that
}

header('Location: /'); // send them back to root (or /magento/ if stored in a subfolder)

or

foreach( Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection() as $item ){
Mage::开发者_StackOverflow社区getSingleton('checkout/cart')->removeItem( $item->getId() )->save();
}

but don´t work in a cron. who is the problem?


Cronjobs are not run from within a single user's session, so calls like Mage::getSingleton('checkout/cart') will not retrieve a user's cart. You'll need to invoke each user's particular cart in order to clear it from cron.

Hope that helps!

Thanks, Joe


Try this Mage::getModel('sales/observer')->cleanExpiredQuotes();

This is already a cron schedule function. Go to System -> Configuration -> Sales -> checkout -> Shopping Cart settings and set "Quote Lifetime (days)" as per your requirement.

This will clear already processed order cart entries. To remove all cart entries (Processed/unprocessed) Just override the above observer function and

replace $quotes->addFieldToFilter('is_active', 0);

with

$quotes->addFieldToFilter('is_active', array(1,0));

Hope this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消