/** * After Save Attribute manipulation * * @param Mage_Catalog_Model_Product $object * @return Mage_Catalog_Model_Product_Attribute_Backend_Price */ public function afterSave($object) { $value = $object->getData($this->getAttribute()->getAttributeCode()); /** * Orig value is only for existing objects */ $oridData = $object->getOrigData(); $origValueExist = $oridData && array_key_exists($this->getAttribute()->getAttributeCode(), $oridData); if ($object->getStoreId() != 0 || !$value || $origValueExist) { return $this; } if ($this->getAttribute()->getIsGlobal() == Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE) { $baseCurrency = Mage::app()->getBaseCurrencyCode(); $storeIds = $object->getStoreIds(); if (is_array($storeIds)) { foreach ($storeIds as $storeId) { $storeCurrency = Mage::app()->getStore($storeId)->getBaseCurrencyCode(); if ($storeCurrency == $baseCurrency) { continue; } $rate = Mage::getModel('directory/currency')->load($baseCurrency)->getRate($storeCurrency); if (!$rate) { $rate = 1; } $newValue = $value * $rate; $object->addAttributeUpdate($this->getAttribute()->getAttributeCode(), $newValue, $storeId); } } } return $this; }
public function testSetOrigData() { $this->assertEmpty($this->_model->getOrigData()); $this->_model->setOrigData('key', 'value'); $this->assertEmpty($this->_model->getOrigData()); $storeId = Mage::app()->getStore()->getId(); Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID); try { $this->_model->setOrigData('key', 'value'); $this->assertEquals('value', $this->_model->getOrigData('key')); } catch (Exception $e) { Mage::app()->getStore()->setId($storeId); throw $e; } Mage::app()->getStore()->setId($storeId); }
/** * After Save Attribute manipulation * * @param Mage_Catalog_Model_Product $object * @return Mage_Catalog_Model_Product_Attribute_Backend_Groupprice_Abstract */ public function afterSave($object) { $websiteId = Mage::app()->getStore($object->getStoreId())->getWebsiteId(); $isGlobal = $this->getAttribute()->isScopeGlobal() || $websiteId == 0; $groupRows = $object->getData($this->getAttribute()->getName()); if (empty($groupRows)) { $this->_getResource()->deleteGroupData($object->getId()); return $this; } $old = array(); $new = array(); $origGroupRows = $object->getOrigData($this->getAttribute()->getName()); if (!is_array($origGroupRows)) { $origGroupRows = array(); } foreach ($origGroupRows as $data) { if ($data['website_id'] > 0 || $data['website_id'] == '0' && $isGlobal) { $key = join('-', array_merge(array($data['website_id'], $data['cust_group']), $this->_getAdditionalUniqueFields($data))); $old[$key] = $data; } } // prepare data for save foreach ($groupRows as $data) { $hasEmptyData = false; foreach ($this->_getAdditionalUniqueFields($data) as $field) { if (empty($field)) { $hasEmptyData = true; break; } } if ($hasEmptyData || !isset($data['cust_group']) || !empty($data['delete'])) { continue; } if ($this->getAttribute()->isScopeGlobal() && $data['website_id'] > 0) { continue; } if (!$isGlobal && (int) $data['website_id'] == 0) { continue; } if (!isset($data['website_id'])) { $data['website_id'] = 0; } $key = join('-', array_merge(array($data['website_id'], $data['cust_group']), $this->_getAdditionalUniqueFields($data))); $useForAllGroups = $data['cust_group'] == Mage_Customer_Model_Group::CUST_GROUP_ALL; $customerGroupId = !$useForAllGroups ? $data['cust_group'] : 0; $new[$key] = array_merge(array('website_id' => $data['website_id'], 'all_groups' => $useForAllGroups ? 1 : 0, 'customer_group_id' => $customerGroupId, 'value' => $data['value']), $this->_getAdditionalUniqueFields($data)); } $delete = array_diff_key($old, $new); $insert = array_diff_key($new, $old); $update = array_intersect_key($new, $old); $isChanged = false; $productId = $object->getId(); if (!empty($delete)) { foreach ($delete as $data) { $this->_getResource()->deleteGroupData($productId, null, $data['group_id']); $isChanged = true; } } if (!empty($insert)) { foreach ($insert as $data) { $group = new Varien_Object($data); $group->setEntityId($productId); $this->_getResource()->saveGroupData($group); $isChanged = true; } } if (!empty($update)) { foreach ($update as $k => $v) { if ($old[$k]['value'] != $v['value']) { $group = new Varien_Object(array('value_id' => $old[$k]['value_id'], 'value' => $v['value'])); $this->_getResource()->saveGroupData($group); $isChanged = true; } } } if ($isChanged) { $valueChangedKey = $this->getAttribute()->getName() . '_changed'; $object->setData($valueChangedKey, 1); } /*$websiteId = Mage::app()->getStore($object->getStoreId())->getWebsiteId(); $isGlobal = $this->getAttribute()->isScopeGlobal() || $websiteId == 0; $priceRows = $object->getData($this->getAttribute()->getName()); if (empty($priceRows)) { if ($isGlobal) { $this->_getResource()->deletePriceData($object->getId()); } else { $this->_getResource()->deletePriceData($object->getId(), $websiteId); } return $this; } $old = array(); $new = array(); // prepare original data for compare $origGroupPrices = $object->getOrigData($this->getAttribute()->getName()); if (!is_array($origGroupPrices)) { $origGroupPrices = array(); } foreach ($origGroupPrices as $data) { if ($data['website_id'] > 0 || ($data['website_id'] == '0' && $isGlobal)) { $key = join('-', array_merge( array($data['website_id'], $data['cust_group']), $this->_getAdditionalUniqueFields($data) )); $old[$key] = $data; } } // prepare data for save foreach ($priceRows as $data) { $hasEmptyData = false; foreach ($this->_getAdditionalUniqueFields($data) as $field) { if (empty($field)) { $hasEmptyData = true; break; } } if ($hasEmptyData || !isset($data['cust_group']) || !empty($data['delete'])) { continue; } if ($this->getAttribute()->isScopeGlobal() && $data['website_id'] > 0) { continue; } if (!$isGlobal && (int)$data['website_id'] == 0) { continue; } $key = join('-', array_merge( array($data['website_id'], $data['cust_group']), $this->_getAdditionalUniqueFields($data) )); $useForAllGroups = $data['cust_group'] == Mage_Customer_Model_Group::CUST_GROUP_ALL; $customerGroupId = !$useForAllGroups ? $data['cust_group'] : 0; $new[$key] = array_merge(array( 'website_id' => $data['website_id'], 'all_groups' => $useForAllGroups ? 1 : 0, 'customer_group_id' => $customerGroupId, 'value' => $data['price'], ), $this->_getAdditionalUniqueFields($data)); } $delete = array_diff_key($old, $new); $insert = array_diff_key($new, $old); $update = array_intersect_key($new, $old); $isChanged = false; $productId = $object->getId(); if (!empty($delete)) { foreach ($delete as $data) { $this->_getResource()->deletePriceData($productId, null, $data['price_id']); $isChanged = true; } } if (!empty($insert)) { foreach ($insert as $data) { $price = new Varien_Object($data); $price->setEntityId($productId); $this->_getResource()->savePriceData($price); $isChanged = true; } } if (!empty($update)) { foreach ($update as $k => $v) { if ($old[$k]['price'] != $v['value']) { $price = new Varien_Object(array( 'value_id' => $old[$k]['price_id'], 'value' => $v['value'] )); $this->_getResource()->savePriceData($price); $isChanged = true; } } } if ($isChanged) { $valueChangedKey = $this->getAttribute()->getName() . '_changed'; $object->setData($valueChangedKey, 1); }*/ return $this; }
/** * Save amounts data * * @param Mage_Catalog_Model_Product $object * @return Enterprise_GiftCard_Model_Attribute_Backend_Giftcard_Amounts */ public function afterSave($object) { $orig = $object->getOrigData($this->getAttribute()->getName()); $current = $object->getData($this->getAttribute()->getName()); if ($orig == $current) { return $this; } $this->_getResource()->deleteProductData($object, $this->getAttribute()); $rows = $object->getData($this->getAttribute()->getName()); if (!is_array($rows)) { return $this; } foreach ($rows as $row) { // Handle the case when model is saved whithout data received from user if ((!isset($row['price']) || empty($row['price'])) && !isset($row['value']) || !empty($row['delete'])) { continue; } $data = array(); $data['website_id'] = $row['website_id']; $data['value'] = isset($row['price']) ? $row['price'] : $row['value']; $data['attribute_id'] = $this->getAttribute()->getId(); $this->_getResource()->insertProductData($object, $data); } return $this; }
/** * Save amounts data * * @param Mage_Catalog_Model_Product $object * @return Enterprise_GiftCard_Model_Attribute_Backend_Giftcard_Amounts */ public function afterSave($object) { $orig = $object->getOrigData($this->getAttribute()->getName()); $current = $object->getData($this->getAttribute()->getName()); if ($orig == $current) { return $this; } $this->_getResource()->deleteProductData($object, $this->getAttribute()); $rows = $object->getData($this->getAttribute()->getName()); if (!is_array($rows)) { return $this; } foreach ($rows as $row) { if (empty($row['price']) || !empty($row['delete'])) { continue; } $data = array(); $data['website_id'] = $row['website_id']; $data['value'] = $row['price']; $data['attribute_id'] = $this->getAttribute()->getId(); $this->_getResource()->insertProductData($object, $data); } return $this; }
/** * @param Mage_Catalog_Model_Product $product * @return bool */ public function isProductNew(Mage_Catalog_Model_Product $product) { return $product->isObjectNew() || $product->getOrigData('sku') == '' && strlen($product->getData('sku')) > 0; }
/** * Save type related data * * @param Mage_Catalog_Model_Product $product * @return Mage_Catalog_Model_Product_Type_Abstract */ public function save($product) { if ($product->dataHasChangedFor('type_id') && $product->getOrigData('type_id')) { $oldTypeProduct = clone $product; $oldTypeInstance = Mage::getSingleton('Mage_Catalog_Model_Product_Type')->factory($oldTypeProduct->setTypeId($product->getOrigData('type_id'))); $oldTypeProduct->setTypeInstance($oldTypeInstance); $oldTypeInstance->deleteTypeSpecificData($oldTypeProduct); } return $this; }
/** * Unset attributes which are not passed in the API call * * @param array $productData * @param Mage_Catalog_Model_Product $product */ protected function _unsetOldData(array &$productData, $product) { $usedAttributes = array(); if (isset($data['attributes'])) { foreach ($data['attributes'] as $attributeData) { $usedAttributes[] = $attributeData['attribute_code']; } } $usedAttributes = array_merge(array_keys($productData), $usedAttributes); $origDataKeys = array_keys($product->getOrigData()); $origDataAttributeKeys = array_intersect($origDataKeys, $this->_getAttributes()); $keysToUnset = array_diff($origDataAttributeKeys, $usedAttributes, $this->_systemAttributes); foreach ($keysToUnset as $key) { $product->setData($key, null); } }
protected function _sendOfferMessages(Mage_Catalog_Model_Product $product, $performThoroughCheck = false) { if ($this->_isValueRegistered('xcom_offer_messages_sent', $product->getId())) { return; } $this->_registerValue('xcom_offer_messages_sent', $product->getId()); $cancelledSids = array(); $createdSids = array(); if ($performThoroughCheck || $product->getIsChangedWebsites()) { $oldWids = $this->_getRegisterValueForKey('xcom_offer_old_stores', $product->getId()); if (!isset($oldWids)) { $oldWids = array(); } $cancelledSids = array_diff($oldWids, $product->getStoreIds()); $createdSids = array_diff($product->getStoreIds(), $oldWids); } $updatedSids = array_diff($product->getStoreIds(), $cancelledSids, $createdSids); // There is a case where product was duplicated and just now a sku was filled in if ($product->getOrigData()) { $oldSku = $product->getOrigData('sku'); $sku = $product->getSku(); if (empty($oldSku) && !empty($sku)) { $createdSids = array_merge($createdSids, $updatedSids); $updatedSids = array(); } } if ($product->dataHasChangedFor('price')) { foreach ($updatedSids as $sid) { $offerInputData = array('product' => $product, 'store_id' => $sid); Mage::helper('xcom_xfabric')->send('com.x.webstore.v1/WebStoreOfferUpdate/WebStoreOfferPriceUpdated', $offerInputData); } } if ($this->_isValueRegistered('xcom_inventory_updated', $product->getId())) { foreach ($updatedSids as $sid) { $offerInputData = array('product' => $product, 'store_id' => $sid); Mage::helper('xcom_xfabric')->send('com.x.webstore.v1/WebStoreOfferUpdate/WebStoreOfferQuantityUpdated', $offerInputData); } } if ($product->dataHasChangedFor('visibility') || $product->dataHasChangedFor('status')) { foreach ($updatedSids as $sid) { $offerInputData = array('product' => $product, 'store_id' => $sid); Mage::helper('xcom_xfabric')->send('com.x.webstore.v1/WebStoreOfferUpdate/WebStoreOfferUpdated', $offerInputData); } } if ($product->dataHasChangedFor('url_key')) { foreach ($updatedSids as $sid) { Mage::getModel('xcom_chronicle/product_url_update')->setProductId($product->getEntityId())->setStoreId($sid)->setUrlPath($product->getUrlPath())->save(); } } foreach ($createdSids as $sid) { $offerInputData = array('product' => $product, 'store_id' => $sid); Mage::helper('xcom_xfabric')->send('com.x.webstore.v1/WebStoreOfferCreation/WebStoreOfferCreated', $offerInputData); } foreach ($cancelledSids as $sid) { $storeProduct = $this->_getRegisterValueForKey('xcom_offer_old_stores_products', $product->getId() . '_' . $sid); if (empty($storeProduct)) { $offerInputData = array('product' => $product, 'store_id' => $sid); } else { $offerInputData = array('product' => $storeProduct); } Mage::helper('xcom_xfabric')->send('com.x.webstore.v1/WebStoreOfferDeletion/WebStoreOfferDeleted', $offerInputData); } }
/** * Unset product attributes into the document collection. * * This function will provide an array containing attributes eventually setted to "use default value". * In this case we have to unset previous store values from MongoDB. * * @param Mage_Catalog_Model_Product $object The product to be saved * @param array $data The new data of the product to be saved * @param bool $isProductNew Indicates if the product is a new one or not * * @return Smile_MongoCatalog_Model_Resource_Override_Catalog_Product Self reference */ protected function _getUnsetAllAttributesData($object, $data, $isProductNew) { // Place at least id as saved field into the document (only mandatory field) $updateData = array(); foreach ($this->_attributesByCode as $attribute) { // If the attribute is empty we do not sore anyting into the DB // Attribute storage format have already been process in previous steps $value = $object->getData($attribute->getAttributeCode()); // Override : compare with orig data, this case occurs when switching value back to "use default" $origValue = $object->getOrigData($attribute->getAttributeCode()); if (!$attribute->isScopeGlobal()) { // This case should only happens on non-global attributes, but we better ensure // this case is a fallback to "use default" value on a product. We must unset previous store value // Value was existing (not null), but is now false, this is the case we need to unset if ($value == false && $origValue !== null && $origValue !== $value) { // By default => attribute data sould be stored into the product current store scope $storeId = 'attr_' . $object->getStoreId(); if (!is_string($value) || $value != '') { // Push saved values into the saved document $fieldName = $storeId . '.' . $attribute->getAttributeCode(); $updateData[$fieldName] = ""; // Always empty value for MongoDB $unset if ($attribute->isScopeWebsite() && $object->getStoreId() != Mage_Core_Model_App::ADMIN_STORE_ID) { // If attribute is website scope and edited for a store, // we should apply the value to all stores of the website $store = Mage::app()->getStore($object->getStoreId()); $websiteStoreIds = $store->getWebsite()->getStoreIds(); foreach ($websiteStoreIds as $storeId) { // Push saved values into the saved document $fieldName = 'attr_' . $storeId . '.' . $attribute->getAttributeCode(); $updateData[$fieldName] = ""; // Always empty value for MongoDB $unset } } } } } } return $updateData; }
/** * After save * * @param Mage_Catalog_Model_Product $object * * @return Innoexts_StorePricing_Model_Catalog_Product_Attribute_Backend_Tierprice */ public function afterSave($object) { $helper = $this->getStorePricingHelper(); $priceHelper = $helper->getProductPriceHelper(); $resource = $this->_getResource(); $objectId = $object->getId(); $storeId = $object->getStoreId(); $websiteId = $helper->getWebsiteIdByStoreId($storeId); $attribute = $this->getAttribute(); $attributeName = $attribute->getName(); $tierPrices = $object->getData($attributeName); if (empty($tierPrices)) { if ($priceHelper->isGlobalScope() || $websiteId == 0) { $resource->deletePriceData2($objectId); } else { if ($priceHelper->isWebsiteScope()) { $resource->deletePriceData2($objectId, $websiteId); } else { if ($priceHelper->isStoreScope()) { $resource->deletePriceData2($objectId, $websiteId, $storeId); } } } return $this; } $old = array(); $new = array(); $origTierPrices = $object->getOrigData($attributeName); if (!is_array($origTierPrices)) { $origTierPrices = array(); } foreach ($origTierPrices as $data) { if (!$this->validateData($data, $storeId, false, false, true)) { continue; } $key = $this->getDataKey($data); $old[$key] = $data; } foreach ($tierPrices as $data) { if (!$this->validateData($data, $storeId, true, true, true)) { continue; } $key = $this->getDataKey($data); $useForAllGroups = $data['cust_group'] == Mage_Customer_Model_Group::CUST_GROUP_ALL; $customerGroupId = !$useForAllGroups ? $data['cust_group'] : 0; $new[$key] = array('website_id' => $data['website_id'], 'store_id' => $data['store_id'], 'all_groups' => $useForAllGroups ? 1 : 0, 'customer_group_id' => $customerGroupId, 'qty' => $data['price_qty'], 'value' => $data['price']); } $delete = array_diff_key($old, $new); $insert = array_diff_key($new, $old); $update = array_intersect_key($new, $old); $isChanged = false; $productId = $objectId; if (!empty($delete)) { foreach ($delete as $data) { $resource->deletePriceData2($productId, null, null, $data['price_id']); $isChanged = true; } } if (!empty($insert)) { foreach ($insert as $data) { $price = new Varien_Object($data); $price->setEntityId($productId); $resource->savePriceData($price); $isChanged = true; } } if (!empty($update)) { foreach ($update as $k => $v) { if ($old[$k]['price'] != $v['value']) { $price = new Varien_Object(array('value_id' => $old[$k]['price_id'], 'value' => $v['value'])); $resource->savePriceData($price); $isChanged = true; } } } if ($isChanged) { $valueChangedKey = $attributeName . '_changed'; $object->setData($valueChangedKey, 1); } return $this; }
/** * Delete data specific for Downloadable product type * * @param Mage_Catalog_Model_Product $product */ public function deleteTypeSpecificData(Mage_Catalog_Model_Product $product) { if ($product->getOrigData('type_id') === Mage_Downloadable_Model_Product_Type::TYPE_DOWNLOADABLE) { $downloadableData = $product->getDownloadableData(); $sampleItems = array(); if (isset($downloadableData['sample'])) { foreach ($downloadableData['sample'] as $sample) { $sampleItems[] = $sample['sample_id']; } } if ($sampleItems) { Mage::getResourceModel('Mage_Downloadable_Model_Resource_Sample')->deleteItems($sampleItems); } $linkItems = array(); if (isset($downloadableData['link'])) { foreach ($downloadableData['link'] as $link) { $linkItems[] = $link['link_id']; } } if ($linkItems) { Mage::getResourceModel('Mage_Downloadable_Model_Resource_Link')->deleteItems($linkItems); } } }