示例#1
0
 /**
  * Check if rule can be applied for specific address/quote/customer
  *
  * @param   Mage_SalesRule_Model_Rule $rule
  * @param   Mage_Sales_Model_Quote_Address $address
  * @return  bool
  */
 protected function _canProcessRule($rule, $address)
 {
     if (!$rule->hasIsValid()) {
         /**
          * check per coupon usage limit
          */
         $couponCode = $address->getQuote()->getCouponCode();
         if ($couponCode) {
             $coupon = Mage::getModel('salesrule/coupon');
             $coupon->load($couponCode, 'code');
             if ($coupon->getId()) {
                 // check entire usage limit
                 if ($coupon->getUsageLimit() && $coupon->getTimesUsed() >= $coupon->getUsageLimit()) {
                     $rule->setIsValid(false);
                     return false;
                 }
                 // check per customer usage limit
                 $customerId = $address->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()) {
                         $rule->setIsValid(false);
                         return false;
                     }
                 }
             }
         }
         /**
          * check per rule usage limit
          */
         $ruleId = $rule->getId();
         if ($ruleId && $rule->getUsesPerCustomer()) {
             $customerId = $address->getQuote()->getCustomerId();
             $ruleCustomer = Mage::getModel('salesrule/rule_customer');
             $ruleCustomer->loadByCustomerRule($customerId, $ruleId);
             if ($ruleCustomer->getId()) {
                 if ($ruleCustomer->getTimesUsed() >= $rule->getUsesPerCustomer()) {
                     $rule->setIsValid(false);
                     return false;
                 }
             }
         }
         $rule->afterLoad();
         /**
          * quote does not meet rule's conditions
          */
         if (!$rule->validate($address)) {
             $rule->setIsValid(false);
             return false;
         }
         /**
          * passed all validations, remember to be valid
          */
         $rule->setIsValid(true);
     }
     return $rule->getIsValid();
 }
 /**
  * Check if rule can be applied for specific address/quote/customer
  *
  * @param   Mage_SalesRule_Model_Rule $rule
  * @param   Mage_Sales_Model_Quote_Address $address
  * @return  bool
  */
 protected function _canProcessRule($rule, $address)
 {
     if (!$rule->hasIsValid()) {
         /**
          * too many times used in general
          */
         if ($rule->getUsesPerCoupon() && $rule->getTimesUsed() >= $rule->getUsesPerCoupon()) {
             $rule->setIsValid(false);
             return false;
         }
         /**
          * too many times used for this customer
          */
         $ruleId = $rule->getId();
         if ($ruleId && $rule->getUsesPerCustomer()) {
             $customerId = $address->getQuote()->getCustomerId();
             $ruleCustomer = Mage::getModel('salesrule/rule_customer');
             $ruleCustomer->loadByCustomerRule($customerId, $ruleId);
             if ($ruleCustomer->getId()) {
                 if ($ruleCustomer->getTimesUsed() >= $rule->getUsesPerCustomer()) {
                     $rule->setIsValid(false);
                     return false;
                 }
             }
         }
         $rule->afterLoad();
         /**
          * quote does not meet rule's conditions
          */
         if (!$rule->validate($address)) {
             $rule->setIsValid(false);
             return false;
         }
         /**
          * passed all validations, remember to be valid
          */
         $rule->setIsValid(true);
     }
     return $rule->getIsValid();
 }