예제 #1
0
 public function updateRuleProductData(Mage_SalesRule_Model_Rule $rule)
 {
     //        foreach ($rule->getActions()->getActions() as $action) {
     //            break;
     //        }
     $ruleId = $rule->getId();
     $read = $this->_getReadAdapter();
     $write = $this->_getWriteAdapter();
     $write->delete($this->getTable('salesrule/rule_product'), $write->quoteInto('rule_id=?', $ruleId));
     if (!$rule->getIsActive()) {
         return $this;
     }
     if ($rule->getUsesPerCoupon() > 0) {
         $usedPerCoupon = $read->fetchOne('select count(*) from ' . $this->getTable('salesrule/rule_customer') . ' where rule_id=?', $ruleId);
         if ($usedPerCoupon >= $rule->getUsesPerCoupon()) {
             return $this;
         }
     }
     $productIds = explode(',', $rule->getProductIds());
     $websiteIds = explode(',', $rule->getWebsiteIds());
     $customerGroupIds = explode(',', $rule->getCustomerGroupIds());
     $fromTime = strtotime($rule->getFromDate());
     $toTime = strtotime($rule->getToDate());
     if ($toTime) {
         $toTime += 86400;
     }
     $couponCode = $rule->getCouponCode() ? "'" . $rule->getCouponCode() . "'" : 'NULL';
     $sortOrder = (int) $rule->getSortOrder();
     $rows = array();
     $header = 'replace into ' . $this->getTable('salesrule/rule_product') . ' (rule_id, from_time, to_time, website_id, customer_group_id, product_id, coupon_code, sort_order) values ';
     try {
         $write->beginTransaction();
         foreach ($productIds as $productId) {
             foreach ($websiteIds as $websiteId) {
                 foreach ($customerGroupIds as $customerGroupId) {
                     $rows[] = "('{$ruleId}', '{$fromTime}', '{$toTime}', '{$websiteId}', '{$customerGroupId}', '{$productId}', {$couponCode}, '{$sortOrder}')";
                     if (sizeof($rows) == 100) {
                         $sql = $header . join(',', $rows);
                         $write->query($sql);
                         $rows = array();
                     }
                 }
             }
         }
         if (!empty($rows)) {
             $sql = $header . join(',', $rows);
             $write->query($sql);
         }
         $write->commit();
     } catch (Exception $e) {
         $write->rollback();
         throw $e;
     }
     return $this;
 }
예제 #2
0
파일: Rule.php 프로젝트: natxetee/magento2
 /**
  * Save product attributes currently used in conditions and actions of rule
  *
  * @param Mage_SalesRule_Model_Rule $rule
  * @param mixed $attributes
  * @return Mage_SalesRule_Model_Resource_Rule
  */
 public function setActualProductAttributes($rule, $attributes)
 {
     $write = $this->_getWriteAdapter();
     $write->delete($this->getTable('salesrule_product_attribute'), array('rule_id=?' => $rule->getId()));
     //Getting attribute IDs for attribute codes
     $attributeIds = array();
     $select = $this->_getReadAdapter()->select()->from(array('a' => $this->getTable('eav_attribute')), array('a.attribute_id'))->where('a.attribute_code IN (?)', array($attributes));
     $attributesFound = $this->_getReadAdapter()->fetchAll($select);
     if ($attributesFound) {
         foreach ($attributesFound as $attribute) {
             $attributeIds[] = $attribute['attribute_id'];
         }
         $data = array();
         foreach ($rule->getCustomerGroupIds() as $customerGroupId) {
             foreach ($rule->getWebsiteIds() as $websiteId) {
                 foreach ($attributeIds as $attribute) {
                     $data[] = array('rule_id' => $rule->getId(), 'website_id' => $websiteId, 'customer_group_id' => $customerGroupId, 'attribute_id' => $attribute);
                 }
             }
         }
         $write->insertMultiple($this->getTable('salesrule_product_attribute'), $data);
     }
     return $this;
 }
 /**
  * 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())));
             }
         }
     }
 }