Skip to content

Commit 7566798

Browse files
committed
allow extra attributes to be added to the feed for variations
1 parent 5deffb1 commit 7566798

File tree

3 files changed

+82
-27
lines changed

3 files changed

+82
-27
lines changed

src/Feed/Generator.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ function (
155155
$product->setAttribute( $field['name'], $field['value'] );
156156
}
157157
}
158-
159158
}
160159
);
161160

@@ -206,6 +205,14 @@ function (
206205
if ( ! empty( $sf_product_variation['height'] ) ) {
207206
$variation->setAttribute( 'height', (string) $sf_product_variation['height'] );
208207
}
208+
if ( ! empty( $sf_product_variation['extra'] ) ) {
209+
foreach ( $sf_product_variation['extra'] as $field ) {
210+
if ( empty( $field['name'] ) ) {
211+
continue;
212+
}
213+
$variation->setAttribute( $field['name'], $field['value'] );
214+
}
215+
}
209216
}
210217
}
211218
);

src/Products/Product.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ public function get_variations( $for_feed = false ) {
493493
$variation_data['image_main'] = apply_filters( 'shopping_feed_variation_main_image', $main_image, $variation, $product );
494494

495495
$variation_data['attributes'] = $this->get_variation_attributes( $variation );
496+
$variation_data['extra'] = $this->get_variation_extra_fields( $variation );
496497
$variations[] = $variation_data;
497498
}
498499

@@ -544,6 +545,23 @@ public function get_extra_fields() {
544545
return apply_filters( 'shopping_feed_extra_fields', [], $this->product );
545546
}
546547

