Skip to content

Commit eada2f3

Browse files
Merge branch 'develop' into ver/6.11.0
2 parents 322d3de + d99fb81 commit eada2f3

File tree

8 files changed

+151
-4
lines changed

8 files changed

+151
-4
lines changed

readme.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,36 @@ function your_custom_fields_function($fields, $wc_product) {
315315
```
316316

317317
### Variation Images
318+
319+
#### Main image
320+
By default the variation's thumbnail is used as the main image in the feed.
321+
322+
You can customize the main image using the filter "shopping_feed_variation_main_image".
323+
324+
```php
325+
add_filter( 'shopping_feed_variation_main_image', 'your_custom_variation_main_images_function', 10, 3 );
326+
327+
/**
328+
* Use the parent's thumbnail if the variation doesn't have one.
329+
*
330+
* @param string $main_image The main image of the variation.
331+
* @param \WC_Product_Variation $variation The variation.
332+
* @param \WC_Product_Variable $product The product.
333+
*
334+
* @return string
335+
*/
336+
function your_custom_variation_main_images_function( $main_image, $variation, $product ) {
337+
if ( empty( $main_image ) && has_post_thumbnail( $product->get_id() ) ) {
338+
$main_image = get_the_post_thumbnail_url( $product->get_id(), 'full' );
339+
}
340+
341+
return $main_image;
342+
}
343+
```
344+
345+
#### Additional images
318346
By default, we don’t support any custom plugin for adding images to WC Product Variation, with this filter you can set the desired images to each variation, you can use the following snippet
347+
319348
```php
320349
add_filter( 'shopping_feed_variation_images', 'your_custom_variation_images_function', 10, 3 );
321350

readme.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,34 @@ function your_custom_fields_function($fields, $wc_product) {
320320
`
321321

322322
### Variation Images
323+
324+
#### Main image
325+
By default the variation's thumbnail is used as the main image in the feed.
326+
327+
You can customize the main image using the filter "shopping_feed_variation_main_image".
328+
329+
`
330+
add_filter( 'shopping_feed_variation_main_image', 'your_custom_variation_main_images_function', 10, 3 );
331+
332+
/**
333+
* Use the parent's thumbnail if the variation doesn't have one.
334+
*
335+
* @param string $main_image The main image of the variation.
336+
* @param \WC_Product_Variation $variation The variation.
337+
* @param \WC_Product_Variable $product The product.
338+
*
339+
* @return string
340+
*/
341+
function your_custom_variation_main_images_function( $main_image, $variation, $product ) {
342+
if ( empty( $main_image ) && has_post_thumbnail( $product->get_id() ) ) {
343+
$main_image = get_the_post_thumbnail_url( $product->get_id(), 'full' );
344+
}
345+
346+
return $main_image;
347+
}
348+
`
349+
350+
#### Additional images
323351
By default, we don’t support any custom plugin for adding images to WC Product Variation, with this filter you can set the desired images to each variation, you can use the following snippet
324352

325353
`

src/Products/Product.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,10 +477,21 @@ public function get_variations( $for_feed = false ) {
477477
$variation_data['width'] = $variation->get_width();
478478
$variation_data['height'] = $variation->get_height();
479479
$variation_data['length'] = $variation->get_length();
480-
if ( ! empty( get_the_post_thumbnail_url( $variation->get_id(), 'full' ) ) ) {
481-
$variation_data['image_main'] = get_the_post_thumbnail_url( $variation->get_id(), 'full' );
480+
481+
$main_image = '';
482+
if ( has_post_thumbnail( $variation->get_id() ) ) {
483+
$main_image = get_the_post_thumbnail_url( $variation->get_id(), 'full' );
482484
}
483485

486+
/**
487+
* Filter the main image of the variation.
488+
*
489+
* @param string $main_image The main image of the variation.
490+
* @param \WC_Product_Variation $variation The variation.
491+
* @param \WC_Product_Variable $product The product.
492+
*/
493+
$variation_data['image_main'] = apply_filters( 'shopping_feed_variation_main_image', $main_image, $variation, $product );
494+
484495
$variation_data['attributes'] = $this->get_variation_attributes( $variation );
485496
$variations[] = $variation_data;
486497
}

tests/_data/images/image1.png

14.3 KB
Loading

tests/_data/images/image2.png

11.2 KB
Loading

tests/_data/images/image3.png

12.7 KB
Loading

tests/_data/images/image4.png

9.29 KB
Loading

tests/wpunit/Feed/ProductFeedTest.php

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)