/** * Save title and price of link item * * @param \Magento\Downloadable\Model\Link $linkObject * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function saveItemTitleAndPrice($linkObject) { $connection = $this->getConnection(); $linkTitleTable = $this->getTable('downloadable_link_title'); $linkPriceTable = $this->getTable('downloadable_link_price'); $select = $connection->select()->from($this->getTable('downloadable_link_title'))->where('link_id=:link_id AND store_id=:store_id'); $bind = [':link_id' => $linkObject->getId(), ':store_id' => (int) $linkObject->getStoreId()]; if ($connection->fetchOne($select, $bind)) { $where = ['link_id = ?' => $linkObject->getId(), 'store_id = ?' => (int) $linkObject->getStoreId()]; if ($linkObject->getUseDefaultTitle()) { $connection->delete($linkTitleTable, $where); } else { $insertData = ['title' => $linkObject->getTitle()]; $connection->update($linkTitleTable, $insertData, $where); } } else { if (!$linkObject->getUseDefaultTitle()) { $connection->insert($linkTitleTable, ['link_id' => $linkObject->getId(), 'store_id' => (int) $linkObject->getStoreId(), 'title' => $linkObject->getTitle()]); } } $select = $connection->select()->from($linkPriceTable)->where('link_id=:link_id AND website_id=:website_id'); $bind = [':link_id' => $linkObject->getId(), ':website_id' => (int) $linkObject->getWebsiteId()]; if ($connection->fetchOne($select, $bind)) { $where = ['link_id = ?' => $linkObject->getId(), 'website_id = ?' => $linkObject->getWebsiteId()]; if ($linkObject->getUseDefaultPrice()) { $connection->delete($linkPriceTable, $where); } else { $connection->update($linkPriceTable, ['price' => $linkObject->getPrice()], $where); } } else { if (!$linkObject->getUseDefaultPrice()) { $dataToInsert[] = ['link_id' => $linkObject->getId(), 'website_id' => (int) $linkObject->getWebsiteId(), 'price' => (double) $linkObject->getPrice()]; if ($linkObject->getOrigData('link_id') != $linkObject->getLinkId()) { $_isNew = true; } else { $_isNew = false; } if ($linkObject->getWebsiteId() == 0 && $_isNew && !$this->_catalogData->isPriceGlobal()) { $websiteIds = $linkObject->getProductWebsiteIds(); foreach ($websiteIds as $websiteId) { $baseCurrency = $this->_configuration->getValue(\Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE, 'default'); $websiteCurrency = $this->_storeManager->getWebsite($websiteId)->getBaseCurrencyCode(); if ($websiteCurrency == $baseCurrency) { continue; } $rate = $this->_createCurrency()->load($baseCurrency)->getRate($websiteCurrency); if (!$rate) { $rate = 1; } $newPrice = $linkObject->getPrice() * $rate; $dataToInsert[] = ['link_id' => $linkObject->getId(), 'website_id' => (int) $websiteId, 'price' => $newPrice]; } } $connection->insertMultiple($linkPriceTable, $dataToInsert); } } return $this; }