/** * Generate permanent rewrites based on current rewrites * * @param int $storeId * @return array */ protected function generateRewritesBasedOnCurrentRewrites($storeId) { $urls = []; foreach ($this->urlMatcher->findAllByFilter($this->createCurrentUrlRewritesFilter($storeId)) as $url) { $targetPath = null; if ($url->getRedirectType()) { $targetPath = str_replace($this->product->getOrigData('url_key'), $this->product->getData('url_key'), $url->getTargetPath()); $redirectType = $url->getRedirectType(); } elseif ($this->product->getData('save_rewrites_history')) { $targetPath = str_replace($this->product->getOrigData('url_key'), $this->product->getData('url_key'), $url->getRequestPath()); $redirectType = OptionProvider::PERMANENT; } if ($targetPath && $url->getRequestPath() != $targetPath) { $urls[] = $this->createUrlRewrite($storeId, $url->getRequestPath(), $targetPath, $redirectType); } } return $urls; }
/** * After Save Attribute manipulation * * @param \Magento\Catalog\Model\Product $object * @return $this */ 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() == \Magento\Catalog\Model\Resource\Eav\Attribute::SCOPE_WEBSITE) { $baseCurrency = $this->_config->getValue(\Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE, 'default'); $storeIds = $object->getStoreIds(); if (is_array($storeIds)) { foreach ($storeIds as $storeId) { $storeCurrency = $this->_storeManager->getStore($storeId)->getBaseCurrencyCode(); if ($storeCurrency == $baseCurrency) { continue; } $rate = $this->_currencyFactory->create()->load($baseCurrency)->getRate($storeCurrency); if (!$rate) { $rate = 1; } $newValue = $value * $rate; $object->addAttributeUpdate($this->getAttribute()->getAttributeCode(), $newValue, $storeId); } } } return $this; }
/** * Delete data specific for Downloadable product type * * @param \Magento\Catalog\Model\Product $product * @return void */ public function deleteTypeSpecificData(\Magento\Catalog\Model\Product $product) { if ($product->getOrigData('type_id') === \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE) { $downloadableData = $product->getDownloadableData(); $sampleItems = []; if (isset($downloadableData['sample'])) { foreach ($downloadableData['sample'] as $sample) { $sampleItems[] = $sample['sample_id']; } } if ($sampleItems) { $this->_sampleResFactory->create()->deleteItems($sampleItems); } $linkItems = []; if (isset($downloadableData['link'])) { foreach ($downloadableData['link'] as $link) { $linkItems[] = $link['link_id']; } } if ($linkItems) { $this->_linkResource->deleteItems($linkItems); } } }
/** * After Save Attribute manipulation * * @param \Magento\Catalog\Model\Product $object * @return $this */ public function afterSave($object) { $websiteId = $this->_storeManager->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'] == CustomerGroupServiceInterface::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 \Magento\Framework\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 \Magento\Framework\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; }
/** * @magentoAppArea adminhtml */ public function testSetOrigDataBackend() { $this->assertEmpty($this->_model->getOrigData()); $this->_model->setOrigData('key', 'value'); $this->assertEquals('value', $this->_model->getOrigData('key')); }
/** * Save type related data * * @param \Magento\Catalog\Model\Product $product * @return $this */ public function save($product) { if ($product->dataHasChangedFor('type_id') && $product->getOrigData('type_id')) { $oldTypeProduct = clone $product; $oldTypeInstance = $this->_catalogProductType->factory($oldTypeProduct->setTypeId($product->getOrigData('type_id'))); $oldTypeProduct->setTypeInstance($oldTypeInstance); $oldTypeInstance->deleteTypeSpecificData($oldTypeProduct); } return $this; }
/** * {@inheritdoc} */ public function getOrigData($key = null) { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getOrigData'); if (!$pluginInfo) { return parent::getOrigData($key); } else { return $this->___callPlugins('getOrigData', func_get_args(), $pluginInfo); } }