From 8a71bcf2475a3c97b19e63afa6bba846ab3dd820 Mon Sep 17 00:00:00 2001 From: Clement Boirie Date: Tue, 18 Nov 2025 08:10:23 +0100 Subject: [PATCH] add new filters - Customize directory where the products's feed is written - Customize products's feed filename - Customize shipping method used when creating the shipping rate during order import --- src/Feed/FeedBuilder/FeedBuilder.php | 8 +++ src/Orders/Order/Products.php | 10 +++ src/Orders/Order/Shipping.php | 9 +++ src/ShoppingFeedHelper.php | 34 ++++++++-- tests/wpunit/Feed/HelperFunctionsTest.php | 83 +++++++++++++++++++++++ 5 files changed, 138 insertions(+), 6 deletions(-) create mode 100644 tests/wpunit/Feed/HelperFunctionsTest.php diff --git a/src/Feed/FeedBuilder/FeedBuilder.php b/src/Feed/FeedBuilder/FeedBuilder.php index e1b723d8..4193803a 100644 --- a/src/Feed/FeedBuilder/FeedBuilder.php +++ b/src/Feed/FeedBuilder/FeedBuilder.php @@ -164,6 +164,14 @@ public function render_feed( $lang = null ): void { * @return bool|\WP_Error */ protected function write_products_feed( string $file_path, array $products ) { + + /** + * Filter the list of products to be written to the feed. + * + * @param \WC_Product[] $products + */ + $products = apply_filters( 'shopping_feed_products_for_feed', $products ); + $products_list = Products::get_instance()->format_products( $products ); try { $generator = Generator::make( $file_path ); diff --git a/src/Orders/Order/Products.php b/src/Orders/Order/Products.php index 2c7ed403..358719af 100644 --- a/src/Orders/Order/Products.php +++ b/src/Orders/Order/Products.php @@ -82,6 +82,16 @@ private function mapping_product( $sf_product, $references_aliases = [] ) { $wc_product_id = $references_aliases[ $sf_product->getReference() ] ?? $sf_product->getReference(); if ( 'sku' === $product_identifier ) { + + /** + * Filter the SKU from the ShoppingFeed order. + * + * @param string $wc_product_id + * @param OrderItem $sf_product + * @param array $references_aliases + */ + $wc_product_id = apply_filters( 'shopping_feed_order_products_product_sku', $wc_product_id, $sf_product, $references_aliases ); + $wc_product_id = wc_get_product_id_by_sku( $wc_product_id ); if ( ! $wc_product_id ) { return array(); diff --git a/src/Orders/Order/Shipping.php b/src/Orders/Order/Shipping.php index 05c25975..7075a6ef 100644 --- a/src/Orders/Order/Shipping.php +++ b/src/Orders/Order/Shipping.php @@ -99,6 +99,15 @@ private function set_shipping_rate() { return; } $shipping_rate = ShoppingFeedHelper::get_wc_shipping_from_sf_carrier( $this->method ); + + /** + * Filter the shipping method data used when creating the shipping rates. + * + * @param array $shipping_rate the shipping method data. + * @param OrderResource $sf_order the ShoppingFeed order. + */ + $shipping_rate = apply_filters( 'shopping_feed_order_shipping_rate', $shipping_rate, $this->sf_order ); + if ( empty( $shipping_rate ) ) { $shipping_rate = $default_shipping_method; } diff --git a/src/ShoppingFeedHelper.php b/src/ShoppingFeedHelper.php index b9e3332b..6fdae3cd 100644 --- a/src/ShoppingFeedHelper.php +++ b/src/ShoppingFeedHelper.php @@ -56,27 +56,49 @@ public static function get_wc_version() { } /** - * Return the feed's directory + * Return the feed's directory. + * * @return string */ public static function get_feed_directory() { - return SF_FEED_DIR; + /** + * Filter the path to the directory where product feeds are stored. + * + * @param string $path Path to the directory. + */ + return (string) apply_filters( 'shopping_feed_feed_directory_path', SF_FEED_DIR ); } /** - * Return the feed's parts directory + * Return the feed's parts directory. + * * @return string */ public static function get_feed_parts_directory() { - return SF_FEED_PARTS_DIR; + /** + * Filter the path to the directory where product feeds parts are stored. + * + * @param string $path Path to the directory. + */ + return (string) apply_filters( 'shopping_feed_feed_parts_directory_path', SF_FEED_PARTS_DIR ); } /** - * Return the feed's file name + * Return the feed's file name. + * + * The filename doesn't contain the file extension. + * * @return string */ public static function get_feed_filename() { - return 'products'; + /** + * Filter the product feed's filename. + * + * The filename must not contain the file extension. + * + * @param string $path Feed's filename. + */ + return (string) apply_filters( 'shopping_feed_feed_filename', 'products' ); } /** diff --git a/tests/wpunit/Feed/HelperFunctionsTest.php b/tests/wpunit/Feed/HelperFunctionsTest.php new file mode 100644 index 00000000..e3d886ff --- /dev/null +++ b/tests/wpunit/Feed/HelperFunctionsTest.php @@ -0,0 +1,83 @@ +assertEquals( + self::$upload_dir['basedir'] . '/shopping-feed', + ShoppingFeedHelper::get_feed_directory() + ); + } + + public function test_get_feed_directory_filter() { + $custom_feed_directory = '/tmp/shopping-feed'; + + add_filter( + 'shopping_feed_feed_directory_path', + function ( $dir ) use ( $custom_feed_directory ) { + return $custom_feed_directory; + } + ); + + $this->assertEquals( + $custom_feed_directory, + ShoppingFeedHelper::get_feed_directory() + ); + } + + public function test_get_feed_part_directory() { + $this->assertEquals( + self::$upload_dir['basedir'] . '/shopping-feed/parts', + ShoppingFeedHelper::get_feed_parts_directory() + ); + } + + public function test_get_feed_part_directory_filter() { + $custom_feed_directory = '/tmp/shopping-feed-parts'; + + add_filter( + 'shopping_feed_feed_parts_directory_path', + function ( $dir ) use ( $custom_feed_directory ) { + return $custom_feed_directory; + } + ); + + $this->assertEquals( + $custom_feed_directory, + ShoppingFeedHelper::get_feed_parts_directory() + ); + } + + public function test_get_feed_filename() { + $this->assertEquals( + 'products', + ShoppingFeedHelper::get_feed_filename() + ); + } + + public function test_get_feed_filename_filter() { + $custom_feed_filename = 'custom-products'; + + add_filter( + 'shopping_feed_feed_filename', + function ( $filename ) use ( $custom_feed_filename ) { + return $custom_feed_filename; + } + ); + + $this->assertEquals( + $custom_feed_filename, + ShoppingFeedHelper::get_feed_filename() + ); + } +} \ No newline at end of file