Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ver/6.11.0 #142

Merged
merged 5 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .plugin-data
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "6.10.0",
"version": "6.11.0",
"slug": "shopping-feed"
}
35 changes: 33 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

* Contributors: ShoppingFeed, BeAPI
* Tags: shoppingfeed, marketplace, woocommerce, woocommerce shoppingfeed, create woocommerce products shoppingfeed, products feed, generate shoppingfeed, amazon, Jet, Walmart, many marketplace, import orders
* Stable tag: 6.10.0
* Version: 6.10.0
* Stable tag: 6.11.0
* Version: 6.11.0
* Requires PHP: 7.3
* Requires at least: 5.7
* Tested up to: 6.7
Expand All @@ -15,6 +15,8 @@
> 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

## Changelog
* 6.11.0
* Variations : add new filter to modify variation's main image
* 6.10.0
* Orders : Redesigned the shipment tracking system.
* Orders (beta) : Add option to include VAT when importing orders.
Expand Down Expand Up @@ -315,7 +317,36 @@ function your_custom_fields_function($fields, $wc_product) {
```

### Variation Images

#### Main image
By default the variation's thumbnail is used as the main image in the feed.

You can customize the main image using the filter "shopping_feed_variation_main_image".

```php
add_filter( 'shopping_feed_variation_main_image', 'your_custom_variation_main_images_function', 10, 3 );

/**
* Use the parent's thumbnail if the variation doesn't have one.
*
* @param string $main_image The main image of the variation.
* @param \WC_Product_Variation $variation The variation.
* @param \WC_Product_Variable $product The product.
*
* @return string
*/
function your_custom_variation_main_images_function( $main_image, $variation, $product ) {
if ( empty( $main_image ) && has_post_thumbnail( $product->get_id() ) ) {
$main_image = get_the_post_thumbnail_url( $product->get_id(), 'full' );
}

return $main_image;
}
```

#### Additional images
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

```php
add_filter( 'shopping_feed_variation_images', 'your_custom_variation_images_function', 10, 3 );

Expand Down
34 changes: 32 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
## ShoppingFeed
Contributors: ShoppingFeed, BeAPI
Tags: shoppingfeed, marketplace, woocommerce, woocommerce shoppingfeed, create woocommerce products shoppingfeed, products feed, generate shoppingfeed, amazon, Jet, Walmart, many marketplace, import orders
Stable tag: 6.10.0
Version: 6.10.0
Stable tag: 6.11.0
Version: 6.11.0
Requires PHP: 7.3
Requires at least: 5.7
Tested up to: 6.7
Expand All @@ -13,6 +13,8 @@ WC tested up to: 9.6.0
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

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

### Variation Images

#### Main image
By default the variation's thumbnail is used as the main image in the feed.

You can customize the main image using the filter "shopping_feed_variation_main_image".

`
add_filter( 'shopping_feed_variation_main_image', 'your_custom_variation_main_images_function', 10, 3 );

/**
* Use the parent's thumbnail if the variation doesn't have one.
*
* @param string $main_image The main image of the variation.
* @param \WC_Product_Variation $variation The variation.
* @param \WC_Product_Variable $product The product.
*
* @return string
*/
function your_custom_variation_main_images_function( $main_image, $variation, $product ) {
if ( empty( $main_image ) && has_post_thumbnail( $product->get_id() ) ) {
$main_image = get_the_post_thumbnail_url( $product->get_id(), 'full' );
}

return $main_image;
}
`

#### Additional images
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

`
Expand Down
4 changes: 2 additions & 2 deletions shoppingfeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Author URI: https://www.shopping-feed.com/
* Text Domain: shopping-feed
* Domain Path: /languages
* Version: 6.10.0
* Version: 6.11.0
* Requires at least: 5.7
* Requires PHP: 7.3
* WC requires at least: 5.1.0
Expand All @@ -26,7 +26,7 @@
require_once plugin_dir_path( __FILE__ ) . '/vendor/autoload.php';
}

define( 'SF_VERSION', '6.10.0' );
define( 'SF_VERSION', '6.11.0' );
define( 'SF_DB_VERSION_SLUG', 'SF_DB_VERSION' );
define( 'SF_DB_VERSION', '1.0.0' );
define( 'SF_UPGRADE_RUNNING', 'SF_UPGRADE_RUNNING' );
Expand Down
15 changes: 13 additions & 2 deletions src/Products/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,10 +477,21 @@ public function get_variations( $for_feed = false ) {
$variation_data['width'] = $variation->get_width();
$variation_data['height'] = $variation->get_height();
$variation_data['length'] = $variation->get_length();
if ( ! empty( get_the_post_thumbnail_url( $variation->get_id(), 'full' ) ) ) {
$variation_data['image_main'] = get_the_post_thumbnail_url( $variation->get_id(), 'full' );

$main_image = '';
if ( has_post_thumbnail( $variation->get_id() ) ) {
$main_image = get_the_post_thumbnail_url( $variation->get_id(), 'full' );
}

/**
* Filter the main image of the variation.
*
* @param string $main_image The main image of the variation.
* @param \WC_Product_Variation $variation The variation.
* @param \WC_Product_Variable $product The product.
*/
$variation_data['image_main'] = apply_filters( 'shopping_feed_variation_main_image', $main_image, $variation, $product );

$variation_data['attributes'] = $this->get_variation_attributes( $variation );
$variations[] = $variation_data;
}
Expand Down
Binary file added tests/_data/images/image1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/_data/images/image2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/_data/images/image3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/_data/images/image4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
83 changes: 81 additions & 2 deletions tests/wpunit/Feed/ProductFeedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ public function test_get_simple_product_dimensions_when_not_defined(){
public function test_get_variation_dimensions_when_it_not_defined() {

// Prepare the variable product object
$wc_variable_product = New \WC_Product_Variable();
$wc_variable_product = new \WC_Product_Variable();
$wc_variable_product->set_length( 5 );
$wc_variable_product->set_height( 10 );
$wc_variable_product->set_width( 15 );
Expand All @@ -366,7 +366,7 @@ public function test_get_variation_dimensions_when_it_not_defined() {
public function test_get_variation_dimensions_when_it_overrides_parent_dimensions(){

// Prepare the variable product object
$wc_variable_product = New \WC_Product_Variable();
$wc_variable_product = new \WC_Product_Variable();
$wc_variable_product->set_length( 5 );
$wc_variable_product->set_height( 10 );
$wc_variable_product->set_width( 15 );
Expand All @@ -386,4 +386,83 @@ public function test_get_variation_dimensions_when_it_overrides_parent_dimension
$this->assertEquals( 30, $sf_product->get_height(), 'Product height should be 30.' );
$this->assertEquals( 40, $sf_product->get_width(), 'Product width should be 40.' );
}

public function test_variation_set_custom_main_image() {
add_filter(
'shopping_feed_variation_main_image',
function( $image, $variation, $product ) {
return 'https://example.com/image.jpg';
},
10,
3
);

$image_id = $this->factory()->attachment->create_object(
[
'file' => codecept_data_dir( 'images/image1.png' ),
'post_mime_type' => 'image/png',
'post_title' => 'Test Image',
'post_content' => '',
'post_status' => 'inherit',
]
);

$variable_product = new \WC_Product_Variable();
$variable_product_id = $variable_product->save();

WC_Helper_Product::create_product_variation_object( $variable_product_id, 'variation-1', 10, [], true );

$sf_product = new Product( $variable_product_id );
$this->assertCount( 1, $sf_product->get_variations(), 'Variable product should have 1 variation.' );
$this->assertArrayHasKey( 'image_main', $sf_product->get_variations()[0], 'Variation should have an image_main key.' );
$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.' );
}

public function test_variation_use_thumbnail_as_main_image() {
$image_id = $this->factory()->attachment->create_object(
[
'file' => codecept_data_dir( 'images/image1.png' ),
'post_mime_type' => 'image/png',
'post_title' => 'Test Image',
'post_content' => '',
'post_status' => 'inherit',
]
);
$image_url = wp_get_attachment_image_url( $image_id, 'full' );

$variable_product = new \WC_Product_Variable();
$variable_product_id = $variable_product->save();

$variation_product = WC_Helper_Product::create_product_variation_object( $variable_product_id, 'variation-1', 10, [], true );
$variation_product->set_image_id( $image_id );
$variation_product->save();

$sf_product = new Product( $variable_product_id );
$this->assertCount( 1, $sf_product->get_variations(), 'Variable product should have 1 variation.' );
$this->assertArrayHasKey( 'image_main', $sf_product->get_variations()[0], 'Variation should have an image_main key.' );
$this->assertEquals( $image_url, $sf_product->get_variations()[0]['image_main'], 'Product main image should be empty.' );
}

public function test_variation_empty_main_image_if_no_image_set() {
$image_id = $this->factory()->attachment->create_object(
[
'file' => codecept_data_dir( 'images/image1.png' ),
'post_mime_type' => 'image/png',
'post_title' => 'Test Image',
'post_content' => '',
'post_status' => 'inherit',
]
);

$variable_product = new \WC_Product_Variable();
$variable_product->set_image_id( $image_id );
$variable_product_id = $variable_product->save();

WC_Helper_Product::create_product_variation_object( $variable_product_id, 'variation-1', 10, [], true );

$sf_product = new Product( $variable_product_id );
$this->assertCount( 1, $sf_product->get_variations(), 'Variable product should have 1 variation.' );
$this->assertArrayHasKey( 'image_main', $sf_product->get_variations()[0], 'Variation should have an image_main key.' );
$this->assertEquals( '', $sf_product->get_variations()[0]['image_main'], 'Product main image should be empty.' );
}
}