eyemaginesrobbins

magento-b2b-observer

Aug 7th, 2013
116
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /**
  3.  * EYEMAGINE - The leading Magento Solution Partner
  4.  *
  5.  * B2B Customer Restrictions
  6.  *
  7.  * @author    EYEMAGINE <magento@eyemaginetech.com>
  8.  * @category  Eyemagine
  9.  * @package   Eyemagine_B2b
  10.  * @copyright Copyright (c) 2013 EYEMAGINE Technology, LLC (http://www.eyemaginetech.com)
  11.  * @license   http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
  12.  */
  13.  
  14. class Eyemagine_B2b_Model_Observer
  15. {
  16.     /**
  17.      * @var Eyemagine_B2b_Helper_Data $helper
  18.      */
  19.     public $helper;
  20.     /**
  21.      * @var Mage_Checkout_Model_Session $checkout
  22.      */
  23.     public $checkout;
  24.     /**
  25.      * @var Mage_Customer_Model_Session $customer
  26.      */
  27.     public $customer;
  28.     /**
  29.      * @var int|null $_subtotalMin
  30.      */
  31.     protected $_subtotalMin = null;
  32.     /**
  33.      * Initialize helper and save checkout session
  34.      *
  35.      * @return void
  36.      */
  37.     public function __construct()
  38.     {
  39.         $this->helper   = Mage::helper('b2b');
  40.         $this->checkout = Mage::getSingleton("checkout/session");
  41.         $this->customer = Mage::getSingleton("customer/session");
  42.     }
  43.     /**
  44.      * Verifies subtotal meets requirements on cart page
  45.      *
  46.      * @return void
  47.      */
  48.     public function checkTotalsCart()
  49.     {
  50.         if ($this->_hasCartError()) {
  51.             $this->_setErrorMessage();
  52.         }
  53.     }
  54.     /**
  55.      * Verifies subtotal meets requirements on checkout
  56.      *
  57.      * @return void
  58.      */
  59.     public function checkTotalsCheckout()
  60.     {
  61.         if ($this->_hasCartError()) {
  62.             Mage::app()->getFrontController()->getResponse()->setRedirect('/checkout/cart');
  63.         }
  64.     }
  65.     public function unsetErrorMessage()
  66.     {
  67.         Mage::getSingleton("b2b/session")->unsetAll();
  68.     }
  69.     /**
  70.      * Verifies subtotal meets requirements
  71.      *
  72.      * @return bool
  73.      */
  74.     protected function _hasCartError()
  75.     {
  76.         if (!$this->helper->isEnabled() || !$this->_isCustomerB2b()) {
  77.             return false;
  78.         }
  79.         return Mage::getSingleton('checkout/cart')->getQuote()->getSubtotal() < $this->getSubtotalMin();
  80.     }
  81.     /**
  82.      * Check if current customer session is a B2B customer
  83.      *
  84.      * @return bool
  85.      */
  86.     protected function _isCustomerB2b()
  87.     {
  88.         if (!$this->customer->isLoggedIn()) {
  89.             return false;
  90.         }
  91.         return in_array(Mage::getSingleton('customer/session')->getCustomerGroupId(), $this->helper->getB2bCustomerGroupIds());
  92.     }
  93.     /**
  94.      * Fetches the number of orders this customer has made
  95.      *
  96.      * @return int
  97.      */
  98.     public function getSubtotalMin()
  99.     {
  100.         if (is_null($this->_subtotalMin)) {
  101.             if ($this->_getCustomerOrderCount() > 1) {
  102.                 $this->_subtotalMin = $this->helper->getSubtotalMinSubsequent();
  103.             }
  104.             else {
  105.                 $this->_subtotalMin = $this->helper->getSubtotalMin();
  106.             }
  107.         }
  108.         return $this->_subtotalMin;
  109.     }
  110.     /**
  111.      * Returns the number of a times the customer has placed an order
  112.      *
  113.      * @return int
  114.      */
  115.     protected function _getCustomerOrderCount()
  116.     {
  117.         return Mage::getModel('sales/order')->getCollection()
  118.             ->addFieldToFilter("customer_id", $this->customer->getId())
  119.             ->getSize();
  120.     }
  121.     /**
  122.      * Sets the error message when the minimum subtotal is not met
  123.      *
  124.      * @return void
  125.      */
  126.     protected function _setErrorMessage()
  127.     {
  128.         $session = Mage::getSingleton("b2b/session");
  129.         if ($session->isErrorMessageAdded()) {
  130.             return;
  131.         }
  132.         $session->addError(str_replace("%min%",
  133.                 Mage::helper('core')->currency($this->getSubtotalMin(), true, false),
  134.                 $this->helper->getErrorMessage()));
  135.         $session->isErrorMessageAdded(true);
  136.     }
  137. }
RAW Paste Data