@@ -44,7 +44,7 @@ class Product extends \Ess\M2ePro\Model\AbstractModel
44
44
* )
45
45
*/
46
46
47
- /** @var array */
47
+ /** @var array */
48
48
public static $ statistics = [];
49
49
50
50
protected $ inventoryFactory ;
@@ -102,8 +102,11 @@ class Product extends \Ess\M2ePro\Model\AbstractModel
102
102
103
103
/** @var \Magento\Catalog\Api\ProductRepositoryInterface */
104
104
private $ productRepository ;
105
+ /** @var \Ess\M2ePro\Model\Magento\Product\ImageFactory */
106
+ private $ imageFactory ;
105
107
106
108
public function __construct (
109
+ \Ess \M2ePro \Model \Magento \Product \ImageFactory $ imageFactory ,
107
110
Factory $ inventoryFactory ,
108
111
\Magento \Framework \Filesystem \DriverPool $ driverPool ,
109
112
\Magento \Framework \App \ResourceConnection $ resourceModel ,
@@ -145,6 +148,7 @@ public function __construct(
145
148
$ this ->moduleConfiguration = $ moduleConfiguration ;
146
149
$ this ->m2eProductFactory = $ m2eProductFactory ;
147
150
$ this ->productRepository = $ productRepository ;
151
+ $ this ->imageFactory = $ imageFactory ;
148
152
149
153
parent ::__construct ($ helperFactory , $ modelFactory );
150
154
}
@@ -1498,11 +1502,11 @@ public function getThumbnailImage()
1498
1502
return null ;
1499
1503
}
1500
1504
1501
- $ thumbnailTempPath = $ this ->filesystem ->getDirectoryRead (\Magento \Framework \App \Filesystem \DirectoryList::MEDIA )
1502
- ->getAbsolutePath () . 'catalog/product/ ' . ltrim ($ thumbnailTempPath , '/ ' );
1505
+ $ thumbnailTempPath = $ this ->filesystem
1506
+ ->getDirectoryRead (\Magento \Framework \App \Filesystem \DirectoryList::MEDIA )
1507
+ ->getAbsolutePath () . 'catalog/product/ ' . ltrim ($ thumbnailTempPath , '/ ' );
1503
1508
1504
- /** @var Image $image */
1505
- $ image = $ this ->modelFactory ->getObject ('Magento_Product_Image ' );
1509
+ $ image = $ this ->imageFactory ->create ();
1506
1510
$ image ->setPath ($ thumbnailTempPath );
1507
1511
$ image ->setArea (\Magento \Framework \App \Area::AREA_ADMINHTML );
1508
1512
$ image ->setStoreId ($ this ->getStoreId ());
@@ -1580,8 +1584,7 @@ public function getImage($attribute = 'image')
1580
1584
return null ;
1581
1585
}
1582
1586
1583
- /** @var Image $image */
1584
- $ image = $ this ->modelFactory ->getObject ('Magento_Product_Image ' );
1587
+ $ image = $ this ->imageFactory ->create ();
1585
1588
$ image ->setUrl ($ imageUrl );
1586
1589
$ image ->setStoreId ($ this ->getStoreId ());
1587
1590
@@ -1601,50 +1604,39 @@ public function getGalleryImages($limitImages = 0)
1601
1604
return [];
1602
1605
}
1603
1606
1604
- $ galleryImages = $ this ->getProduct ()->getData ( ' media_gallery ' );
1607
+ $ galleryImages = $ this ->getProduct ()->getMediaGalleryEntries ( );
1605
1608
1606
- if (! isset ($ galleryImages[ ' images ' ]) || ! is_array ( $ galleryImages [ ' images ' ] )) {
1609
+ if (empty ($ galleryImages )) {
1607
1610
return [];
1608
1611
}
1609
1612
1610
1613
$ i = 0 ;
1611
1614
$ images = [];
1612
1615
1613
- foreach ($ galleryImages[ ' images ' ] as $ galleryImage ) {
1616
+ foreach ($ galleryImages as $ galleryImage ) {
1614
1617
if ($ i >= $ limitImages ) {
1615
1618
break ;
1616
1619
}
1617
1620
1618
- if (isset ($ galleryImage ['disabled ' ]) && (bool )$ galleryImage ['disabled ' ]) {
1619
- continue ;
1620
- }
1621
-
1622
- if (!isset ($ galleryImage ['file ' ])) {
1623
- continue ;
1624
- }
1625
-
1626
- if (
1627
- isset ($ galleryImage ['media_type ' ]) &&
1628
- $ galleryImage ['media_type ' ] === ExternalVideoEntryConverter::MEDIA_TYPE_CODE
1629
- ) {
1621
+ if ($ this ->isNeedSkipGalleryImage ($ galleryImage )) {
1630
1622
continue ;
1631
1623
}
1632
1624
1633
- $ imageUrl = $ this ->storeFactory ->create ()
1634
- ->load ($ this ->getStoreId ())
1635
- ->getBaseUrl (
1636
- \Magento \Framework \UrlInterface::URL_TYPE_MEDIA ,
1637
- $ this ->moduleConfiguration ->getSecureImageUrlInItemDescriptionMode ()
1638
- );
1639
- $ imageUrl .= 'catalog/product/ ' . ltrim ($ galleryImage ['file ' ], '/ ' );
1625
+ $ imageUrl = $ this ->storeFactory
1626
+ ->create ()
1627
+ ->load ($ this ->getStoreId ())
1628
+ ->getBaseUrl (
1629
+ \Magento \Framework \UrlInterface::URL_TYPE_MEDIA ,
1630
+ $ this ->moduleConfiguration ->getSecureImageUrlInItemDescriptionMode ()
1631
+ );
1632
+ $ imageUrl .= 'catalog/product/ ' . ltrim ($ galleryImage ->getFile (), '/ ' );
1640
1633
$ imageUrl = $ this ->prepareImageUrl ($ imageUrl );
1641
1634
1642
1635
if (empty ($ imageUrl )) {
1643
1636
continue ;
1644
1637
}
1645
1638
1646
- /** @var Image $image */
1647
- $ image = $ this ->modelFactory ->getObject ('Magento_Product_Image ' );
1639
+ $ image = $ this ->imageFactory ->create ();
1648
1640
$ image ->setUrl ($ imageUrl );
1649
1641
$ image ->setStoreId ($ this ->getStoreId ());
1650
1642
@@ -1703,8 +1695,7 @@ public function getGalleryImageByPosition($position = 1)
1703
1695
1704
1696
$ imageUrl = $ this ->prepareImageUrl ($ imageUrl );
1705
1697
1706
- /** @var Image $image */
1707
- $ image = $ this ->modelFactory ->getObject ('Magento_Product_Image ' );
1698
+ $ image = $ this ->imageFactory ->create ();
1708
1699
$ image ->setUrl ($ imageUrl );
1709
1700
$ image ->setStoreId ($ this ->getStoreId ());
1710
1701
@@ -1795,5 +1786,35 @@ public function clearNotFoundAttributes()
1795
1786
$ this ->notFoundAttributes = [];
1796
1787
}
1797
1788
1798
- //########################################
1789
+ // ----------------------------------------
1790
+
1791
+ private function isNeedSkipGalleryImage (
1792
+ \Magento \Catalog \Api \Data \ProductAttributeMediaGalleryEntryInterface $ galleryImage
1793
+ ): bool {
1794
+ if ($ galleryImage ->isDisabled ()) {
1795
+ return true ;
1796
+ }
1797
+
1798
+ if (empty ($ galleryImage ->getFile ())) {
1799
+ return true ;
1800
+ }
1801
+
1802
+ if ($ galleryImage ->getMediaType () === ExternalVideoEntryConverter::MEDIA_TYPE_CODE ) {
1803
+ return true ;
1804
+ }
1805
+
1806
+ if (in_array ('image ' , $ galleryImage ->getTypes ())) {
1807
+ return false ;
1808
+ }
1809
+
1810
+ if (in_array ('small_image ' , $ galleryImage ->getTypes ())) {
1811
+ return true ;
1812
+ }
1813
+
1814
+ if (in_array ('thumbnail ' , $ galleryImage ->getTypes ())) {
1815
+ return true ;
1816
+ }
1817
+
1818
+ return false ;
1819
+ }
1799
1820
}
0 commit comments