/app/code/community/Ess/M2ePro/Model/Observer/Category.php

https://github.com/expressdecor/Expressdecor · PHP · 256 lines · 151 code · 54 blank · 51 comment · 34 complexity · 8d6ac7a207f924c019b109604cc5d35f MD5 · raw file

  1. <?php
  2. /*
  3. * @copyright Copyright (c) 2011 by ESS-UA.
  4. */
  5. class Ess_M2ePro_Model_Observer_Category
  6. {
  7. //####################################
  8. public function catalogCategoryChangeProducts(Varien_Event_Observer $observer)
  9. {
  10. try {
  11. // Get category data
  12. //---------------------------
  13. $categoryId = $observer->getData('category')->getId();
  14. //---------------------------
  15. // Get changes into categories
  16. //---------------------------
  17. $changedProductsIds = $observer->getData('product_ids');
  18. if (count($changedProductsIds) == 0) {
  19. return;
  20. }
  21. $tempArray = $observer->getData('category')->getData('posted_products');
  22. $postedProductsIds = array();
  23. foreach ($tempArray as $key => $value) {
  24. $postedProductsIds[] = $key;
  25. }
  26. $addedProducts = array();
  27. $deletedProducts = array();
  28. foreach ($changedProductsIds as $productId) {
  29. if (in_array($productId,$postedProductsIds)) {
  30. $addedProducts[] = $productId;
  31. } else {
  32. $deletedProducts[] = $productId;
  33. }
  34. }
  35. if (count($addedProducts) == 0 && count($deletedProducts) == 0) {
  36. return;
  37. }
  38. //---------------------------
  39. // Make changes with listings
  40. self::synchChangesWithListings($categoryId,$addedProducts,$deletedProducts);
  41. } catch (Exception $exception) {
  42. Mage::helper('M2ePro/Exception')->process($exception,true);
  43. return;
  44. }
  45. }
  46. //-----------------------------------
  47. public static function synchChangesWithListings($categoryId,
  48. $addedProducts,
  49. $deletedProducts)
  50. {
  51. try {
  52. // Check listings categories
  53. //---------------------------
  54. $listingsCategories = Mage::getModel('M2ePro/Listing_Category')
  55. ->getCollection()
  56. ->addFieldToFilter('category_id', $categoryId)
  57. ->toArray();
  58. if ($listingsCategories['totalRecords'] == 0) {
  59. return;
  60. }
  61. //---------------------------
  62. // Get all related listings
  63. //---------------------------
  64. $listingsIds = array();
  65. foreach ($listingsCategories['items'] as $listingCategory) {
  66. $listingsIds[] = $listingCategory['listing_id'];
  67. }
  68. $listingsIds = array_unique($listingsIds);
  69. if (count($listingsIds) == 0) {
  70. return;
  71. }
  72. $listingsModels = array();
  73. foreach ($listingsIds as $listingId) {
  74. /** @var $tempModel Ess_M2ePro_Model_Listing */
  75. $tempModel = Mage::getModel('M2ePro/Listing')->loadInstance($listingId);
  76. if (!$tempModel->isSourceCategories()) {
  77. continue;
  78. }
  79. /** @var $listingStoreObject Mage_Core_Model_Store */
  80. $listingStoreObject = Mage::getModel('core/store')->load($tempModel->getStoreId());
  81. $tempModel->setData('store_website_id',$listingStoreObject->getWebsite()->getId());
  82. $listingsModels[] = $tempModel;
  83. }
  84. if (count($listingsModels) == 0) {
  85. return;
  86. }
  87. //---------------------------
  88. // Add new products
  89. //---------------------------
  90. foreach ($addedProducts as $product) {
  91. if (!($product instanceof Mage_Catalog_Model_Product)) {
  92. $product = Mage::getModel('catalog/product')->load((int)$product);
  93. }
  94. $productId = (int)$product->getId();
  95. if ((bool)Mage::helper('M2ePro/Module')->getConfig()
  96. ->getGroupValue('/listings/categories_add_actions/', 'ignore_not_visible') &&
  97. (int)$product->getVisibility() == Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE) {
  98. continue;
  99. }
  100. foreach ($listingsModels as $listingModel) {
  101. /** @var $listingModel Ess_M2ePro_Model_Listing */
  102. if ((int)$listingModel->getData('store_website_id') > 0 &&
  103. !in_array($listingModel->getData('store_website_id'),$product->getWebsiteIds())) {
  104. continue;
  105. }
  106. // Cancel when auto add none set
  107. //------------------------------
  108. if ($listingModel->getData('categories_add_action') ==
  109. Ess_M2ePro_Model_Listing::CATEGORIES_ADD_ACTION_NONE) {
  110. continue;
  111. }
  112. //------------------------------
  113. // Only add product
  114. //------------------------------
  115. if ($listingModel->getData('categories_add_action') ==
  116. Ess_M2ePro_Model_Listing::CATEGORIES_ADD_ACTION_ADD) {
  117. $listingModel->addProduct(
  118. $product instanceof Mage_Catalog_Model_Product ? $product : $productId
  119. );
  120. }
  121. //------------------------------
  122. // Add product and list
  123. //------------------------------
  124. if ($listingModel->getData('categories_add_action') ==
  125. Ess_M2ePro_Model_Listing::CATEGORIES_ADD_ACTION_ADD_LIST) {
  126. /** @var $listingProduct Ess_M2ePro_Model_Listing_Product */
  127. $listingProduct = $listingModel->addProduct(
  128. $product instanceof Mage_Catalog_Model_Product ? $product : $productId
  129. );
  130. if (!($listingProduct instanceof Ess_M2ePro_Model_Listing_Product)) {
  131. $tempListingsProducts = $listingModel->getProducts(true,array('product_id'=>$productId));
  132. count($tempListingsProducts) > 0 && $listingProduct = array_shift($tempListingsProducts);
  133. }
  134. if ($listingProduct instanceof Ess_M2ePro_Model_Listing_Product) {
  135. $paramsTemp = array();
  136. $paramsTemp['status_changer'] = Ess_M2ePro_Model_Listing_Product::STATUS_CHANGER_OBSERVER;
  137. $listingProduct->isListable() && $listingProduct->listAction($paramsTemp);
  138. }
  139. }
  140. //------------------------------
  141. }
  142. }
  143. //---------------------------
  144. // Delete products
  145. //---------------------------
  146. foreach ($deletedProducts as $product) {
  147. if (!($product instanceof Mage_Catalog_Model_Product)) {
  148. $product = Mage::getModel('catalog/product')->load((int)$product);
  149. }
  150. $productId = (int)$product->getId();
  151. foreach ($listingsModels as $listingModel) {
  152. /** @var $listingModel Ess_M2ePro_Model_Listing */
  153. if (!in_array($listingModel->getData('store_website_id'),$product->getWebsiteIds())) {
  154. continue;
  155. }
  156. // Cancel when auto delete none set
  157. //------------------------------
  158. if ($listingModel->getData('categories_delete_action') ==
  159. Ess_M2ePro_Model_Listing::CATEGORIES_DELETE_ACTION_NONE) {
  160. continue;
  161. }
  162. //------------------------------
  163. // Find needed product
  164. //------------------------------
  165. $listingsProducts = $listingModel->getProducts(true,array('product_id'=>$productId));
  166. if (count($listingsProducts) <= 0) {
  167. continue;
  168. }
  169. $listingProduct = array_shift($listingsProducts);
  170. if (!($listingProduct instanceof Ess_M2ePro_Model_Listing_Product)) {
  171. continue;
  172. }
  173. //------------------------------
  174. // Only stop product
  175. //------------------------------
  176. if ($listingModel->getData('categories_delete_action') ==
  177. Ess_M2ePro_Model_Listing::CATEGORIES_DELETE_ACTION_STOP) {
  178. $paramsTemp = array();
  179. $paramsTemp['status_changer'] = Ess_M2ePro_Model_Listing_Product::STATUS_CHANGER_OBSERVER;
  180. $listingProduct->isStoppable() && $listingProduct->stopAction($paramsTemp);
  181. }
  182. //------------------------------
  183. // Stop product on marketplace and remove
  184. //------------------------------
  185. if ($listingModel->getData('categories_delete_action') ==
  186. Ess_M2ePro_Model_Listing::CATEGORIES_DELETE_ACTION_STOP_REMOVE) {
  187. $paramsTemp = array();
  188. $paramsTemp['remove'] = true;
  189. $paramsTemp['status_changer'] = Ess_M2ePro_Model_Listing_Product::STATUS_CHANGER_OBSERVER;
  190. $listingProduct->stopAction($paramsTemp);
  191. }
  192. //------------------------------
  193. }
  194. }
  195. //---------------------------
  196. } catch (Exception $exception) {
  197. Mage::helper('M2ePro/Exception')->process($exception,true);
  198. return;
  199. }
  200. }
  201. //####################################
  202. }