@@ -346,7 +346,7 @@ public function test_get_simple_product_dimensions_when_not_defined(){
346346 public function test_get_variation_dimensions_when_it_not_defined () {
347347
348348 // Prepare the variable product object
349- $ wc_variable_product = New \WC_Product_Variable ();
349+ $ wc_variable_product = new \WC_Product_Variable ();
350350 $ wc_variable_product ->set_length ( 5 );
351351 $ wc_variable_product ->set_height ( 10 );
352352 $ wc_variable_product ->set_width ( 15 );
@@ -366,7 +366,7 @@ public function test_get_variation_dimensions_when_it_not_defined() {
366366 public function test_get_variation_dimensions_when_it_overrides_parent_dimensions (){
367367
368368 // Prepare the variable product object
369- $ wc_variable_product = New \WC_Product_Variable ();
369+ $ wc_variable_product = new \WC_Product_Variable ();
370370 $ wc_variable_product ->set_length ( 5 );
371371 $ wc_variable_product ->set_height ( 10 );
372372 $ wc_variable_product ->set_width ( 15 );
@@ -386,4 +386,83 @@ public function test_get_variation_dimensions_when_it_overrides_parent_dimension
386386 $ this ->assertEquals ( 30 , $ sf_product ->get_height (), 'Product height should be 30. ' );
387387 $ this ->assertEquals ( 40 , $ sf_product ->get_width (), 'Product width should be 40. ' );
388388 }
389+
390+ public function test_variation_set_custom_main_image () {
391+ add_filter (
392+ 'shopping_feed_variation_main_image ' ,
393+ function ( $ image , $ variation , $ product ) {
394+ return 'https://example.com/image.jpg ' ;
395+ },
396+ 10 ,
397+ 3
398+ );
399+
400+ $ image_id = $ this ->factory ()->attachment ->create_object (
401+ [
402+ 'file ' => codecept_data_dir ( 'images/image1.png ' ),
403+ 'post_mime_type ' => 'image/png ' ,
404+ 'post_title ' => 'Test Image ' ,
405+ 'post_content ' => '' ,
406+ 'post_status ' => 'inherit ' ,
407+ ]
408+ );
409+
410+ $ variable_product = new \WC_Product_Variable ();
411+ $ variable_product_id = $ variable_product ->save ();
412+
413+ WC_Helper_Product::create_product_variation_object ( $ variable_product_id , 'variation-1 ' , 10 , [], true );
414+
415+ $ sf_product = new Product ( $ variable_product_id );
416+ $ this ->assertCount ( 1 , $ sf_product ->get_variations (), 'Variable product should have 1 variation. ' );
417+ $ this ->assertArrayHasKey ( 'image_main ' , $ sf_product ->get_variations ()[0 ], 'Variation should have an image_main key. ' );
418+ $ this ->assertEquals ( 'https://example.com/image.jpg ' , $ sf_product ->get_variations ()[0 ]['image_main ' ], 'Product main image should be equal to the custom value from the filter. ' );
419+ }
420+
421+ public function test_variation_use_thumbnail_as_main_image () {
422+ $ image_id = $ this ->factory ()->attachment ->create_object (
423+ [
424+ 'file ' => codecept_data_dir ( 'images/image1.png ' ),
425+ 'post_mime_type ' => 'image/png ' ,
426+ 'post_title ' => 'Test Image ' ,
427+ 'post_content ' => '' ,
428+ 'post_status ' => 'inherit ' ,
429+ ]
430+ );
431+ $ image_url = wp_get_attachment_image_url ( $ image_id , 'full ' );
432+
433+ $ variable_product = new \WC_Product_Variable ();
434+ $ variable_product_id = $ variable_product ->save ();
435+
436+ $ variation_product = WC_Helper_Product::create_product_variation_object ( $ variable_product_id , 'variation-1 ' , 10 , [], true );
437+ $ variation_product ->set_image_id ( $ image_id );
438+ $ variation_product ->save ();
439+
440+ $ sf_product = new Product ( $ variable_product_id );
441+ $ this ->assertCount ( 1 , $ sf_product ->get_variations (), 'Variable product should have 1 variation. ' );
442+ $ this ->assertArrayHasKey ( 'image_main ' , $ sf_product ->get_variations ()[0 ], 'Variation should have an image_main key. ' );
443+ $ this ->assertEquals ( $ image_url , $ sf_product ->get_variations ()[0 ]['image_main ' ], 'Product main image should be empty. ' );
444+ }
445+
446+ public function test_variation_empty_main_image_if_no_image_set () {
447+ $ image_id = $ this ->factory ()->attachment ->create_object (
448+ [
449+ 'file ' => codecept_data_dir ( 'images/image1.png ' ),
450+ 'post_mime_type ' => 'image/png ' ,
451+ 'post_title ' => 'Test Image ' ,
452+ 'post_content ' => '' ,
453+ 'post_status ' => 'inherit ' ,
454+ ]
455+ );
456+
457+ $ variable_product = new \WC_Product_Variable ();
458+ $ variable_product ->set_image_id ( $ image_id );
459+ $ variable_product_id = $ variable_product ->save ();
460+
461+ WC_Helper_Product::create_product_variation_object ( $ variable_product_id , 'variation-1 ' , 10 , [], true );
462+
463+ $ sf_product = new Product ( $ variable_product_id );
464+ $ this ->assertCount ( 1 , $ sf_product ->get_variations (), 'Variable product should have 1 variation. ' );
465+ $ this ->assertArrayHasKey ( 'image_main ' , $ sf_product ->get_variations ()[0 ], 'Variation should have an image_main key. ' );
466+ $ this ->assertEquals ( '' , $ sf_product ->get_variations ()[0 ]['image_main ' ], 'Product main image should be empty. ' );
467+ }
389468}
0 commit comments