diff --git a/composer.json b/composer.json index 4ce3d80..31bdfbb 100644 --- a/composer.json +++ b/composer.json @@ -12,9 +12,14 @@ { "name": "Andreas von Studnitz", "email": "avs@integer-net.de" + }, + { + "name": "Fabian Blechschmidt", + "email": "blechschmidt@fabian-blechschmidt.de" } ], "require": { + "php": "^7.0", "magento-hackathon/magento-composer-installer": "*" }, "require-dev": { diff --git a/src/app/code/community/Hackathon/PromoCodeMessages/Model/Observer.php b/src/app/code/community/Hackathon/PromoCodeMessages/Model/Observer.php index fdf132e..0977bc1 100644 --- a/src/app/code/community/Hackathon/PromoCodeMessages/Model/Observer.php +++ b/src/app/code/community/Hackathon/PromoCodeMessages/Model/Observer.php @@ -23,7 +23,6 @@ class Hackathon_PromoCodeMessages_Model_Observer { - /** * Called on sales_quote_collect_totals_after. Ensure that the action is couponPost before continuing. * @@ -31,23 +30,32 @@ class Hackathon_PromoCodeMessages_Model_Observer */ public function validateCode(Varien_Event_Observer $observer) { - if (Mage::getStoreConfigFlag('checkout/promocodemessages/enabled')) { - $action = Mage::app()->getRequest()->getActionName(); + if (!Mage::getStoreConfigFlag('checkout/promocodemessages/enabled')) { + return; + } - if ($action == 'couponPost') { + if (Mage::app()->getRequest()->getActionName() !== 'couponPost') { + return; + } - if (Mage::app()->getRequest()->getParam('remove') == 1) { - return; - } + if ((int)Mage::app()->getRequest()->getParam('remove') === 1) { + return; + } - $quote = $observer->getQuote(); - $couponCode = $quote->getCouponCode(); + $quote = $observer->getQuote(); + $couponCode = $quote->getCouponCode(); - if (!$couponCode || $couponCode == '') { - // parent validation has failed - $couponCode = (string)Mage::app()->getRequest()->getParam('coupon_code'); - Mage::getModel('hackathon_promocodemessages/validator')->validate($couponCode, $quote); - } + if (!$couponCode) { + // parent validation has failed + $couponCode = (string)Mage::app()->getRequest()->getParam('coupon_code'); + try { + Mage::getModel('hackathon_promocodemessages/validator')->validate($couponCode, $quote); + } catch (Mage_Core_Exception $e) { + $msg = Mage::helper('hackathon_promocodemessages') + ->__('Your coupon "%s" could not be redeemed.', $couponCode); + $msg = "

$msg

"; + $msg .= $e->getMessage(); + throw new Mage_Core_Exception($msg, 0, $e); } } } diff --git a/src/app/code/community/Hackathon/PromoCodeMessages/Model/Validator.php b/src/app/code/community/Hackathon/PromoCodeMessages/Model/Validator.php index d75bc7a..ac1f300 100644 --- a/src/app/code/community/Hackathon/PromoCodeMessages/Model/Validator.php +++ b/src/app/code/community/Hackathon/PromoCodeMessages/Model/Validator.php @@ -24,7 +24,7 @@ class Hackathon_PromoCodeMessages_Model_Validator extends Mage_Core_Model_Abstra { /** @var Mage_Sales_Model_Quote */ - protected $_quote = null; + protected $_quote; /** * Array of conditions attached to the current rule. @@ -45,13 +45,13 @@ class Hackathon_PromoCodeMessages_Model_Validator extends Mage_Core_Model_Abstra * * @var array */ - protected $_foundOperators = null; + protected $_foundOperators; /** * Default values for 'not found' operator options. * @var array */ - protected $_notFoundOperators = null; + protected $_notFoundOperators; /** * Rule-specific attributes that use price. Note that product attribute type is determined dynamically. @@ -87,7 +87,7 @@ public function __construct() * * @param string $couponCode * @param Mage_Sales_Model_Quote $quote - * @return string + * @return void * @throws Mage_Core_Exception */ public function validate($couponCode, $quote) @@ -105,7 +105,7 @@ public function validate($couponCode, $quote) // no coupon if (!$coupon->getId()) { - Mage::throwException($this->_formatMessage('Coupon code does not exist.')); + Mage::throwException($this->_formatMessageWithContainer('Coupon code does not exist.')); } /** @var $rule Mage_SalesRule_Model_Rule */ @@ -129,40 +129,42 @@ public function validate($couponCode, $quote) * * @param Mage_SalesRule_Model_Rule $rule * @param Mage_SalesRule_Model_Coupon $coupon - * @return string + * @return void * @throws Mage_Core_Exception */ protected function _validateGeneral($rule, $coupon) { - $msg = ''; if (!$rule->getIsActive()) { - Mage::throwException($this->_formatMessage('Your coupon is inactive.')); + Mage::throwException($this->_formatMessageWithContainer('Your coupon is inactive.')); } // check websites + /** @var int[] $websiteIds */ $websiteIds = $rule->getWebsiteIds(); - if (!in_array($this->_getQuote()->getStore()->getWebsiteId(), $websiteIds)) { + if (!in_array($this->_getQuote()->getStore()->getWebsiteId(), $websiteIds, false)) { $websiteNames = Mage::getResourceModel('core/website_collection') ->addFieldToFilter('website_id', ['in' => $websiteIds]) ->getColumnValues('name'); - $msg .= $this->_formatMessage( + $msg = $this->_formatMessageWithContainer( 'Your coupon is not valid for this store.', implode(', ', $websiteNames), 'Allowed Websites: %s.' ); + Mage::throwException($msg); } // check customer groups $groupIds = $rule->getCustomerGroupIds(); - if (!in_array($this->_getQuote()->getCustomerGroupId(), $groupIds)) { + if (!in_array($this->_getQuote()->getCustomerGroupId(), $groupIds, false)) { $customerGroupNames = Mage::getResourceModel('customer/group_collection') ->addFieldToFilter('customer_group_id', ['in' => $groupIds]) ->getColumnValues('customer_group_code'); - $msg .= $this->_formatMessage( + $msg = $this->_formatMessageWithContainer( 'Your coupon is not valid for your Customer Group.', implode(', ', $customerGroupNames), 'Allowed Customer Groups: %s.' ); + Mage::throwException($msg); } // check dates @@ -172,10 +174,11 @@ protected function _validateGeneral($rule, $coupon) if ($rule->getFromDate()) { $fromDate = new Zend_Date($rule->getFromDate(), Varien_Date::DATE_INTERNAL_FORMAT); if ($now->isEarlier($fromDate, Zend_Date::DATE_MEDIUM)) { - $msg .= $this->_formatMessage( + $msg = $this->_formatMessageWithContainer( 'Your coupon is not valid yet. It will be active on %s.', Mage::helper('core')->formatDate($fromDate, Mage_Core_Model_Locale::FORMAT_TYPE_LONG) ); + Mage::throwException($msg); } } @@ -183,30 +186,33 @@ protected function _validateGeneral($rule, $coupon) if ($rule->getToDate()) { $toDate = new Zend_Date($rule->getToDate(), Varien_Date::DATE_INTERNAL_FORMAT); if ($now->isLater($toDate, Zend_Date::DATE_MEDIUM)) { - $msg .= $this->_formatMessage( + $msg = $this->_formatMessageWithContainer( 'Your coupon is no longer valid. It expired on %s.', Mage::helper('core')->formatDate($toDate, Mage_Core_Model_Locale::FORMAT_TYPE_LONG) ); + Mage::throwException($msg); } } // 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')); - $msg .= $this->_formatMessage( + if ($isCouponAlreadyUsed && $coupon->getData('magemail_expired_at')) { + $mageMailToDate = new Zend_Date($coupon->getData('magemail_expired_at'), Varien_Date::DATE_INTERNAL_FORMAT); + $msg = $this->_formatMessageWithContainer( 'Your coupon is no longer valid. It expired on %s.', - $expirationDate + Mage::helper('core')->formatDate($mageMailToDate, Mage_Core_Model_Locale::FORMAT_TYPE_LONG) ); + Mage::throwException($msg); } // check global usage limit if ($coupon->getUsageLimit() && $coupon->getTimesUsed() >= $coupon->getUsageLimit()) { - $msg .= $this->_formatMessage( + $msg = $this->_formatMessageWithContainer( 'Your coupon was already used.', $coupon->getUsageLimit(), 'It may only be used %s time(s).' ); + Mage::throwException($msg); } // check per customer usage limit $customerId = $this->_getQuote()->getCustomerId(); @@ -215,15 +221,17 @@ protected function _validateGeneral($rule, $coupon) Mage::getResourceModel('salesrule/coupon_usage')->loadByCustomerCoupon( $couponUsage, $customerId, - $coupon->getId()); + $coupon->getId() + ); if ($couponUsage->getCouponId() && $couponUsage->getTimesUsed() >= $coupon->getUsagePerCustomer() ) { - $msg .= $this->_formatMessage( + $msg = $this->_formatMessageWithContainer( 'You have already used your coupon.', $coupon->getUsagePerCustomer(), 'It may only be used %s time(s).' ); + Mage::throwException($msg); } } @@ -232,18 +240,15 @@ protected function _validateGeneral($rule, $coupon) if ($ruleId && $rule->getUsesPerCustomer()) { $ruleCustomer = Mage::getModel('salesrule/rule_customer'); $ruleCustomer->loadByCustomerRule($customerId, $ruleId); - if ($ruleCustomer->getId()) { - if ($ruleCustomer->getTimesUsed() >= $rule->getUsesPerCustomer()) { - $msg .= $this->_formatMessage( - 'You have already used your coupon.', - $rule->getUsesPerCustomer(), - 'It may only be used %s time(s).' - ); - } + if ($ruleCustomer->getId() && $ruleCustomer->getTimesUsed() >= $rule->getUsesPerCustomer()) { + $msg = $this->_formatMessageWithContainer( + 'You have already used your coupon.', + $rule->getUsesPerCustomer(), + 'It may only be used %s time(s).' + ); + Mage::throwException($msg); } } - - return $msg; } /** @@ -266,6 +271,7 @@ protected function _validateConditions($rule) return $this->_formatMessage($errorMsgs); } + return ''; } /** @@ -297,6 +303,7 @@ protected function _validateActions($rule) return $this->_formatMessage($errorMsgs); } + return ''; } /** @@ -304,27 +311,29 @@ protected function _validateActions($rule) * complete. * * @param array $condition + * @param bool $isNotFoundOperator + * * @return array * @throws Mage_Core_Exception */ - protected function _processCondition($condition = [], $isNotFoundOperator = false) + protected function _processCondition($condition = [], $isNotFoundOperator = false): array { $msgs = []; // TODO: we need to get a heading for aggregate here $msg = $this->_processRule($condition, $isNotFoundOperator); - if (!is_null($msg)) { + if ($msg !== null) { $msgs[] = $msg; } // aggregate conditions - if (isset($condition['aggregator']) && isset($condition['conditions'])) { + if (isset($condition['aggregator'], $condition['conditions'])) { $headingMsg = sprintf('
  • %s