示例#1
0
 /**
  * Load primary coupon (is_primary = 1) for specified rule
  *
  *
  * @param Mage_SalesRule_Model_Coupon $object
  * @param Mage_SalesRule_Model_Rule|int $rule
  * @return unknown
  */
 public function loadPrimaryByRule(Mage_SalesRule_Model_Coupon $object, $rule)
 {
     $read = $this->_getReadAdapter();
     if ($rule instanceof Mage_SalesRule_Model_Rule) {
         $ruleId = $rule->getId();
     } else {
         $ruleId = (int) $rule;
     }
     $select = $read->select()->from($this->getMainTable())->where('rule_id = :rule_id')->where('is_primary = :is_primary');
     $data = $read->fetchRow($select, array(':rule_id' => $ruleId, ':is_primary' => 1));
     if (!$data) {
         return false;
     }
     $object->setData($data);
     $this->_afterLoad($object);
     return true;
 }
示例#2
0
 /**
  * Retrieve rule's primary coupon
  *
  * @return Mage_SalesRule_Model_Coupon
  */
 public function getPrimaryCoupon()
 {
     if ($this->_primaryCoupon === null) {
         $this->_primaryCoupon = Mage::getModel('salesrule/coupon');
         $this->_primaryCoupon->loadPrimaryByRule($this->getId());
         $this->_primaryCoupon->setRule($this)->setIsPrimary(true);
     }
     return $this->_primaryCoupon;
 }
 /**
  * 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())));
             }
         }
     }
 }
示例#4
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());
 }
示例#5
0
 /**
  * Add Coupon Code to Email
  *
  * @param Mage_SalesRule_Model_Coupon $coupon
  *
  * @return $this
  */
 protected function _filterCoupon(Mage_SalesRule_Model_Coupon $coupon)
 {
     if (!in_array('coupon', $this->_filteredObjects)) {
         $this->setField('couponCode', $coupon->getCode());
         $this->_filteredObjects[] = 'coupon';
     }
     return $this;
 }