00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_ProductAlert_Model_Observer
00036 {
00037
00038
00039
00040 const XML_PATH_ERROR_TEMPLATE = 'catalog/productalert_cron/error_email_template';
00041
00042
00043
00044
00045 const XML_PATH_ERROR_IDENTITY = 'catalog/productalert_cron/error_email_identity';
00046
00047
00048
00049
00050 const XML_PATH_ERROR_RECIPIENT = 'catalog/productalert_cron/error_email';
00051
00052
00053
00054
00055
00056 const XML_PATH_PRICE_ALLOW = 'catalog/productalert/allow_price';
00057
00058
00059
00060
00061
00062 const XML_PATH_STOCK_ALLOW = 'catalog/productalert/allow_stock';
00063
00064
00065
00066
00067
00068
00069 protected $_websites;
00070
00071
00072
00073
00074
00075
00076 protected $_errors = array();
00077
00078
00079
00080
00081
00082
00083 protected function _getWebsites()
00084 {
00085 if (is_null($this->_websites)) {
00086 try {
00087 $this->_websites = Mage::app()->getWebsites();
00088 }
00089 catch (Exception $e) {
00090 $this->_errors[] = $e->getMessage();
00091 }
00092 }
00093 return $this->_websites;
00094 }
00095
00096
00097
00098
00099
00100
00101
00102 protected function _processPrice(Mage_ProductAlert_Model_Email $email)
00103 {
00104 $email->setType('price');
00105 foreach ($this->_getWebsites() as $website) {
00106
00107
00108 if (!$website->getDefaultGroup() || !$website->getDefaultGroup()->getDefaultStore()) {
00109 continue;
00110 }
00111 if (!Mage::getStoreConfig(self::XML_PATH_PRICE_ALLOW, $website->getDefaultGroup()->getDefaultStore()->getId())) {
00112 continue;
00113 }
00114 try {
00115 $collection = Mage::getModel('productalert/price')
00116 ->getCollection()
00117 ->addWebsiteFilter($website->getId())
00118 ->setCustomerOrder();
00119 }
00120 catch (Exception $e) {
00121 $this->_errors[] = $e->getMessage();
00122 return $this;
00123 }
00124
00125 $previousCustomer = null;
00126 $email->setWebsite($website);
00127 foreach ($collection as $alert) {
00128 try {
00129 if (!$previousCustomer || $previousCustomer->getId() != $alert->getCustomerId()) {
00130 $customer = Mage::getModel('customer/customer')->load($alert->getCustomerId());
00131 if ($previousCustomer) {
00132 $email->send();
00133 }
00134 if (!$customer) {
00135 continue;
00136 }
00137 $previousCustomer = $customer;
00138 $email->clean();
00139 $email->setCustomer($customer);
00140 }
00141 else {
00142 $customer = $previousCustomer;
00143 }
00144
00145 $product = Mage::getModel('catalog/product')
00146 ->setStoreId($website->getDefaultStore()->getId())
00147 ->load($alert->getProductId());
00148 if (!$product) {
00149 continue;
00150 }
00151 $product->setCustomerGroupId($customer->getGroupId());
00152 if ($alert->getPrice() > $product->getFinalPrice()) {
00153 $email->addPriceProduct($product);
00154
00155 $alert->setPrice($product->getFinalPrice());
00156 $alert->setLastSendDate(Mage::getModel('core/date')->gmtDate());
00157 $alert->setSendCount($alert->getSendCount() + 1);
00158 $alert->setStatus(1);
00159 $alert->save();
00160 }
00161 }
00162 catch (Exception $e) {
00163 $this->_errors[] = $e->getMessage();
00164 }
00165 }
00166 if ($previousCustomer) {
00167 try {
00168 $email->send();
00169 }
00170 catch (Exception $e) {
00171 $this->_errors[] = $e->getMessage();
00172 }
00173 }
00174 }
00175 return $this;
00176 }
00177
00178
00179
00180
00181
00182
00183
00184 protected function _processStock(Mage_ProductAlert_Model_Email $email)
00185 {
00186 $email->setType('stock');
00187
00188 foreach ($this->_getWebsites() as $website) {
00189
00190
00191 if (!$website->getDefaultGroup() || !$website->getDefaultGroup()->getDefaultStore()) {
00192 continue;
00193 }
00194 if (!Mage::getStoreConfig(self::XML_PATH_STOCK_ALLOW, $website->getDefaultGroup()->getDefaultStore()->getId())) {
00195 continue;
00196 }
00197 try {
00198 $collection = Mage::getModel('productalert/stock')
00199 ->getCollection()
00200 ->addWebsiteFilter($website->getId())
00201 ->addStatusFilter(0)
00202 ->setCustomerOrder();
00203 }
00204 catch (Exception $e) {
00205 $this->_errors[] = $e->getMessage();
00206 return $this;
00207 }
00208
00209 $previousCustomer = null;
00210 $email->setWebsite($website);
00211 foreach ($collection as $alert) {
00212 try {
00213 if (!$previousCustomer || $previousCustomer->getId() != $alert->getCustomerId()) {
00214 $customer = Mage::getModel('customer/customer')->load($alert->getCustomerId());
00215 if ($previousCustomer) {
00216 $email->send();
00217 }
00218 if (!$customer) {
00219 continue;
00220 }
00221 $previousCustomer = $customer;
00222 $email->clean();
00223 $email->setCustomer($customer);
00224 }
00225 else {
00226 $customer = $previousCustomer;
00227 }
00228
00229 $product = Mage::getModel('catalog/product')
00230 ->setStoreId($website->getDefaultStore()->getId())
00231 ->load($alert->getProductId());
00232
00233 if (!$product) {
00234 continue;
00235 }
00236
00237 $product->setCustomerGroupId($customer->getGroupId());
00238
00239 if ($product->isSalable()) {
00240 $email->addStockProduct($product);
00241
00242 $alert->setSendDate(Mage::getModel('core/date')->gmtDate());
00243 $alert->setSendCount($alert->getSendCount() + 1);
00244 $alert->setStatus(1);
00245 $alert->save();
00246 }
00247 }
00248 catch (Exception $e) {
00249 $this->_errors[] = $e->getMessage();
00250 }
00251 }
00252
00253 if ($previousCustomer) {
00254 try {
00255 $email->send();
00256 }
00257 catch (Exception $e) {
00258 $this->_errors[] = $e->getMessage();
00259 }
00260 }
00261 }
00262
00263 return $this;
00264 }
00265
00266
00267
00268
00269
00270
00271 protected function _sendErrorEmail()
00272 {
00273 if (count($this->_errors)) {
00274 if (!Mage::getStoreConfig(self::XML_PATH_ERROR_TEMPLATE)) {
00275 return $this;
00276 }
00277
00278 $translate = Mage::getSingleton('core/translate');
00279
00280 $translate->setTranslateInline(false);
00281
00282 $emailTemplate = Mage::getModel('core/email_template');
00283
00284 $emailTemplate->setDesignConfig(array('area' => 'backend'))
00285 ->sendTransactional(
00286 Mage::getStoreConfig(self::XML_PATH_ERROR_TEMPLATE),
00287 Mage::getStoreConfig(self::XML_PATH_ERROR_IDENTITY),
00288 Mage::getStoreConfig(self::XML_PATH_ERROR_RECIPIENT),
00289 null,
00290 array('warnings' => join("\n", $this->_errors))
00291 );
00292
00293 $translate->setTranslateInline(true);
00294 $this->_errors[] = array();
00295 }
00296 return $this;
00297 }
00298
00299
00300
00301
00302
00303
00304 public function process()
00305 {
00306 $email = Mage::getModel('productalert/email');
00307
00308 $this->_processPrice($email);
00309 $this->_processStock($email);
00310 $this->_sendErrorEmail();
00311
00312 return $this;
00313 }
00314 }