public function catalogProductSaveAfter(Varien_Event_Observer $observer) { try { $productNew = $observer->getEvent()->getProduct(); if (!$productNew instanceof Mage_Catalog_Model_Product) { return; } // Update product name for listing log //-------------------- $nameOld = $this->_productNameOld; $nameNew = $productNew->getName(); if ($nameOld != $nameNew && $productNew->getStoreId() == 0) { Mage::getModel('M2ePro/ListingsLogs')->updateProductTitle($productNew->getId(), $nameNew); } //-------------------- // Get listing where is product $listingsIds = Mage::getResourceModel('M2ePro/Listings')->getListingsWhereIsProduct($productNew->getId()); if (count($listingsIds) > 0) { // Save global changes //-------------------- Mage::getModel('M2ePro/ProductsChanges')->updateAttribute($productNew->getId(), 'product_instance', 'any_old', 'any_new', Ess_M2ePro_Model_ProductsChanges::CREATOR_TYPE_OBSERVER); //-------------------- // Save changes for status //-------------------- $statusOld = (int) $this->_productStatusOld; $statusNew = (int) $productNew->getStatus(); $rez = Mage::getModel('M2ePro/ProductsChanges')->updateAttribute($productNew->getId(), 'status', $statusOld, $statusNew, Ess_M2ePro_Model_ProductsChanges::CREATOR_TYPE_OBSERVER); if ($rez !== false) { $statusOld = $statusOld == Mage_Catalog_Model_Product_Status::STATUS_ENABLED ? 'Enabled' : 'Disabled'; $statusNew = $statusNew == Mage_Catalog_Model_Product_Status::STATUS_ENABLED ? 'Enabled' : 'Disabled'; foreach ($listingsIds as $listingId) { Mage::getModel('M2ePro/ListingsLogs')->addProductMessage($listingId, $productNew->getId(), Ess_M2ePro_Model_ListingsLogs::INITIATOR_EXTENSION, NULL, Ess_M2ePro_Model_ListingsLogs::ACTION_CHANGE_PRODUCT_STATUS, Mage::getModel('M2ePro/LogsBase')->encodeDescription('From [%from%] to [%to%]', array('from' => $statusOld, 'to' => $statusNew)), Ess_M2ePro_Model_ListingsLogs::TYPE_NOTICE, Ess_M2ePro_Model_ListingsLogs::PRIORITY_LOW); } } //-------------------- // Save changes for price //-------------------- $priceOld = round((double) $this->_productPriceOld, 2); $priceNew = round((double) $productNew->getPrice(), 2); $rez = Mage::getModel('M2ePro/ProductsChanges')->updateAttribute($productNew->getId(), 'price', $priceOld, $priceNew, Ess_M2ePro_Model_ProductsChanges::CREATOR_TYPE_OBSERVER); if ($rez !== false) { foreach ($listingsIds as $listingId) { Mage::getModel('M2ePro/ListingsLogs')->addProductMessage($listingId, $productNew->getId(), Ess_M2ePro_Model_ListingsLogs::INITIATOR_EXTENSION, NULL, Ess_M2ePro_Model_ListingsLogs::ACTION_CHANGE_PRODUCT_PRICE, Mage::getModel('M2ePro/LogsBase')->encodeDescription('From [%from%] to [%to%]', array('!from' => $priceOld, '!to' => $priceNew)), Ess_M2ePro_Model_ListingsLogs::TYPE_NOTICE, Ess_M2ePro_Model_ListingsLogs::PRIORITY_LOW); } } //-------------------- // Save changes for special price //-------------------- $specialPriceOld = round((double) $this->_productSpecialPriceOld, 2); $specialPriceNew = round((double) $productNew->getSpecialPrice(), 2); $rez = Mage::getModel('M2ePro/ProductsChanges')->updateAttribute($productNew->getId(), 'special_price', $specialPriceOld, $specialPriceNew, Ess_M2ePro_Model_ProductsChanges::CREATOR_TYPE_OBSERVER); if ($rez !== false) { foreach ($listingsIds as $listingId) { Mage::getModel('M2ePro/ListingsLogs')->addProductMessage($listingId, $productNew->getId(), Ess_M2ePro_Model_ListingsLogs::INITIATOR_EXTENSION, NULL, Ess_M2ePro_Model_ListingsLogs::ACTION_CHANGE_PRODUCT_SPECIAL_PRICE, Mage::getModel('M2ePro/LogsBase')->encodeDescription('From [%from%] to [%to%]', array('!from' => $specialPriceOld, '!to' => $specialPriceNew)), Ess_M2ePro_Model_ListingsLogs::TYPE_NOTICE, Ess_M2ePro_Model_ListingsLogs::PRIORITY_LOW); } } //-------------------- // Save changes for special price from date //-------------------- $specialPriceFromDateOld = $this->_productSpecialPriceFromDate; $specialPriceFromDateNew = $productNew->getSpecialFromDate(); $rez = Mage::getModel('M2ePro/ProductsChanges')->updateAttribute($productNew->getId(), 'special_price_from_date', $specialPriceFromDateOld, $specialPriceFromDateNew, Ess_M2ePro_Model_ProductsChanges::CREATOR_TYPE_OBSERVER); if ($rez !== false) { if (is_null($specialPriceFromDateOld) || $specialPriceFromDateOld === false || $specialPriceFromDateOld == '') { $specialPriceFromDateOld = 'None'; } if (is_null($specialPriceFromDateNew) || $specialPriceFromDateNew === false || $specialPriceFromDateNew == '') { $specialPriceFromDateNew = 'None'; } foreach ($listingsIds as $listingId) { Mage::getModel('M2ePro/ListingsLogs')->addProductMessage($listingId, $productNew->getId(), Ess_M2ePro_Model_ListingsLogs::INITIATOR_EXTENSION, NULL, Ess_M2ePro_Model_ListingsLogs::ACTION_CHANGE_PRODUCT_SPECIAL_PRICE_FROM_DATE, Mage::getModel('M2ePro/LogsBase')->encodeDescription('From [%from%] to [%to%]', array('!from' => $specialPriceFromDateOld, '!to' => $specialPriceFromDateNew)), Ess_M2ePro_Model_ListingsLogs::TYPE_NOTICE, Ess_M2ePro_Model_ListingsLogs::PRIORITY_LOW); } } //-------------------- // Save changes for special price to date //-------------------- $specialPriceToDateOld = $this->_productSpecialPriceToDate; $specialPriceToDateNew = $productNew->getSpecialToDate(); $rez = Mage::getModel('M2ePro/ProductsChanges')->updateAttribute($productNew->getId(), 'special_price_to_date', $specialPriceToDateOld, $specialPriceToDateNew, Ess_M2ePro_Model_ProductsChanges::CREATOR_TYPE_OBSERVER); if ($rez !== false) { if (is_null($specialPriceToDateOld) || $specialPriceToDateOld === false || $specialPriceToDateOld == '') { $specialPriceToDateOld = 'None'; } if (is_null($specialPriceToDateNew) || $specialPriceToDateNew === false || $specialPriceToDateNew == '') { $specialPriceToDateNew = 'None'; } foreach ($listingsIds as $listingId) { Mage::getModel('M2ePro/ListingsLogs')->addProductMessage($listingId, $productNew->getId(), Ess_M2ePro_Model_ListingsLogs::INITIATOR_EXTENSION, NULL, Ess_M2ePro_Model_ListingsLogs::ACTION_CHANGE_PRODUCT_SPECIAL_PRICE_TO_DATE, Mage::getModel('M2ePro/LogsBase')->encodeDescription('From [%from%] to [%to%]', array('!from' => $specialPriceToDateOld, '!to' => $specialPriceToDateNew)), Ess_M2ePro_Model_ListingsLogs::TYPE_NOTICE, Ess_M2ePro_Model_ListingsLogs::PRIORITY_LOW); } } //-------------------- // Save changes for custom attributes //-------------------- $magentoProductModel = Mage::getModel('M2ePro/MagentoProduct')->setProduct($productNew); foreach ($this->_productCustomAttributes as $attribute) { $customAttributeOld = $attribute['value_old']; $customAttributeNew = $magentoProductModel->getAttributeValue($attribute['attribute']); $rez = Mage::getModel('M2ePro/ProductsChanges')->updateAttribute($productNew->getId(), $attribute['attribute'], $customAttributeOld, $customAttributeNew, Ess_M2ePro_Model_ProductsChanges::CREATOR_TYPE_OBSERVER); if ($rez !== false) { $customAttributeOld = $this->cutAttributeLength($customAttributeOld); $customAttributeNew = $this->cutAttributeLength($customAttributeNew); foreach ($attribute['listings'] as $listingId) { Mage::getModel('M2ePro/ListingsLogs')->addProductMessage($listingId, $productNew->getId(), Ess_M2ePro_Model_ListingsLogs::INITIATOR_EXTENSION, NULL, Ess_M2ePro_Model_ListingsLogs::ACTION_CHANGE_CUSTOM_ATTRIBUTE, Mage::getModel('M2ePro/LogsBase')->encodeDescription('Attribute "%attr%" from [%from%] to [%to%]', array('!attr' => $attribute['attribute'], '!from' => $customAttributeOld, '!to' => $customAttributeNew)), Ess_M2ePro_Model_ListingsLogs::TYPE_NOTICE, Ess_M2ePro_Model_ListingsLogs::PRIORITY_LOW); } } } //-------------------- // Update listings products variations //-------------------- foreach ($listingsIds as $listingId) { $listingsProductsTemp = Mage::getModel('M2ePro/Listings')->loadInstance($listingId)->getListingsProducts(true, array('product_id' => $productNew->getId())); foreach ($listingsProductsTemp as $listingProductTemp) { Mage::helper('M2ePro/Variations')->updateVariations($listingProductTemp); } } //-------------------- } // Synch changes for categories //-------------------- $categoriesNew = array_keys($productNew->getCategoryCollection()->exportToArray()); $addedCategories = array_diff($categoriesNew, $this->_productCategoriesOld); foreach ($addedCategories as $categoryId) { Ess_M2ePro_Model_Observer_Category::synchChangesWithListings($categoryId, $productNew->getStoreId(), array($productNew->getId()), array()); } $deletedCategories = array_diff($this->_productCategoriesOld, $categoriesNew); foreach ($deletedCategories as $categoryId) { Ess_M2ePro_Model_Observer_Category::synchChangesWithListings($categoryId, $productNew->getStoreId(), array(), array($productNew->getId())); } //-------------------- } catch (Exception $exception) { try { Mage::helper('M2ePro/Exception')->process($exception, true); } catch (Exception $exceptionTemp) { } return; } }
private function tryToPerformCategoriesActions($productNew) { $categoriesNew = $productNew->getCategoryIds(); $addedCategories = array_diff($categoriesNew, $this->_productCategoriesOld); foreach ($addedCategories as $categoryId) { Ess_M2ePro_Model_Observer_Category::synchChangesWithListings($categoryId, array($productNew), array()); } $deletedCategories = array_diff($this->_productCategoriesOld, $categoriesNew); foreach ($deletedCategories as $categoryId) { Ess_M2ePro_Model_Observer_Category::synchChangesWithListings($categoryId, array(), array($productNew)); } }