Ejemplo n.º 1
0
 /**
  * Look up how many times a coupon was used by a customer
  *
  * @param int                         $customerId
  * @param Mage_SalesRule_Model_Coupon $coupon
  *
  * @return int
  */
 public function getCustomerCouponUseCount($customerId, Mage_SalesRule_Model_Coupon $coupon)
 {
     $couponUsage = new Varien_Object();
     Mage::getResourceModel('salesrule/coupon_usage')->loadByCustomerCoupon($couponUsage, $customerId, $coupon->getId());
     return intval($couponUsage->getTimesUsed());
 }
 /**
  * Validates conditions in the "Rule Information" tab of sales rule admin.
  *
  * @param Mage_SalesRule_Model_Rule $rule
  * @param Mage_SalesRule_Model_Coupon $coupon
  * @return string
  */
 protected function _validateGeneral($rule, $coupon)
 {
     if (!$rule->getIsActive()) {
         Mage::throwException($this->_formatMessage('Your coupon is inactive.'));
     }
     // check websites
     $websiteIds = $rule->getWebsiteIds();
     if (!in_array($this->_getQuote()->getStore()->getWebsiteId(), $websiteIds)) {
         $websiteNames = Mage::getResourceModel('core/website_collection')->addFieldToFilter('website_id', array('in' => $websiteIds))->getColumnValues('name');
         Mage::throwException($this->_formatMessage('Your coupon is not valid for this store.', implode(', ', $websiteNames), 'Allowed Websites: %s.'));
     }
     // check customer groups
     $groupIds = $rule->getCustomerGroupIds();
     if (!in_array($this->_getQuote()->getCustomerGroupId(), $groupIds)) {
         $customerGroupNames = Mage::getResourceModel('customer/group_collection')->addFieldToFilter('customer_group_id', array('in' => $groupIds))->getColumnValues('customer_group_code');
         Mage::throwException($this->_formatMessage('Your coupon is not valid for your Customer Group.', implode(', ', $customerGroupNames), 'Allowed Customer Groups: %s.'));
     }
     // check dates
     $now = new Zend_Date(Mage::getModel('core/date')->timestamp(time()), Zend_Date::TIMESTAMP);
     // check from date
     if ($rule->getFromDate()) {
         $fromDate = new Zend_Date($rule->getFromDate(), Varien_Date::DATE_INTERNAL_FORMAT);
         if ($now->isEarlier($fromDate, Zend_Date::DATE_MEDIUM)) {
             Mage::throwException($this->_formatMessage('Your coupon is not valid yet. It will be active on %s.', Mage::helper('core')->formatDate($fromDate, Mage_Core_Model_Locale::FORMAT_TYPE_LONG), ''));
         }
     }
     // check to date
     if ($rule->getToDate()) {
         $toDate = new Zend_Date($rule->getToDate(), Varien_Date::DATE_INTERNAL_FORMAT);
         if ($now->isLater($toDate, Zend_Date::DATE_MEDIUM)) {
             Mage::throwException($this->_formatMessage('Your coupon is no longer valid. It expired on %s.', Mage::helper('core')->formatDate($toDate, Mage_Core_Model_Locale::FORMAT_TYPE_LONG), ''));
         }
     }
     // magemail coupon-level auto-expiration date
     $isCouponAlreadyUsed = $coupon->getUsageLimit() && $coupon->getTimesUsed() >= $coupon->getUsageLimit();
     if ($coupon->getdata('magemail_expired_at') && $isCouponAlreadyUsed) {
         $expirationDate = Mage::getSingleton('core/date')->date('M d, Y', $coupon->getdata('magemail_expired_at'));
         Mage::throwException($this->_formatMessage('Your coupon is no longer valid. It expired on %s.', $expirationDate));
     }
     // check global usage limit
     if ($coupon->getUsageLimit() && $coupon->getTimesUsed() >= $coupon->getUsageLimit()) {
         Mage::throwException($this->_formatMessage('Your coupon was already used.', $coupon->getUsageLimit(), sprintf('It may only be used %d time(s).', $coupon->getUsageLimit())));
     }
     // check per customer usage limit
     $customerId = $this->_getQuote()->getCustomerId();
     if ($customerId && $coupon->getUsagePerCustomer()) {
         $couponUsage = new Varien_Object();
         Mage::getResourceModel('salesrule/coupon_usage')->loadByCustomerCoupon($couponUsage, $customerId, $coupon->getId());
         if ($couponUsage->getCouponId() && $couponUsage->getTimesUsed() >= $coupon->getUsagePerCustomer()) {
             Mage::throwException($this->_formatMessage('You have already used your coupon.', $coupon->getUsageLimit(), sprintf('It may only be used %d time(s).', $coupon->getUsagePerCustomer())));
         }
     }
     // check per rule usage limit
     $ruleId = $rule->getId();
     if ($ruleId && $rule->getUsesPerCustomer()) {
         $ruleCustomer = Mage::getModel('salesrule/rule_customer');
         $ruleCustomer->loadByCustomerRule($customerId, $ruleId);
         if ($ruleCustomer->getId()) {
             if ($ruleCustomer->getTimesUsed() >= $rule->getUsesPerCustomer()) {
                 Mage::throwException($this->_formatMessage('You have already used your coupon.', $coupon->getUsageLimit(), sprintf('It may only be used %d time(s).', $coupon->getUsagePerCustomer())));
             }
         }
     }
 }