@@ -309,12 +309,12 @@ define([
309
309
_create : function ( ) {
310
310
var options = this . options ,
311
311
gallery = $ ( '[data-gallery-role=gallery-placeholder]' , '.column.main' ) ,
312
- isProductViewExist = $ ( 'body.catalog-product-view' ) . size ( ) > 0 ,
313
- $main = isProductViewExist ?
312
+ productData = this . _determineProductData ( ) ,
313
+ $main = productData . isInProductView ?
314
314
this . element . parents ( '.column.main' ) :
315
315
this . element . parents ( '.product-item-info' ) ;
316
316
317
- if ( isProductViewExist ) {
317
+ if ( productData . isInProductView ) {
318
318
gallery . data ( 'gallery' ) ?
319
319
this . _onGalleryLoaded ( gallery ) :
320
320
gallery . on ( 'gallery:loaded' , this . _onGalleryLoaded . bind ( this , gallery ) ) ;
@@ -328,6 +328,28 @@ define([
328
328
this . inProductList = this . productForm . length > 0 ;
329
329
} ,
330
330
331
+ /**
332
+ * Determine product id and related data
333
+ *
334
+ * @returns {{productId: *, isInProductView: boolean} }
335
+ * @private
336
+ */
337
+ _determineProductData : function ( ) {
338
+ // Check if product is in a list of products.
339
+ var productId = this . element . parents ( '.product-item-details' )
340
+ . find ( '.price-box.price-final_price' ) . attr ( 'data-product-id' ) ,
341
+ isInProductView = false ;
342
+
343
+ if ( ! productId ) {
344
+ // Check individual product.
345
+ var product = document . getElementsByName ( 'product' ) [ 0 ] ;
346
+ productId = product ? product . value : undefined ;
347
+ isInProductView = productId > 0 ;
348
+ }
349
+
350
+ return { productId : productId , isInProductView : isInProductView } ;
351
+ } ,
352
+
331
353
/**
332
354
* Render controls
333
355
*
@@ -897,7 +919,7 @@ define([
897
919
var $widget = this ,
898
920
$this = $widget . element ,
899
921
attributes = { } ,
900
- productId = 0 ,
922
+ productData = this . _determineProductData ( ) ,
901
923
mediaCallData ,
902
924
mediaCacheKey ,
903
925
@@ -911,7 +933,7 @@ define([
911
933
if ( ! ( mediaCacheKey in $widget . options . mediaCache ) ) {
912
934
$widget . options . mediaCache [ mediaCacheKey ] = data ;
913
935
}
914
- $widget . _ProductMediaCallback ( $this , data ) ;
936
+ $widget . _ProductMediaCallback ( $this , data , productData . isInProductView ) ;
915
937
$widget . _DisableProductMediaLoader ( $this ) ;
916
938
} ;
917
939
@@ -925,17 +947,8 @@ define([
925
947
attributes [ $selected . attr ( 'attribute-code' ) ] = $selected . attr ( 'option-selected' ) ;
926
948
} ) ;
927
949
928
- if ( $ ( 'body.catalog-product-view' ) . size ( ) > 0 ) {
929
- //Product Page
930
- productId = document . getElementsByName ( 'product' ) [ 0 ] . value ;
931
- } else {
932
- //Category View
933
- productId = $this . parents ( '.product.details.product-item-details' )
934
- . find ( '.price-box.price-final_price' ) . attr ( 'data-product-id' ) ;
935
- }
936
-
937
950
mediaCallData = {
938
- 'product_id' : productId ,
951
+ 'product_id' : productData . productId ,
939
952
'attributes' : attributes ,
940
953
'additional' : $ . parseQuery ( )
941
954
} ;
@@ -1001,11 +1014,11 @@ define([
1001
1014
*
1002
1015
* @param {Object } $this
1003
1016
* @param {String } response
1017
+ * @param {Boolean } isInProductView
1004
1018
* @private
1005
1019
*/
1006
- _ProductMediaCallback : function ( $this , response ) {
1007
- var isProductViewExist = $ ( 'body.catalog-product-view' ) . size ( ) > 0 ,
1008
- $main = isProductViewExist ? $this . parents ( '.column.main' ) : $this . parents ( '.product-item-info' ) ,
1020
+ _ProductMediaCallback : function ( $this , response , isInProductView ) {
1021
+ var $main = isInProductView ? $this . parents ( '.column.main' ) : $this . parents ( '.product-item-info' ) ,
1009
1022
$widget = this ,
1010
1023
images = [ ] ,
1011
1024
@@ -1020,7 +1033,7 @@ define([
1020
1033
} ;
1021
1034
1022
1035
if ( _ . size ( $widget ) < 1 || ! support ( response ) ) {
1023
- this . updateBaseImage ( this . options . mediaGalleryInitial , $main , isProductViewExist ) ;
1036
+ this . updateBaseImage ( this . options . mediaGalleryInitial , $main , isInProductView ) ;
1024
1037
1025
1038
return ;
1026
1039
}
@@ -1045,7 +1058,7 @@ define([
1045
1058
} ) ;
1046
1059
}
1047
1060
1048
- this . updateBaseImage ( images , $main , isProductViewExist ) ;
1061
+ this . updateBaseImage ( images , $main , isInProductView ) ;
1049
1062
} ,
1050
1063
1051
1064
/**
@@ -1070,16 +1083,16 @@ define([
1070
1083
* Update [gallery-placeholder] or [product-image-photo]
1071
1084
* @param {Array } images
1072
1085
* @param {jQuery } context
1073
- * @param {Boolean } isProductViewExist
1086
+ * @param {Boolean } isInProductView
1074
1087
*/
1075
- updateBaseImage : function ( images , context , isProductViewExist ) {
1088
+ updateBaseImage : function ( images , context , isInProductView ) {
1076
1089
var justAnImage = images [ 0 ] ,
1077
1090
initialImages = this . options . mediaGalleryInitial ,
1078
1091
gallery = context . find ( this . options . mediaGallerySelector ) . data ( 'gallery' ) ,
1079
1092
imagesToUpdate ,
1080
1093
isInitial ;
1081
1094
1082
- if ( isProductViewExist ) {
1095
+ if ( isInProductView ) {
1083
1096
imagesToUpdate = images . length ? this . _setImageType ( $ . extend ( true , [ ] , images ) ) : [ ] ;
1084
1097
isInitial = _ . isEqual ( imagesToUpdate , initialImages ) ;
1085
1098
0 commit comments