From a46aac7c9fc5696a1b6ca26db392923b15e9d20d Mon Sep 17 00:00:00 2001 From: sgillot Date: Thu, 5 Jun 2025 11:24:32 +0200 Subject: [PATCH 1/2] issue/80993 : add a custom filter to add custom fields to variations --- src/Feed/Generator.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/Feed/Generator.php b/src/Feed/Generator.php index b0a55bda..64fd3e3b 100644 --- a/src/Feed/Generator.php +++ b/src/Feed/Generator.php @@ -233,6 +233,32 @@ function ( if ( ! empty( $sf_product_variation['height'] ) ) { $variation->setAttribute( 'height', (string) $sf_product_variation['height'] ); } + + /** + * Filter to add custom fields to product variations in the shopping feed. + * + * This filter allows developers to add custom meta fields or attributes + * to product variations before they are exported to the shopping feed XML. + * + * @param \ShoppingFeed\Feed\Product\ProductVariation $variation The variation object where custom attributes can be added using setAttribute() + * + * @param array $sf_product_variation The variation data from the shopping feed + * + * @param Product $sf_product The parent product object for additional context + * + * @return \ShoppingFeed\Feed\Product\ProductVariation The modified variation object + * + * @example + * // Add a custom text field to all variations + * add_filter( 'shopping_feed_variation_custom_fields', function( $variation, $sf_product_variation, $sf_product ) { + * $custom_field = get_post_meta( $sf_product_variation['id'], 'custom_field_meta_key', true ); + * if ( !empty( $custom_field ) ) { + * $variation->setAttribute( 'custom_field_name', $custom_field ); + * } + * return $variation; + * }, 10, 3); + */ + $variation = apply_filters( 'shopping_feed_variation_custom_fields', $variation, $sf_product_variation, $sf_product ); } } ); From 609e63285a1a0e06df977b1e1a05b1f5311f9d7d Mon Sep 17 00:00:00 2001 From: sgillot Date: Thu, 3 Jul 2025 18:14:59 +0200 Subject: [PATCH 2/2] issue/80993 : mimicry the way it is done for the product extra fields --- src/Feed/Generator.php | 36 +++++++++--------------------------- src/Products/Product.php | 8 ++++++++ 2 files changed, 17 insertions(+), 27 deletions(-) diff --git a/src/Feed/Generator.php b/src/Feed/Generator.php index 64fd3e3b..99004ee8 100644 --- a/src/Feed/Generator.php +++ b/src/Feed/Generator.php @@ -172,7 +172,6 @@ function ( if ( ! empty( $images ) ) { $product->setAdditionalImages( $sf_product->get_images() ); } - $extra_fields = $sf_product->get_extra_fields(); if ( ! empty( $extra_fields ) ) { foreach ( $extra_fields as $field ) { @@ -233,32 +232,15 @@ function ( if ( ! empty( $sf_product_variation['height'] ) ) { $variation->setAttribute( 'height', (string) $sf_product_variation['height'] ); } - - /** - * Filter to add custom fields to product variations in the shopping feed. - * - * This filter allows developers to add custom meta fields or attributes - * to product variations before they are exported to the shopping feed XML. - * - * @param \ShoppingFeed\Feed\Product\ProductVariation $variation The variation object where custom attributes can be added using setAttribute() - * - * @param array $sf_product_variation The variation data from the shopping feed - * - * @param Product $sf_product The parent product object for additional context - * - * @return \ShoppingFeed\Feed\Product\ProductVariation The modified variation object - * - * @example - * // Add a custom text field to all variations - * add_filter( 'shopping_feed_variation_custom_fields', function( $variation, $sf_product_variation, $sf_product ) { - * $custom_field = get_post_meta( $sf_product_variation['id'], 'custom_field_meta_key', true ); - * if ( !empty( $custom_field ) ) { - * $variation->setAttribute( 'custom_field_name', $custom_field ); - * } - * return $variation; - * }, 10, 3); - */ - $variation = apply_filters( 'shopping_feed_variation_custom_fields', $variation, $sf_product_variation, $sf_product ); + $variation_extra_fields = $sf_product->get_variation_extra_fields( $variation ); + if ( ! empty( $variation_extra_fields ) ) { + foreach ( $variation_extra_fields as $variation_extra_field ) { + if ( empty( $variation_extra_field['name'] ) ) { + continue; + } + $product->setAttribute( $variation_extra_field['name'], $variation_extra_field['value'] ); + } + } } } ); diff --git a/src/Products/Product.php b/src/Products/Product.php index 3548fc69..af22a02d 100644 --- a/src/Products/Product.php +++ b/src/Products/Product.php @@ -544,6 +544,14 @@ public function get_extra_fields() { return apply_filters( 'shopping_feed_extra_fields', [], $this->product ); } + /** + * Get Variation Extra fields + * Field : ['name'=>'', 'value'=>''] + */ + public function get_variation_extra_fields( $variation ) { + return apply_filters( 'shopping_feed_variation_extra_fields', [], $variation ); + } + /** * Get product's stock quantity. *