Skip to content

Commit 9c2d51a

Browse files
Merge pull request #142 from BeAPI/ver/6.11.0
Ver/6.11.0
2 parents 322d3de + 3b04690 commit 9c2d51a

File tree

10 files changed

+162
-11
lines changed

10 files changed

+162
-11
lines changed

.plugin-data

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"version": "6.10.0",
2+
"version": "6.11.0",
33
"slug": "shopping-feed"
44
}

readme.md

+33-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
* Contributors: ShoppingFeed, BeAPI
44
* Tags: shoppingfeed, marketplace, woocommerce, woocommerce shoppingfeed, create woocommerce products shoppingfeed, products feed, generate shoppingfeed, amazon, Jet, Walmart, many marketplace, import orders
5-
* Stable tag: 6.10.0
6-
* Version: 6.10.0
5+
* Stable tag: 6.11.0
6+
* Version: 6.11.0
77
* Requires PHP: 7.3
88
* Requires at least: 5.7
99
* Tested up to: 6.7
@@ -15,6 +15,8 @@
1515
> Version 6.0.0 is a major version, there are several changes and improvements which affect the architecture of the plugin. You will have to re-configure the plugin, all the previous settings will be lost
1616
1717
## Changelog
18+
* 6.11.0
19+
* Variations : add new filter to modify variation's main image
1820
* 6.10.0
1921
* Orders : Redesigned the shipment tracking system.
2022
* Orders (beta) : Add option to include VAT when importing orders.
@@ -315,7 +317,36 @@ function your_custom_fields_function($fields, $wc_product) {
315317
```
316318

317319
### Variation Images
320+
321+
#### Main image
322+
By default the variation's thumbnail is used as the main image in the feed.
323+
324+
You can customize the main image using the filter "shopping_feed_variation_main_image".
325+
326+
```php
327+
add_filter( 'shopping_feed_variation_main_image', 'your_custom_variation_main_images_function', 10, 3 );
328+
329+
/**
330+
* Use the parent's thumbnail if the variation doesn't have one.
331+
*
332+
* @param string $main_image The main image of the variation.
333+
* @param \WC_Product_Variation $variation The variation.
334+
* @param \WC_Product_Variable $product The product.
335+
*
336+
* @return string
337+
*/
338+
function your_custom_variation_main_images_function( $main_image, $variation, $product ) {
339+
if ( empty( $main_image ) && has_post_thumbnail( $product->get_id() ) ) {
340+
$main_image = get_the_post_thumbnail_url( $product->get_id(), 'full' );
341+
}
342+
343+
return $main_image;
344+
}
345+
```
346+
347+
#### Additional images
318348
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
349+
319350
```php
320351
add_filter( 'shopping_feed_variation_images', 'your_custom_variation_images_function', 10, 3 );
321352

readme.txt

+32-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
## ShoppingFeed
22
Contributors: ShoppingFeed, BeAPI
33
Tags: shoppingfeed, marketplace, woocommerce, woocommerce shoppingfeed, create woocommerce products shoppingfeed, products feed, generate shoppingfeed, amazon, Jet, Walmart, many marketplace, import orders
4-
Stable tag: 6.10.0
5-
Version: 6.10.0
4+
Stable tag: 6.11.0
5+
Version: 6.11.0
66
Requires PHP: 7.3
77
Requires at least: 5.7
88
Tested up to: 6.7
@@ -13,6 +13,8 @@ WC tested up to: 9.6.0
1313
Version 6.0.0 is a major version, there are several changes and improvements which affect the architecture of the plugin. You will have to re-configure the plugin, all the previous settings will be lost
1414

1515
== Changelog ==
16+
* 6.11.0
17+
* Variations : add new filter to modify variation's main image
1618
* 6.10.0
1719
* Orders : Redesigned the shipment tracking system.
1820
* Orders (beta) : Add option to include VAT when importing orders.
@@ -320,6 +322,34 @@ function your_custom_fields_function($fields, $wc_product) {
320322
`
321323

322324
### Variation Images
325+
326+
#### Main image
327+
By default the variation's thumbnail is used as the main image in the feed.
328+
329+
You can customize the main image using the filter "shopping_feed_variation_main_image".
330+
331+
`
332+
add_filter( 'shopping_feed_variation_main_image', 'your_custom_variation_main_images_function', 10, 3 );
333+
334+
/**
335+
* Use the parent's thumbnail if the variation doesn't have one.
336+
*
337+
* @param string $main_image The main image of the variation.
338+
* @param \WC_Product_Variation $variation The variation.
339+
* @param \WC_Product_Variable $product The product.
340+
*
341+
* @return string
342+
*/
343+
function your_custom_variation_main_images_function( $main_image, $variation, $product ) {
344+
if ( empty( $main_image ) && has_post_thumbnail( $product->get_id() ) ) {
345+
$main_image = get_the_post_thumbnail_url( $product->get_id(), 'full' );
346+
}
347+
348+
return $main_image;
349+
}
350+
`
351+
352+
#### Additional images
323353
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
324354

325355
`

shoppingfeed.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Author URI: https://www.shopping-feed.com/
88
* Text Domain: shopping-feed
99
* Domain Path: /languages
10-
* Version: 6.10.0
10+
* Version: 6.11.0
1111
* Requires at least: 5.7
1212
* Requires PHP: 7.3
1313
* WC requires at least: 5.1.0
@@ -26,7 +26,7 @@
2626
require_once plugin_dir_path( __FILE__ ) . '/vendor/autoload.php';
2727
}
2828

29-
define( 'SF_VERSION', '6.10.0' );
29+
define( 'SF_VERSION', '6.11.0' );
3030
define( 'SF_DB_VERSION_SLUG', 'SF_DB_VERSION' );
3131
define( 'SF_DB_VERSION', '1.0.0' );
3232
define( 'SF_UPGRADE_RUNNING', 'SF_UPGRADE_RUNNING' );

src/Products/Product.php

+13-2
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

+81-2
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)