diff --git a/app/code/Magento/BundleImportExport/Model/Export/RowCustomizer.php b/app/code/Magento/BundleImportExport/Model/Export/RowCustomizer.php index 376de359f2530e6fe0ca271cb42508bcce56792c..24ccbbee23649908163750b13795d9302ead3b1a 100644 --- a/app/code/Magento/BundleImportExport/Model/Export/RowCustomizer.php +++ b/app/code/Magento/BundleImportExport/Model/Export/RowCustomizer.php @@ -109,7 +109,7 @@ class RowCustomizer implements RowCustomizerInterface /** * @var \Magento\Bundle\Model\ResourceModel\Option\Collection[] */ - private $optionsCollection = []; + private $optionCollections = []; /** * @var array @@ -130,9 +130,9 @@ class RowCustomizer implements RowCustomizerInterface * @param StoreManagerInterface $storeManager * @throws \RuntimeException */ - public function __construct(StoreManagerInterface $storeManager = null) + public function __construct(StoreManagerInterface $storeManager) { - $this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(StoreManagerInterface::class); + $this->storeManager = $storeManager; } /** @@ -237,10 +237,10 @@ class RowCustomizer implements RowCustomizerInterface */ protected function getFormattedBundleOptionValues($product) { - $optionsCollection = $this->getProductOptionsCollection($product); + $optionCollections = $this->getProductOptionCollections($product); $bundleData = ''; $optionTitles = $this->getBundleOptionTitles($product); - foreach ($optionsCollection->getItems() as $option) { + foreach ($optionCollections->getItems() as $option) { $optionValues = $this->getFormattedOptionValues($option, $optionTitles); $bundleData .= $this->getFormattedBundleSelections( $optionValues, @@ -418,6 +418,7 @@ class RowCustomizer implements RowCustomizerInterface /** * Get product options titles. + * * Values for all store views (default) should be specified with 'name' key. * If user want to specify value or change existing for non default store views it should be specified with * 'name_' prefix and needed store view suffix. @@ -429,20 +430,20 @@ class RowCustomizer implements RowCustomizerInterface * @param \Magento\Catalog\Model\Product $product $product * @return array */ - private function getBundleOptionTitles($product) + private function getBundleOptionTitles($product): array { - $optionsCollection = $this->getProductOptionsCollection($product); + $optionCollections = $this->getProductOptionCollections($product); $optionsTitles = []; /** @var \Magento\Bundle\Model\Option $option */ - foreach ($optionsCollection->getItems() as $option) { + foreach ($optionCollections->getItems() as $option) { $optionsTitles[$option->getId()]['name'] = $option->getTitle(); } $storeIds = $product->getStoreIds(); if (array_count_values($storeIds) > 1) { foreach ($storeIds as $storeId) { - $optionsCollection = $this->getProductOptionsCollection($product, $storeId); + $optionCollections = $this->getProductOptionCollections($product, $storeId); /** @var \Magento\Bundle\Model\Option $option */ - foreach ($optionsCollection->getItems() as $option) { + foreach ($optionCollections->getItems() as $option) { $optionTitle = $option->getTitle(); if ($optionsTitles[$option->getId()]['name'] != $optionTitle) { $optionsTitles[$option->getId()]['name_' . $this->getStoreCodeById($storeId)] = $optionTitle; @@ -455,33 +456,37 @@ class RowCustomizer implements RowCustomizerInterface /** * Get product options collection by provided product model. + * * Set given store id to the product if it was defined (default store id will be set if was not). * * @param \Magento\Catalog\Model\Product $product $product * @param integer $storeId * @return \Magento\Bundle\Model\ResourceModel\Option\Collection */ - private function getProductOptionsCollection($product, $storeId = \Magento\Store\Model\Store::DEFAULT_STORE_ID) - { + private function getProductOptionCollections( + $product, + $storeId = \Magento\Store\Model\Store::DEFAULT_STORE_ID + ): \Magento\Bundle\Model\ResourceModel\Option\Collection { $productSku = $product->getSku(); - if (!isset($this->optionsCollection[$productSku][$storeId])) { + if (!isset($this->optionCollections[$productSku][$storeId])) { $product->unsetData($this->optionCollectionCacheKey); $product->setStoreId($storeId); - $this->optionsCollection[$productSku][$storeId] = $product->getTypeInstance() - ->getOptionsCollection($product) + $this->optionCollections[$productSku][$storeId] = $product->getTypeInstance() + ->getOptionCollections($product) ->setOrder('position', Collection::SORT_ORDER_ASC); } - return $this->optionsCollection[$productSku][$storeId]; + return $this->optionCollections[$productSku][$storeId]; } /** * Retrieve store code by it's ID. + * * Collect store id in $storeIdToCode[] private variable if it was not initialized earlier. * * @param $storeId - * @return mixed + * @return string */ - private function getStoreCodeById($storeId) + private function getStoreCodeById($storeId): string { if (!isset($this->storeIdToCode[$storeId])) { $this->storeIdToCode[$storeId] = $this->storeManager->getStore($storeId)->getCode(); diff --git a/app/code/Magento/BundleImportExport/Model/Import/Product/Type/Bundle.php b/app/code/Magento/BundleImportExport/Model/Import/Product/Type/Bundle.php index c0e17433e43332064d3bf2988741c34f8368ff4b..b7f70d18f5c85209da0f64579778774b54e2b9b8 100644 --- a/app/code/Magento/BundleImportExport/Model/Import/Product/Type/Bundle.php +++ b/app/code/Magento/BundleImportExport/Model/Import/Product/Type/Bundle.php @@ -155,6 +155,7 @@ class Bundle extends \Magento\CatalogImportExport\Model\Import\Product\Type\Abst * @param \Magento\Framework\EntityManager\MetadataPool|null $metadataPool * @param Bundle\RelationsDataSaver|null $relationsDataSaver * @param StoreManagerInterface $storeManager + * @throws \Magento\Framework\Exception\LocalizedException * @throws \RuntimeException */ public function __construct( @@ -288,7 +289,7 @@ class Bundle extends \Magento\CatalogImportExport\Model\Import\Product\Type\Abst foreach ($optionNames as $optionName) { preg_match($pattern, $optionName, $storeCodes); $storeCode = array_pop($storeCodes); - $storeId = !$storeCode ? $storeId : $this->getStoreIdByCode($storeCode); + $storeId = $storeCode ? $this->getStoreIdByCode($storeCode) : $storeId; $optionValues[] = [ 'option_id' => $optionId, 'parent_product_id' => $option['parent_id'], @@ -728,7 +729,7 @@ class Bundle extends \Magento\CatalogImportExport\Model\Import\Product\Type\Abst * @param string|null $storeCode * @return int */ - private function getStoreIdByCode($storeCode) + private function getStoreIdByCode($storeCode): int { if (!isset($this->storeIdToCode[$storeCode])) { /** @var $store \Magento\Store\Model\Store */ diff --git a/app/code/Magento/CatalogImportExport/Model/Export/Product.php b/app/code/Magento/CatalogImportExport/Model/Export/Product.php index cead77d7f7134ef9ea06bfc8807121dae4f518b5..af14ef0bf6e8e2bf98fefd3443750f50144514b8 100644 --- a/app/code/Magento/CatalogImportExport/Model/Export/Product.php +++ b/app/code/Magento/CatalogImportExport/Model/Export/Product.php @@ -1347,6 +1347,7 @@ class Product extends \Magento\ImportExport\Model\Export\Entity\AbstractEntity /** * Collect custom options data for products that will be exported. + * * Option name and type will be collected for all store views, all other data (which can't be changed on store view * level will be collected for DEFAULT_STORE_ID only. * Store view specified data will be saved to the additional store view row. @@ -1381,7 +1382,7 @@ class Product extends \Magento\ImportExport\Model\Export\Entity\AbstractEntity $productId = $option['product_id']; $row['name'] = $option['title']; $row['type'] = $option['type']; - if (Store::DEFAULT_STORE_ID == $storeId) { + if (Store::DEFAULT_STORE_ID === $storeId) { $row['required'] = $option['is_require']; $row['price'] = $option['price']; $row['price_type'] = ($option['price_type'] == 'percent') ? $option['price_type'] : 'fixed'; @@ -1403,7 +1404,7 @@ class Product extends \Magento\ImportExport\Model\Export\Entity\AbstractEntity if ($values) { foreach ($values as $value) { $row['option_title'] = $value['title']; - if (Store::DEFAULT_STORE_ID == $storeId) { + if (Store::DEFAULT_STORE_ID === $storeId) { $row['option_title'] = $value['title']; $valuePriceType = ($value['price_type'] == 'percent') ? $value['price_type'] : 'fixed'; $row['price'] = $value['price']; diff --git a/app/code/Magento/CatalogImportExport/Model/Import/Product/Option.php b/app/code/Magento/CatalogImportExport/Model/Import/Product/Option.php index 345c4c634df36198eba7b84fa1e6d9ca79fef408..75391e7d4a66a0554f6d2b1d726edacec353a8d9 100644 --- a/app/code/Magento/CatalogImportExport/Model/Import/Product/Option.php +++ b/app/code/Magento/CatalogImportExport/Model/Import/Product/Option.php @@ -1197,6 +1197,8 @@ class Option extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity $this->_tables['catalog_product_option_type_value'] ); $prevOptionId = 0; + $optionId = null; + $valueId = null; while ($bunch = $this->_dataSourceModel->getNextBunch()) { $products = []; $options = []; @@ -1209,7 +1211,7 @@ class Option extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity $childCount = []; foreach ($bunch as $rowNumber => $rowData) { - if (empty($rowData[PRODUCT::COL_STORE_VIEW_CODE]) && isset($optionId) && isset($valueId)) { + if (isset($optionId, $valueId) && empty($rowData[PRODUCT::COL_STORE_VIEW_CODE])) { $nextOptionId = $optionId; $nextValueId = $valueId; }