548+
/**
549+
* Get variation extra fields
550+
*
551+
* @param \WC_Product_Variation $variation
552+
*
553+
* @return array
554+
*/
555+
public function get_variation_extra_fields( $variation ) {
556+
/**
557+
* Filter extra fields written in the feed for the variation.
558+
*
559+
* @param array $extra_fields
560+
* @param \WC_Product_Variation $variation
561+
*/
562+
return apply_filters( 'shopping_feed_variation_extra_fields', [], $variation );
563+
}
564+
547565
/**
548566
* Get product's stock quantity.
549567
*

tests/wpunit/Feed/ProductFeedTest.php

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ public function test_attribute_on_simple_product_does_not_exist() {
308308
*
309309
* @author Stéphane Gillot
310310
*/
311-
public function test_get_simple_product_dimensions_when_defined(){
311+
public function test_get_simple_product_dimensions_when_defined() {
312312
$wc_product = WC_Helper_Product::create_simple_product();
313313
$wc_product->set_length( 5 );
314314
$wc_product->set_height( 10 );
@@ -329,9 +329,9 @@ public function test_get_simple_product_dimensions_when_defined(){
329329
*
330330
* @author Stéphane Gillot
331331
*/
332-
public function test_get_simple_product_dimensions_when_not_defined(){
332+
public function test_get_simple_product_dimensions_when_not_defined() {
333333
$wc_product = WC_Helper_Product::create_simple_product();
334-
$wc_product->set_length('' );
334+
$wc_product->set_length( '' );
335335
$wc_product->set_height( '' );
336336
$wc_product->set_width( '' );
337337
$wc_product->save();
@@ -363,7 +363,7 @@ public function test_get_variation_dimensions_when_it_not_defined() {
363363
$this->assertEquals( '', $sf_product->get_width(), 'Product width should be null.' );
364364
}
365365

366-
public function test_get_variation_dimensions_when_it_overrides_parent_dimensions(){
366+
public function test_get_variation_dimensions_when_it_overrides_parent_dimensions() {
367367

368368
// Prepare the variable product object
369369
$wc_variable_product = new \WC_Product_Variable();
@@ -374,9 +374,9 @@ public function test_get_variation_dimensions_when_it_overrides_parent_dimension
374374

375375
$variation = WC_Helper_Product::create_variation_product();
376376
$variation->set_parent_id( $wc_variable_product->get_id() );
377-
$variation->set_length(20);
378-
$variation->set_height(30);
379-
$variation->set_width(40);
377+
$variation->set_length( 20 );
378+
$variation->set_height( 30 );
379+
$variation->set_width( 40 );
380380
$variation->save();
381381

382382

@@ -390,7 +390,7 @@ public function test_get_variation_dimensions_when_it_overrides_parent_dimension
390390
public function test_variation_set_custom_main_image() {
391391
add_filter(
392392
'shopping_feed_variation_main_image',
393-
function( $image, $variation, $product ) {
393+
function ( $image, $variation, $product ) {
394394
return 'https://example.com/image.jpg';
395395
},
396396
10,
@@ -399,15 +399,15 @@ function( $image, $variation, $product ) {
399399

400400
$image_id = $this->factory()->attachment->create_object(
401401
[
402-
'file' => codecept_data_dir( 'images/image1.png' ),
402+
'file' => codecept_data_dir( 'images/image1.png' ),
403403
'post_mime_type' => 'image/png',
404-
'post_title' => 'Test Image',
405-
'post_content' => '',
406-
'post_status' => 'inherit',
404+
'post_title' => 'Test Image',
405+
'post_content' => '',
406+
'post_status' => 'inherit',
407407
]
408408
);
409-
410-
$variable_product = new \WC_Product_Variable();
409+
410+
$variable_product = new \WC_Product_Variable();
411411
$variable_product_id = $variable_product->save();
412412

413413
WC_Helper_Product::create_product_variation_object( $variable_product_id, 'variation-1', 10, [], true );
@@ -419,18 +419,18 @@ function( $image, $variation, $product ) {
419419
}
420420

421421
public function test_variation_use_thumbnail_as_main_image() {
422-
$image_id = $this->factory()->attachment->create_object(
422+
$image_id = $this->factory()->attachment->create_object(
423423
[
424-
'file' => codecept_data_dir( 'images/image1.png' ),
424+
'file' => codecept_data_dir( 'images/image1.png' ),
425425
'post_mime_type' => 'image/png',
426-
'post_title' => 'Test Image',
427-
'post_content' => '',
428-
'post_status' => 'inherit',
426+
'post_title' => 'Test Image',
427+
'post_content' => '',
428+
'post_status' => 'inherit',
429429
]
430430
);
431431
$image_url = wp_get_attachment_image_url( $image_id, 'full' );
432-
433-
$variable_product = new \WC_Product_Variable();
432+
433+
$variable_product = new \WC_Product_Variable();
434434
$variable_product_id = $variable_product->save();
435435

436436
$variation_product = WC_Helper_Product::create_product_variation_object( $variable_product_id, 'variation-1', 10, [], true );
@@ -446,14 +446,14 @@ public function test_variation_use_thumbnail_as_main_image() {
446446
public function test_variation_empty_main_image_if_no_image_set() {
447447
$image_id = $this->factory()->attachment->create_object(
448448
[
449-
'file' => codecept_data_dir( 'images/image1.png' ),
449+
'file' => codecept_data_dir( 'images/image1.png' ),
450450
'post_mime_type' => 'image/png',
451-
'post_title' => 'Test Image',
452-
'post_content' => '',
453-
'post_status' => 'inherit',
451+
'post_title' => 'Test Image',
452+
'post_content' => '',
453+
'post_status' => 'inherit',
454454
]
455455
);
456-
456+
457457
$variable_product = new \WC_Product_Variable();
458458
$variable_product->set_image_id( $image_id );
459459
$variable_product_id = $variable_product->save();
@@ -465,4 +465,34 @@ public function test_variation_empty_main_image_if_no_image_set() {
465465
$this->assertArrayHasKey( 'image_main', $sf_product->get_variations()[0], 'Variation should have an image_main key.' );
466466
$this->assertEquals( '', $sf_product->get_variations()[0]['image_main'], 'Product main image should be empty.' );
467467
}
468+
469+
/**
470+
* @covers \ShoppingFeed\ShoppingFeedWC\Products\Product::get_variation_extra_fields
471+
*/
472+
public function test_variation_extra_fields() {
473+
$extra_fields = [
474+
'field1' => 'value1',
475+
'field2' => 'value2',
476+
];
477+
478+
add_filter(
479+
'shopping_feed_variation_extra_fields',
480+
function ( $fields, $variation ) use ( $extra_fields ) {
481+
return $extra_fields;
482+
},
483+
10,
484+
2
485+
);
486+
487+
$variable_product = new \WC_Product_Variable();
488+
$variable_product_id = $variable_product->save();
489+
490+
WC_Helper_Product::create_product_variation_object( $variable_product_id, 'variation-1', 10, [], true );
491+
492+
$sf_product = new Product( $variable_product_id );
493+
$variations = $sf_product->get_variations();
494+
$this->assertCount( 1, $variations, 'Variable product should have 1 variation.' );
495+
$this->assertIsArray( reset( $variations )['extra'], 'Variation data should have a key "extra" containing an array.' );
496+
$this->assertEqualSets( $extra_fields, reset( $variations )['extra'] );
497+
}
468498
}

0 commit comments

Comments
 (0)