Exemplo n.º 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->hasIsValidForAddress($address) && !$address->isObjectNew()) {
         return $rule->getIsValidForAddress($address);
     }
     /**
      * check per coupon usage limit
      */
     if ($rule->getCouponType() != Mage_SalesRule_Model_Rule::COUPON_TYPE_NO_COUPON) {
         $couponCode = $address->getQuote()->getCouponCode();
         if (strlen($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->setIsValidForAddress($address, 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->setIsValidForAddress($address, 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->setIsValidForAddress($address, false);
                 return false;
             }
         }
     }
     $rule->afterLoad();
     /**
      * quote does not meet rule's conditions
      */
     if (!$rule->validate($address)) {
         $rule->setIsValidForAddress($address, false);
         return false;
     }
     /**
      * passed all validations, remember to be valid
      */
     $rule->setIsValidForAddress($address, true);
     return true;
 }
 /**
  * Check if rule can be applied for specific address/quote/customer
  *
  * @param   Mage_SalesRule_Model_Rule
  * @param   Mage_Sales_Model_Quote_Address
  * @return  bool
  */
 protected function _canProcessRule(Mage_SalesRule_Model_Rule $rule, Mage_Sales_Model_Quote_Address $address)
 {
     /**
      * check if validation already occurred
      */
     if ($rule->hasIsValidForAddress($address) && $address->isObjectNew() === false) {
         return $rule->getIsValidForAddress($address);
     }
     /**
      * check per rule usage limit
      */
     if ($rule->getId() && $rule->getUsesPerCustomer() && $this->isValidRuleUsageForAddress($rule, $address) === false) {
         $rule->setIsValidForAddress($address, false);
         return false;
     }
     $rule->afterLoad();
     /**
      * quote does not meet rule's conditions
      */
     if ($rule->validate($address) === false) {
         $rule->setIsValidForAddress($address, false);
         return false;
     }
     /**
      * passed all validations, remember to be valid
      */
     $rule->setIsValidForAddress($address, true);
     return true;
 }