@@ -898,8 +898,8 @@ public function isAttributeValid($attrCode, array $attrParams, array $rowData, $
898
898
}
899
899
900
900
/**
901
- *
902
901
* Multiple value separator getter.
902
+ *
903
903
* @return string
904
904
*/
905
905
public function getMultipleValueSeparator ()
@@ -949,6 +949,8 @@ public function getMediaGalleryAttributeId()
949
949
}
950
950
951
951
/**
952
+ * Get product by type name.
953
+ *
952
954
* @param string $name
953
955
* @return Product\Type\AbstractType
954
956
*/
@@ -1189,8 +1191,10 @@ protected function _initErrorTemplates()
1189
1191
}
1190
1192
1191
1193
/**
1192
- * Set valid attribute set and product type to rows with all scopes
1193
- * to ensure that existing products doesn't changed.
1194
+ * Set valid attribute set and product type to rows.
1195
+ *
1196
+ * Set valid attribute set and product type to rows with all
1197
+ * scopes to ensure that existing products doesn't changed.
1194
1198
*
1195
1199
* @param array $rowData
1196
1200
* @return array
@@ -1220,6 +1224,7 @@ protected function _prepareRowForDb(array $rowData)
1220
1224
1221
1225
/**
1222
1226
* Gather and save information about product links.
1227
+ *
1223
1228
* Must be called after ALL products saving done.
1224
1229
*
1225
1230
* @return $this
@@ -1468,6 +1473,7 @@ public function saveProductEntity(array $entityRowsIn, array $entityRowsUp)
1468
1473
1469
1474
/**
1470
1475
* Return additional data, needed to select.
1476
+ *
1471
1477
* @return array
1472
1478
*/
1473
1479
private function getOldSkuFieldsForSelect ()
@@ -1477,6 +1483,7 @@ private function getOldSkuFieldsForSelect()
1477
1483
1478
1484
/**
1479
1485
* Adds newly created products to _oldSku
1486
+ *
1480
1487
* @param array $newProducts
1481
1488
* @return void
1482
1489
*/
@@ -1514,6 +1521,7 @@ private function getNewSkuFieldsForSelect()
1514
1521
1515
1522
/**
1516
1523
* Init media gallery resources
1524
+ *
1517
1525
* @return void
1518
1526
* @since 100.0.4
1519
1527
* @deprecated
@@ -1544,6 +1552,8 @@ protected function getExistingImages($bunch)
1544
1552
}
1545
1553
1546
1554
/**
1555
+ * Retrieve image from row.
1556
+ *
1547
1557
* @param array $rowData
1548
1558
* @return array
1549
1559
*/
@@ -1599,6 +1609,7 @@ protected function _saveProducts()
1599
1609
$ tierPrices = [];
1600
1610
$ mediaGallery = [];
1601
1611
$ labelsForUpdate = [];
1612
+ $ imagesForChangeVisibility = [];
1602
1613
$ uploadedImages = [];
1603
1614
$ previousType = null ;
1604
1615
$ prevAttributeSet = null ;
@@ -1718,21 +1729,24 @@ protected function _saveProducts()
1718
1729
}
1719
1730
1720
1731
// 5. Media gallery phase
1721
- $ disabledImages = [];
1722
1732
list ($ rowImages , $ rowLabels ) = $ this ->getImagesFromRow ($ rowData );
1723
1733
$ storeId = !empty ($ rowData [self ::COL_STORE ])
1724
1734
? $ this ->getStoreIdByCode ($ rowData [self ::COL_STORE ])
1725
1735
: Store::DEFAULT_STORE_ID ;
1726
- if (isset ($ rowData ['_media_is_disabled ' ]) && strlen (trim ($ rowData ['_media_is_disabled ' ]))) {
1727
- $ disabledImages = array_flip (
1728
- explode ($ this ->getMultipleValueSeparator (), $ rowData ['_media_is_disabled ' ])
1729
- );
1736
+ $ imageHiddenStates = $ this ->getImagesHiddenStates ($ rowData );
1737
+ foreach (array_keys ($ imageHiddenStates ) as $ image ) {
1738
+ if (array_key_exists ($ rowSku , $ existingImages )
1739
+ && array_key_exists ($ image , $ existingImages [$ rowSku ])
1740
+ ) {
1741
+ $ rowImages [self ::COL_MEDIA_IMAGE ][] = $ image ;
1742
+ $ uploadedImages [$ image ] = $ image ;
1743
+ }
1744
+
1730
1745
if (empty ($ rowImages )) {
1731
- foreach (array_keys ($ disabledImages ) as $ disabledImage ) {
1732
- $ rowImages [self ::COL_MEDIA_IMAGE ][] = $ disabledImage ;
1733
- }
1746
+ $ rowImages [self ::COL_MEDIA_IMAGE ][] = $ image ;
1734
1747
}
1735
1748
}
1749
+
1736
1750
$ rowData [self ::COL_MEDIA_IMAGE ] = [];
1737
1751
1738
1752
/*
@@ -1766,13 +1780,23 @@ protected function _saveProducts()
1766
1780
1767
1781
if ($ uploadedFile && !isset ($ mediaGallery [$ storeId ][$ rowSku ][$ uploadedFile ])) {
1768
1782
if (isset ($ existingImages [$ rowSku ][$ uploadedFile ])) {
1783
+ $ currentFileData = $ existingImages [$ rowSku ][$ uploadedFile ];
1769
1784
if (isset ($ rowLabels [$ column ][$ columnImageKey ])
1770
1785
&& $ rowLabels [$ column ][$ columnImageKey ] !=
1771
- $ existingImages [ $ rowSku ][ $ uploadedFile ] ['label ' ]
1786
+ $ currentFileData ['label ' ]
1772
1787
) {
1773
1788
$ labelsForUpdate [] = [
1774
1789
'label ' => $ rowLabels [$ column ][$ columnImageKey ],
1775
- 'imageData ' => $ existingImages [$ rowSku ][$ uploadedFile ]
1790
+ 'imageData ' => $ currentFileData
1791
+ ];
1792
+ }
1793
+
1794
+ if (array_key_exists ($ uploadedFile , $ imageHiddenStates )
1795
+ && $ currentFileData ['disabled ' ] != $ imageHiddenStates [$ uploadedFile ]
1796
+ ) {
1797
+ $ imagesForChangeVisibility [] = [
1798
+ 'disabled ' => $ imageHiddenStates [$ uploadedFile ],
1799
+ 'imageData ' => $ currentFileData
1776
1800
];
1777
1801
}
1778
1802
} else {
@@ -1785,7 +1809,8 @@ protected function _saveProducts()
1785
1809
? $ rowLabels [$ column ][$ columnImageKey ]
1786
1810
: '' ,
1787
1811
'position ' => ++$ position ,
1788
- 'disabled ' => isset ($ disabledImages [$ columnImage ]) ? '1 ' : '0 ' ,
1812
+ 'disabled ' => isset ($ imageHiddenStates [$ columnImage ])
1813
+ ? $ imageHiddenStates [$ columnImage ] : '0 ' ,
1789
1814
'value ' => $ uploadedFile ,
1790
1815
];
1791
1816
}
@@ -1905,6 +1930,8 @@ protected function _saveProducts()
1905
1930
$ mediaGallery
1906
1931
)->_saveProductAttributes (
1907
1932
$ attributes
1933
+ )->updateMediaGalleryVisibility (
1934
+ $ imagesForChangeVisibility
1908
1935
)->updateMediaGalleryLabels (
1909
1936
$ labelsForUpdate
1910
1937
);
@@ -1919,6 +1946,34 @@ protected function _saveProducts()
1919
1946
}
1920
1947
1921
1948
/**
1949
+ * Prepare array with image states (visible or hidden from product page)
1950
+ *
1951
+ * @param array $rowData
1952
+ * @return array
1953
+ */
1954
+ private function getImagesHiddenStates ($ rowData )
1955
+ {
1956
+ $ statesArray = [];
1957
+ $ mappingArray = [
1958
+ '_media_is_disabled ' => '1 '
1959
+ ];
1960
+
1961
+ foreach ($ mappingArray as $ key => $ value ) {
1962
+ if (isset ($ rowData [$ key ]) && strlen (trim ($ rowData [$ key ]))) {
1963
+ $ items = explode ($ this ->getMultipleValueSeparator (), $ rowData [$ key ]);
1964
+
1965
+ foreach ($ items as $ item ) {
1966
+ $ statesArray [$ item ] = $ value ;
1967
+ }
1968
+ }
1969
+ }
1970
+
1971
+ return $ statesArray ;
1972
+ }
1973
+
1974
+ /**
1975
+ * Process row categories.
1976
+ *
1922
1977
* @param array $rowData
1923
1978
* @return array
1924
1979
*/
@@ -1946,6 +2001,8 @@ protected function processRowCategories($rowData)
1946
2001
}
1947
2002
1948
2003
/**
2004
+ * Get product websites.
2005
+ *
1949
2006
* @param string $productSku
1950
2007
* @return array
1951
2008
*/
@@ -1955,6 +2012,8 @@ public function getProductWebsites($productSku)
1955
2012
}
1956
2013
1957
2014
/**
2015
+ * Retrieve product categories.
2016
+ *
1958
2017
* @param string $productSku
1959
2018
* @return array
1960
2019
*/
@@ -1964,6 +2023,8 @@ public function getProductCategories($productSku)
1964
2023
}
1965
2024
1966
2025
/**
2026
+ * Get store id by code.
2027
+ *
1967
2028
* @param string $storeCode
1968
2029
* @return array|int|null|string
1969
2030
*/
@@ -2055,6 +2116,8 @@ protected function _getUploader()
2055
2116
}
2056
2117
2057
2118
/**
2119
+ * Retrieve uploader.
2120
+ *
2058
2121
* @return Uploader
2059
2122
* @throws \Magento\Framework\Exception\LocalizedException
2060
2123
*/
@@ -2065,6 +2128,7 @@ public function getUploader()
2065
2128
2066
2129
/**
2067
2130
* Uploading files into the "catalog/product" media folder.
2131
+ *
2068
2132
* Return a new file name if the same file is already exists.
2069
2133
*
2070
2134
* @param string $fileName
@@ -2259,7 +2323,7 @@ public function getEntityTypeCode()
2259
2323
* Returns array of new products data with SKU as key. All SKU keys are in lowercase for avoiding creation of
2260
2324
* new products with the same SKU in different letter cases.
2261
2325
*
2262
- * @var string $sku
2326
+ * @param string $sku
2263
2327
* @return array
2264
2328
*/
2265
2329
public function getNewSku ($ sku = null )
@@ -2452,6 +2516,8 @@ public function validateRow(array $rowData, $rowNum)
2452
2516
}
2453
2517
2454
2518
/**
2519
+ * Check if need to validate url key.
2520
+ *
2455
2521
* @param array $rowData
2456
2522
* @return bool
2457
2523
*/
@@ -2763,8 +2829,11 @@ protected function getProductUrlSuffix($storeId = null)
2763
2829
}
2764
2830
2765
2831
/**
2832
+ * Get url key.
2833
+ *
2766
2834
* @param array $rowData
2767
2835
* @return string
2836
+ *
2768
2837
* @since 100.0.3
2769
2838
*/
2770
2839
protected function getUrlKey ($ rowData )
@@ -2781,7 +2850,10 @@ protected function getUrlKey($rowData)
2781
2850
}
2782
2851
2783
2852
/**
2853
+ * Retrieve resource.
2854
+ *
2784
2855
* @return Proxy\Product\ResourceModel
2856
+ *
2785
2857
* @since 100.0.3
2786
2858
*/
2787
2859
protected function getResource ()
@@ -2835,6 +2907,21 @@ private function updateMediaGalleryLabels(array $labels)
2835
2907
}
2836
2908
}
2837
2909
2910
+ /**
2911
+ * Update 'disabled' field for media gallery entity
2912
+ *
2913
+ * @param array $images
2914
+ * @return $this
2915
+ */
2916
+ private function updateMediaGalleryVisibility (array $ images )
2917
+ {
2918
+ if (!empty ($ images )) {
2919
+ $ this ->mediaProcessor ->updateMediaGalleryVisibility ($ images );
2920
+ }
2921
+
2922
+ return $ this ;
2923
+ }
2924
+
2838
2925
/**
2839
2926
* Parse values from multiple attributes fields
2840
2927
*
0 commit comments