@@ -1543,6 +1543,7 @@ protected function _saveProducts()
1543
1543
$ tierPrices = [];
1544
1544
$ mediaGallery = [];
1545
1545
$ labelsForUpdate = [];
1546
+ $ imagesForChangeVisibility = [];
1546
1547
$ uploadedImages = [];
1547
1548
$ previousType = null ;
1548
1549
$ prevAttributeSet = null ;
@@ -1662,21 +1663,24 @@ protected function _saveProducts()
1662
1663
}
1663
1664
1664
1665
// 5. Media gallery phase
1665
- $ disabledImages = [];
1666
1666
list ($ rowImages , $ rowLabels ) = $ this ->getImagesFromRow ($ rowData );
1667
1667
$ storeId = !empty ($ rowData [self ::COL_STORE ])
1668
1668
? $ this ->getStoreIdByCode ($ rowData [self ::COL_STORE ])
1669
1669
: Store::DEFAULT_STORE_ID ;
1670
- if (isset ($ rowData ['_media_is_disabled ' ]) && strlen (trim ($ rowData ['_media_is_disabled ' ]))) {
1671
- $ disabledImages = array_flip (
1672
- explode ($ this ->getMultipleValueSeparator (), $ rowData ['_media_is_disabled ' ])
1673
- );
1670
+ $ imageHiddenStates = $ this ->getImagesHiddenStates ($ rowData );
1671
+ foreach (array_keys ($ imageHiddenStates ) as $ image ) {
1672
+ if (array_key_exists ($ rowSku , $ existingImages )
1673
+ && array_key_exists ($ image , $ existingImages [$ rowSku ])
1674
+ ) {
1675
+ $ rowImages [self ::COL_MEDIA_IMAGE ][] = $ image ;
1676
+ $ uploadedImages [$ image ] = $ image ;
1677
+ }
1678
+
1674
1679
if (empty ($ rowImages )) {
1675
- foreach (array_keys ($ disabledImages ) as $ disabledImage ) {
1676
- $ rowImages [self ::COL_MEDIA_IMAGE ][] = $ disabledImage ;
1677
- }
1680
+ $ rowImages [self ::COL_MEDIA_IMAGE ][] = $ image ;
1678
1681
}
1679
1682
}
1683
+
1680
1684
$ rowData [self ::COL_MEDIA_IMAGE ] = [];
1681
1685
1682
1686
/*
@@ -1710,12 +1714,22 @@ protected function _saveProducts()
1710
1714
1711
1715
if ($ uploadedFile && !isset ($ mediaGallery [$ storeId ][$ rowSku ][$ uploadedFile ])) {
1712
1716
if (isset ($ existingImages [$ rowSku ][$ uploadedFile ])) {
1717
+ $ currentFileData = $ existingImages [$ rowSku ][$ uploadedFile ];
1713
1718
if (isset ($ rowLabels [$ column ][$ columnImageKey ])
1714
- && $ rowLabels [$ column ][$ columnImageKey ] != $ existingImages [ $ rowSku ][ $ uploadedFile ] ['label ' ]
1719
+ && $ rowLabels [$ column ][$ columnImageKey ] != $ currentFileData ['label ' ]
1715
1720
) {
1716
1721
$ labelsForUpdate [] = [
1717
1722
'label ' => $ rowLabels [$ column ][$ columnImageKey ],
1718
- 'imageData ' => $ existingImages [$ rowSku ][$ uploadedFile ]
1723
+ 'imageData ' => $ currentFileData ,
1724
+ ];
1725
+ }
1726
+
1727
+ if (array_key_exists ($ uploadedFile , $ imageHiddenStates )
1728
+ && $ currentFileData ['disabled ' ] != $ imageHiddenStates [$ uploadedFile ]
1729
+ ) {
1730
+ $ imagesForChangeVisibility [] = [
1731
+ 'disabled ' => $ imageHiddenStates [$ uploadedFile ],
1732
+ 'imageData ' => $ currentFileData ,
1719
1733
];
1720
1734
}
1721
1735
} else {
@@ -1724,9 +1738,11 @@ protected function _saveProducts()
1724
1738
}
1725
1739
$ mediaGallery [$ storeId ][$ rowSku ][$ uploadedFile ] = [
1726
1740
'attribute_id ' => $ this ->getMediaGalleryAttributeId (),
1727
- 'label ' => isset ($ rowLabels [$ column ][$ columnImageKey ]) ? $ rowLabels [$ column ][$ columnImageKey ] : '' ,
1741
+ 'label ' => isset ($ rowLabels [$ column ][$ columnImageKey ])
1742
+ ? $ rowLabels [$ column ][$ columnImageKey ] : '' ,
1728
1743
'position ' => ++$ position ,
1729
- 'disabled ' => isset ($ disabledImages [$ columnImage ]) ? '1 ' : '0 ' ,
1744
+ 'disabled ' => isset ($ imageHiddenStates [$ columnImage ])
1745
+ ? $ imageHiddenStates [$ columnImage ] : '0 ' ,
1730
1746
'value ' => $ uploadedFile ,
1731
1747
];
1732
1748
}
@@ -1847,6 +1863,8 @@ protected function _saveProducts()
1847
1863
$ mediaGallery
1848
1864
)->_saveProductAttributes (
1849
1865
$ attributes
1866
+ )->updateMediaGalleryVisibility (
1867
+ $ imagesForChangeVisibility
1850
1868
)->updateMediaGalleryLabels (
1851
1869
$ labelsForUpdate
1852
1870
);
@@ -1861,6 +1879,34 @@ protected function _saveProducts()
1861
1879
}
1862
1880
1863
1881
/**
1882
+ * Prepare array with image states (visible or hidden from product page)
1883
+ *
1884
+ * @param array $rowData
1885
+ * @return array
1886
+ */
1887
+ private function getImagesHiddenStates (array $ rowData ): array
1888
+ {
1889
+ $ statesArray = [];
1890
+ $ mappingArray = [
1891
+ '_media_is_disabled ' => '1 ' ,
1892
+ ];
1893
+
1894
+ foreach ($ mappingArray as $ key => $ value ) {
1895
+ if (isset ($ rowData [$ key ]) && strlen (trim ($ rowData [$ key ]))) {
1896
+ $ items = explode ($ this ->getMultipleValueSeparator (), $ rowData [$ key ]);
1897
+
1898
+ foreach ($ items as $ item ) {
1899
+ $ statesArray [$ item ] = $ value ;
1900
+ }
1901
+ }
1902
+ }
1903
+
1904
+ return $ statesArray ;
1905
+ }
1906
+
1907
+ /**
1908
+ * Resolve valid category ids from provided row data.
1909
+ *
1864
1910
* @param array $rowData
1865
1911
* @return array
1866
1912
*/
@@ -2205,7 +2251,7 @@ public function getEntityTypeCode()
2205
2251
* Returns array of new products data with SKU as key. All SKU keys are in lowercase for avoiding creation of
2206
2252
* new products with the same SKU in different letter cases.
2207
2253
*
2208
- * @var string $sku
2254
+ * @param string $sku
2209
2255
* @return array
2210
2256
*/
2211
2257
public function getNewSku ($ sku = null )
@@ -2782,6 +2828,21 @@ private function updateMediaGalleryLabels(array $labels)
2782
2828
$ this ->mediaProcessor ->updateMediaGalleryLabels ($ labels );
2783
2829
}
2784
2830
2831
+ /**
2832
+ * Update 'disabled' field for media gallery entity
2833
+ *
2834
+ * @param array $images
2835
+ * @return $this
2836
+ */
2837
+ private function updateMediaGalleryVisibility (array $ images ): Product
2838
+ {
2839
+ if (!empty ($ images )) {
2840
+ $ this ->mediaProcessor ->updateMediaGalleryVisibility ($ images );
2841
+ }
2842
+
2843
+ return $ this ;
2844
+ }
2845
+
2785
2846
/**
2786
2847
* Parse values from multiple attributes fields
2787
2848
*
0 commit comments