Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.83 KB | None | 0 0
  1. class Listrak_Remarketing_CartController extends Mage_Core_Controller_Front_Action
  2. {
  3.     private $_ltkSession = false;
  4.  
  5.     public function indexAction()
  6.     {
  7.         return $this;
  8.     }
  9.  
  10.     public function reloadAction()
  11.     {
  12.         $checkout = Mage::getSingleton('checkout/session');
  13.         $cust = Mage::getSingleton('customer/session');
  14.         $chkQuote = Mage::helper('checkout/cart')->getQuote();
  15.  
  16.         try {
  17.             $ltksid = $this->_getLtksid();
  18.             if (!$ltksid) {
  19.                 return $this->_redirectAfterReload();
  20.             }
  21.  
  22.             $ltksidcookie = Mage::getModel('core/cookie')->get('ltksid');
  23.             if (!empty($ltksidcookie) && $ltksidcookie == $ltksid && $chkQuote && $chkQuote->getId()) {
  24.                 return $this->_redirectAfterReload();
  25.             }
  26.  
  27.             $ltksession = $this->_getSession();
  28.             if ($ltksession && $ltksession->getQuoteId() && $cust && $cust->isLoggedIn() && $cust->getId() === $ltksession->getCustomerId()) {
  29.                 return $this->_redirectAfterReload();
  30.             }
  31.  
  32.             $quote = $this->_getQuote();
  33.             if ($quote && $quote->getId() && $quote->getId() != $checkout->getQuoteId() && $quote->getIsActive()) {
  34.                 if (!$chkQuote) {
  35.                     $chkQuote = Mage::getModel('sales/quote');
  36.                 }
  37.  
  38.                 $chkQuote->merge($quote)
  39.                     ->collectTotals()
  40.                     ->save();
  41.                 $checkout->setQuoteId($chkQuote->getId());
  42.             }
  43.         } catch (Exception $ex) {
  44.             Mage::getModel("listrak/log")->addException($ex);
  45.         }
  46.  
  47.         return $this->_redirectAfterReload();
  48.     }
  49.    
  50.     private function _getLtksid()
  51.     {
  52.         return $this->getRequest()->getParam('ltksid');
  53.     }
  54.  
  55.     private function _getSession()
  56.     {
  57.         if ($this->_ltkSession === false) {
  58.             $sid = $this->_getLtksid();
  59.  
  60.             if(preg_match('/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/i', $sid))
  61.             {
  62.                 if (Mage::helper('remarketing')->trackingTablesExist()) {
  63.                     $ltksession = Mage::getModel("listrak/session")->setSessionId($sid);
  64.                     $ltksession->getResource()->loadBySessionId($ltksession);
  65.  
  66.                     if ($ltksession->hasQuoteId())
  67.                         $this->_ltkSession = $ltksession;
  68.                 }
  69.             }
  70.  
  71.             if ($this->_ltkSession === false)
  72.                 $this->_ltkSession = null;
  73.         }
  74.  
  75.         return $this->_ltkSession;
  76.     }
  77.  
  78.     private function _getQuote()
  79.     {
  80.         $session = $this->_getSession();
  81.         if ($session) {
  82.             $storeId = Mage::app()->getStore()->getStoreId();
  83.             $quoteId = $session->getQuoteId();
  84.         }
  85.         else {
  86.             $sid = $this->_getLtksid();
  87.             $qid = Mage::helper('remarketing')->urlDecrypt($sid);
  88.  
  89.             $parts = explode(' ', $qid, 2);
  90.             if (sizeof($parts) > 1) {
  91.                 $storeId = intval($parts[0]);
  92.                 $quoteId = intval($parts[1]);
  93.             }
  94.         }
  95.  
  96.         if (isset($storeId) && isset($quoteId)) {
  97.             $quote = Mage::getModel('sales/quote')->setStoreId($storeId)->load($quoteId);
  98.             if ($quote->getEntityId())
  99.                 return $quote;
  100.         }
  101.  
  102.         return null;
  103.     }
  104.  
  105.     private function _redirectAfterReload()
  106.     {
  107.         $qs = $this->getRequest()->getParams();
  108.         unset($qs["redirectUrl"]);
  109.         unset($qs["ltksid"]);
  110.  
  111.         $url = $this->getRequest()->getParam('redirectUrl');
  112.         if (!$url) {
  113.             $url = 'checkout/cart/';
  114.         }
  115.  
  116.         return $this->_redirect(
  117.             $url,
  118.             array('_query' => $qs, '_secure' => Mage::app()->getStore()->isCurrentlySecure())
  119.         );
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement