From 37856fc1ad4f985147a7988966c2c46b6491e16b Mon Sep 17 00:00:00 2001 From: Vinod Kumar Date: Sat, 20 Sep 2025 17:50:43 +0100 Subject: [PATCH 1/2] Removed get_product_fbid graph api functionality --- facebook-commerce.php | 64 ----------- .../WCFacebookCommerceIntegrationTest.php | 107 ------------------ 2 files changed, 171 deletions(-) diff --git a/facebook-commerce.php b/facebook-commerce.php index 78789d048..235752991 100644 --- a/facebook-commerce.php +++ b/facebook-commerce.php @@ -3199,70 +3199,6 @@ public function on_product_bulk_edit_save( $product ) { } } - /** - * Gets Facebook product ID from meta or from Facebook API. - * - * @param string $fbid_type ID type (group or item) - * @param int $wp_id post ID - * @param WC_Facebook_Product|null $woo_product product - * - * @return string facebook product id or an empty string - */ - public function get_product_fbid( string $fbid_type, int $wp_id, $woo_product = null ) { - $fb_id = WC_Facebookcommerce_Utils::get_fbid_post_meta( $wp_id, $fbid_type ); - if ( $fb_id ) { - return $fb_id; - } - if ( ! $woo_product ) { - $woo_product = new WC_Facebook_Product( $wp_id ); - } - $products = WC_Facebookcommerce_Utils::get_product_array( $woo_product ); - // if the product with ID equal to $wp_id is variable, $woo_product will be the first child - $woo_product = new WC_Facebook_Product( current( $products ) ); - - $fb_retailer_id = WC_Facebookcommerce_Utils::get_fb_retailer_id( $woo_product ); - - try { - $response = $this->facebook_for_woocommerce->get_api()->get_product_facebook_ids( - $this->get_product_catalog_id(), - $fb_retailer_id - ); - - if ( $response->data && $response->data[0] && $response->data[0]['id'] ) { - $fb_id = self::FB_PRODUCT_GROUP_ID === $fbid_type - ? $response->data[0]['product_group']['id'] - : $response->data[0]['id']; - update_post_meta( $wp_id, $fbid_type, $fb_id ); - return $fb_id; - } elseif ( $response->id ) { - $fb_id = self::FB_PRODUCT_GROUP_ID === $fbid_type - ? $response->get_facebook_product_group_id() - : $response->id; - update_post_meta( $wp_id, $fbid_type, $fb_id ); - return $fb_id; - } - } catch ( Exception $e ) { - Logger::log( - 'There was an issue connecting to the Facebook API:' . $e->getMessage(), - [], - array( - 'should_send_log_to_meta' => false, - 'should_save_log_in_woocommerce' => true, - 'woocommerce_log_level' => \WC_Log_Levels::ERROR, - ) - ); - $this->display_error_message( - sprintf( - /* translators: Placeholders %1$s - original error message from Facebook API */ - esc_html__( 'There was an issue connecting to the Facebook API: %s', 'facebook-for-woocommerce' ), - $e->getMessage() - ) - ); - } - - return null; - } - /** * Display test result. **/ diff --git a/tests/Unit/WCFacebookCommerceIntegrationTest.php b/tests/Unit/WCFacebookCommerceIntegrationTest.php index 1d8fb97a7..635c0c4e8 100644 --- a/tests/Unit/WCFacebookCommerceIntegrationTest.php +++ b/tests/Unit/WCFacebookCommerceIntegrationTest.php @@ -2829,113 +2829,6 @@ public function test_update_fb_visibility_to_published_for_simple_product() { $this->assertEquals( 'yes', get_post_meta( $product->get_id(), Products::VISIBILITY_META_KEY, true ) ); } - /** - * Tests get product facebook id returns post meta value. - * - * @return void - */ - public function test_get_product_fbid_returns_post_meta_value() { - $product = WC_Helper_Product::create_simple_product(); - $product->add_meta_data( WC_Facebookcommerce_Integration::FB_PRODUCT_GROUP_ID, 'some-facebook-product-group-id' ); - $product->add_meta_data( WC_Facebookcommerce_Integration::FB_PRODUCT_ITEM_ID, 'some-facebook-product-item-id' ); - $product->save_meta_data(); - - $group_id = $this->integration->get_product_fbid( WC_Facebookcommerce_Integration::FB_PRODUCT_GROUP_ID, $product->get_id() ); - $item_id = $this->integration->get_product_fbid( WC_Facebookcommerce_Integration::FB_PRODUCT_ITEM_ID, $product->get_id() ); - - $this->assertEquals( 'some-facebook-product-group-id', $group_id ); - $this->assertEquals( 'some-facebook-product-item-id', $item_id ); - } - - /** - * Tests get product facebook id calls facebook graph api to get id and updates post meta group id value. - * - * @return void - */ - public function test_get_product_fbid_calls_facebook_and_sets_post_meta_value_for_group_id() { - add_option( WC_Facebookcommerce_Integration::OPTION_PRODUCT_CATALOG_ID, '1122334455' ); - - $product = WC_Helper_Product::create_simple_product(); - $fb_retailer_id = WC_Facebookcommerce_Utils::get_fb_retailer_id( new WC_Facebook_Product( $product->get_id() ) ); - - $this->api->expects( $this->once() ) - ->method( 'get_product_facebook_ids' ) - ->with( '1122334455', $fb_retailer_id ) - ->willReturn( new API\ProductCatalog\Products\Id\Response( '{"id":"product-id","product_group":{"id":"product-group-id"}}' ) ); - - $facebook_product_group_id = $this->integration->get_product_fbid( WC_Facebookcommerce_Integration::FB_PRODUCT_GROUP_ID, $product->get_id() ); - - $this->assertEquals( 'product-group-id', $facebook_product_group_id ); - $this->assertEquals( 'product-group-id', get_post_meta( $product->get_id(), WC_Facebookcommerce_Integration::FB_PRODUCT_GROUP_ID, true ) ); - } - - /** - * Tests get product facebook id calls facebook graph api to get id and updates post meta item id value. - * - * @return void - */ - public function test_get_product_fbid_calls_facebook_and_sets_post_meta_value_for_item_id() { - add_option( WC_Facebookcommerce_Integration::OPTION_PRODUCT_CATALOG_ID, '1122334455' ); - - $product = WC_Helper_Product::create_simple_product(); - $fb_retailer_id = WC_Facebookcommerce_Utils::get_fb_retailer_id( new WC_Facebook_Product( $product->get_id() ) ); - - $this->api->expects( $this->once() ) - ->method( 'get_product_facebook_ids' ) - ->with( '1122334455', $fb_retailer_id ) - ->willReturn( new API\ProductCatalog\Products\Id\Response( '{"id":"product-id","product_group":{"id":"product-group-id"}}' ) ); - - $facebook_product_id = $this->integration->get_product_fbid( WC_Facebookcommerce_Integration::FB_PRODUCT_ITEM_ID, $product->get_id() ); - - $this->assertEquals( 'product-id', $facebook_product_id ); - $this->assertEquals( 'product-id', get_post_meta( $product->get_id(), WC_Facebookcommerce_Integration::FB_PRODUCT_ITEM_ID, true ) ); - } - - /** - * Tests get product facebook id calls facebook graph api with the endpoint with filter to get id and updates - * post meta item id value. - * - * @return void - */ - public function test_get_product_fbid_calls_facebook_and_sets_post_meta_value_for_item_id_with_filter_endpoint() { - add_option( WC_Facebookcommerce_Integration::OPTION_PRODUCT_CATALOG_ID, '1122334455' ); - - $product = WC_Helper_Product::create_simple_product(); - $fb_retailer_id = WC_Facebookcommerce_Utils::get_fb_retailer_id( new WC_Facebook_Product( $product->get_id() ) ); - - $this->api->expects( $this->once() ) - ->method( 'get_product_facebook_ids' ) - ->with( '1122334455', $fb_retailer_id ) - ->willReturn( new API\ProductCatalog\Products\Id\Response( '{"data":[{"id":"product-id","product_group":{"id":"product-group-id"}}]}' ) ); - - $facebook_product_id = $this->integration->get_product_fbid( WC_Facebookcommerce_Integration::FB_PRODUCT_ITEM_ID, $product->get_id() ); - - $this->assertEquals( 'product-id', $facebook_product_id ); - $this->assertEquals( 'product-id', get_post_meta( $product->get_id(), WC_Facebookcommerce_Integration::FB_PRODUCT_ITEM_ID, true ) ); - } - - /** - * Tests get product facebook id calls facebook graph api with the endpoint with filter to find that the item doesn't exist. - * - * @return void - */ - public function test_get_product_fbid_calls_facebook_and_sets_post_meta_value_for_item_id_with_filter_endpoint_empty_data() { - add_option( WC_Facebookcommerce_Integration::OPTION_PRODUCT_CATALOG_ID, '1122334455' ); - - $product = WC_Helper_Product::create_simple_product(); - $fb_retailer_id = WC_Facebookcommerce_Utils::get_fb_retailer_id( new WC_Facebook_Product( $product->get_id() ) ); - - $this->api->expects( $this->once() ) - ->method( 'get_product_facebook_ids' ) - ->with( '1122334455', $fb_retailer_id ) - ->willReturn( new API\ProductCatalog\Products\Id\Response( '{"data":[]}' ) ); - - $facebook_product_id = $this->integration->get_product_fbid( WC_Facebookcommerce_Integration::FB_PRODUCT_ITEM_ID, $product->get_id() ); - - $this->assertEquals( null, $facebook_product_id ); - $this->assertEquals( null, get_post_meta( $product->get_id(), WC_Facebookcommerce_Integration::FB_PRODUCT_ITEM_ID, true ) ); - } - /** * Tests if meta diagnosis is enabled by default. * From 457b050bc2234a9e8c0e803cb8369792fa53db11 Mon Sep 17 00:00:00 2001 From: Vinod Kumar Date: Sat, 20 Sep 2025 18:04:32 +0100 Subject: [PATCH 2/2] Cleanup Product Delete Graph APIs --- .../ProductCatalog/Products/Id/Request.php | 36 ---- .../ProductCatalog/Products/Id/Response.php | 27 --- .../Products/Id/ProductsIdResponseTest.php | 191 ------------------ .../WCFacebookCommerceIntegrationTest.php | 89 -------- 4 files changed, 343 deletions(-) delete mode 100644 includes/API/ProductCatalog/Products/Id/Request.php delete mode 100644 includes/API/ProductCatalog/Products/Id/Response.php delete mode 100644 tests/Unit/Api/ProductCatalog/Products/Id/ProductsIdResponseTest.php diff --git a/includes/API/ProductCatalog/Products/Id/Request.php b/includes/API/ProductCatalog/Products/Id/Request.php deleted file mode 100644 index af39f78de..000000000 --- a/includes/API/ProductCatalog/Products/Id/Request.php +++ /dev/null @@ -1,36 +0,0 @@ - Products > Get Graph Api. - * - * @link https://developers.facebook.com/docs/marketing-api/reference/product-catalog/products/ - */ -class Request extends ApiRequest { - - /** - * @param string $facebook_product_catalog_id Facebook Product Catalog ID. - * @param string $facebook_product_retailer_id Facebook Product Retailer ID. - */ - public function __construct( string $facebook_product_catalog_id, string $facebook_product_retailer_id ) { - - /** - * We use the endpoint with filter to get the product id and group id for new products to check if the product is already synced to Facebook. - */ - $path = "/{$facebook_product_catalog_id}/products"; - parent::__construct( $path, 'GET' ); - - $this->set_params( - array( - 'filter' => '{"retailer_id":{"eq":"' . $facebook_product_retailer_id . '"}}', - 'fields' => 'id,product_group{id}', - ) - ); - } -} diff --git a/includes/API/ProductCatalog/Products/Id/Response.php b/includes/API/ProductCatalog/Products/Id/Response.php deleted file mode 100644 index e4570b8d2..000000000 --- a/includes/API/ProductCatalog/Products/Id/Response.php +++ /dev/null @@ -1,27 +0,0 @@ - Product Groups > Update Graph Api. - * - * @link https://developers.facebook.com/docs/marketing-api/reference/product-catalog/products/ - * @property-read string id Either request was successful or not. - * @property-read array product_group Product group data container containing facebook product group id - * e.g. product_group => [ id => ] - */ -class Response extends ApiResponse { - /** - * Returns product's Facebook product group id. - * - * @return string - */ - public function get_facebook_product_group_id(): string { - return $this->product_group['id'] ?? ''; - } -} diff --git a/tests/Unit/Api/ProductCatalog/Products/Id/ProductsIdResponseTest.php b/tests/Unit/Api/ProductCatalog/Products/Id/ProductsIdResponseTest.php deleted file mode 100644 index f5f9b8ab1..000000000 --- a/tests/Unit/Api/ProductCatalog/Products/Id/ProductsIdResponseTest.php +++ /dev/null @@ -1,191 +0,0 @@ -assertTrue( class_exists( Response::class ) ); - } - - /** - * Test that the class extends ApiResponse. - */ - public function test_class_extends_api_response() { - $response = new Response( '{}' ); - $this->assertInstanceOf( ApiResponse::class, $response ); - } - - /** - * Test get_facebook_product_group_id with valid data. - */ - public function test_get_facebook_product_group_id_with_valid_data() { - $response_data = json_encode( array( - 'id' => '123456789', - 'product_group' => array( - 'id' => '987654321' - ) - ) ); - - $response = new Response( $response_data ); - - $this->assertEquals( '987654321', $response->get_facebook_product_group_id() ); - } - - /** - * Test get_facebook_product_group_id with missing product_group. - */ - public function test_get_facebook_product_group_id_with_missing_product_group() { - $response_data = json_encode( array( - 'id' => '123456789' - ) ); - - $response = new Response( $response_data ); - - // Should return empty string when product_group is missing - $this->assertEquals( '', $response->get_facebook_product_group_id() ); - } - - /** - * Test get_facebook_product_group_id with missing id in product_group. - */ - public function test_get_facebook_product_group_id_with_missing_id() { - $response_data = json_encode( array( - 'id' => '123456789', - 'product_group' => array( - 'name' => 'Test Product Group' - ) - ) ); - - $response = new Response( $response_data ); - - // Should return empty string when id is missing in product_group - $this->assertEquals( '', $response->get_facebook_product_group_id() ); - } - - /** - * Test get_facebook_product_group_id with null product_group. - */ - public function test_get_facebook_product_group_id_with_null_product_group() { - $response_data = json_encode( array( - 'id' => '123456789', - 'product_group' => null - ) ); - - $response = new Response( $response_data ); - - // Should return empty string when product_group is null - $this->assertEquals( '', $response->get_facebook_product_group_id() ); - } - - /** - * Test get_facebook_product_group_id with empty response. - */ - public function test_get_facebook_product_group_id_with_empty_response() { - $response_data = json_encode( array() ); - - $response = new Response( $response_data ); - - // Should return empty string when response is empty - $this->assertEquals( '', $response->get_facebook_product_group_id() ); - } - - /** - * Test get_facebook_product_group_id with various ID formats. - */ - public function test_get_facebook_product_group_id_with_various_formats() { - $test_cases = array( - 'numeric_string' => '123456789', - 'alphanumeric' => 'abc123def456', - 'with_underscores' => 'group_123_456', - 'with_dashes' => 'group-123-456', - 'empty_string' => '', - 'zero' => '0', - ); - - foreach ( $test_cases as $description => $group_id ) { - $response_data = json_encode( array( - 'product_group' => array( - 'id' => $group_id - ) - ) ); - - $response = new Response( $response_data ); - - $this->assertEquals( $group_id, $response->get_facebook_product_group_id(), "Failed for: {$description}" ); - } - } - - /** - * Test accessing response data properties. - */ - public function test_response_data_properties() { - $response_data = json_encode( array( - 'id' => '123456789', - 'product_group' => array( - 'id' => '987654321', - 'name' => 'Test Group' - ), - 'other_field' => 'other_value' - ) ); - - $response = new Response( $response_data ); - - // Test accessing properties through magic getter - $this->assertEquals( '123456789', $response->id ); - $this->assertIsArray( $response->product_group ); - $this->assertEquals( '987654321', $response->product_group['id'] ); - $this->assertEquals( 'Test Group', $response->product_group['name'] ); - $this->assertEquals( 'other_value', $response->other_field ); - } - - /** - * Test with malformed JSON. - */ - public function test_with_malformed_json() { - $response = new Response( 'invalid json' ); - - // Should return empty string when JSON is invalid - $this->assertEquals( '', $response->get_facebook_product_group_id() ); - } - - /** - * Test with nested product group data. - */ - public function test_with_nested_product_group_data() { - $response_data = json_encode( array( - 'id' => '123456789', - 'product_group' => array( - 'id' => '987654321', - 'retailer_id' => 'SKU123', - 'availability' => 'in stock', - 'additional_info' => array( - 'category' => 'Electronics', - 'brand' => 'TestBrand' - ) - ) - ) ); - - $response = new Response( $response_data ); - - // Should still correctly extract the product group ID - $this->assertEquals( '987654321', $response->get_facebook_product_group_id() ); - - // Verify other data is accessible - $this->assertEquals( 'SKU123', $response->product_group['retailer_id'] ); - $this->assertEquals( 'in stock', $response->product_group['availability'] ); - } -} \ No newline at end of file diff --git a/tests/Unit/WCFacebookCommerceIntegrationTest.php b/tests/Unit/WCFacebookCommerceIntegrationTest.php index 1d8fb97a7..6d9e601cd 100644 --- a/tests/Unit/WCFacebookCommerceIntegrationTest.php +++ b/tests/Unit/WCFacebookCommerceIntegrationTest.php @@ -2847,95 +2847,6 @@ public function test_get_product_fbid_returns_post_meta_value() { $this->assertEquals( 'some-facebook-product-item-id', $item_id ); } - /** - * Tests get product facebook id calls facebook graph api to get id and updates post meta group id value. - * - * @return void - */ - public function test_get_product_fbid_calls_facebook_and_sets_post_meta_value_for_group_id() { - add_option( WC_Facebookcommerce_Integration::OPTION_PRODUCT_CATALOG_ID, '1122334455' ); - - $product = WC_Helper_Product::create_simple_product(); - $fb_retailer_id = WC_Facebookcommerce_Utils::get_fb_retailer_id( new WC_Facebook_Product( $product->get_id() ) ); - - $this->api->expects( $this->once() ) - ->method( 'get_product_facebook_ids' ) - ->with( '1122334455', $fb_retailer_id ) - ->willReturn( new API\ProductCatalog\Products\Id\Response( '{"id":"product-id","product_group":{"id":"product-group-id"}}' ) ); - - $facebook_product_group_id = $this->integration->get_product_fbid( WC_Facebookcommerce_Integration::FB_PRODUCT_GROUP_ID, $product->get_id() ); - - $this->assertEquals( 'product-group-id', $facebook_product_group_id ); - $this->assertEquals( 'product-group-id', get_post_meta( $product->get_id(), WC_Facebookcommerce_Integration::FB_PRODUCT_GROUP_ID, true ) ); - } - - /** - * Tests get product facebook id calls facebook graph api to get id and updates post meta item id value. - * - * @return void - */ - public function test_get_product_fbid_calls_facebook_and_sets_post_meta_value_for_item_id() { - add_option( WC_Facebookcommerce_Integration::OPTION_PRODUCT_CATALOG_ID, '1122334455' ); - - $product = WC_Helper_Product::create_simple_product(); - $fb_retailer_id = WC_Facebookcommerce_Utils::get_fb_retailer_id( new WC_Facebook_Product( $product->get_id() ) ); - - $this->api->expects( $this->once() ) - ->method( 'get_product_facebook_ids' ) - ->with( '1122334455', $fb_retailer_id ) - ->willReturn( new API\ProductCatalog\Products\Id\Response( '{"id":"product-id","product_group":{"id":"product-group-id"}}' ) ); - - $facebook_product_id = $this->integration->get_product_fbid( WC_Facebookcommerce_Integration::FB_PRODUCT_ITEM_ID, $product->get_id() ); - - $this->assertEquals( 'product-id', $facebook_product_id ); - $this->assertEquals( 'product-id', get_post_meta( $product->get_id(), WC_Facebookcommerce_Integration::FB_PRODUCT_ITEM_ID, true ) ); - } - - /** - * Tests get product facebook id calls facebook graph api with the endpoint with filter to get id and updates - * post meta item id value. - * - * @return void - */ - public function test_get_product_fbid_calls_facebook_and_sets_post_meta_value_for_item_id_with_filter_endpoint() { - add_option( WC_Facebookcommerce_Integration::OPTION_PRODUCT_CATALOG_ID, '1122334455' ); - - $product = WC_Helper_Product::create_simple_product(); - $fb_retailer_id = WC_Facebookcommerce_Utils::get_fb_retailer_id( new WC_Facebook_Product( $product->get_id() ) ); - - $this->api->expects( $this->once() ) - ->method( 'get_product_facebook_ids' ) - ->with( '1122334455', $fb_retailer_id ) - ->willReturn( new API\ProductCatalog\Products\Id\Response( '{"data":[{"id":"product-id","product_group":{"id":"product-group-id"}}]}' ) ); - - $facebook_product_id = $this->integration->get_product_fbid( WC_Facebookcommerce_Integration::FB_PRODUCT_ITEM_ID, $product->get_id() ); - - $this->assertEquals( 'product-id', $facebook_product_id ); - $this->assertEquals( 'product-id', get_post_meta( $product->get_id(), WC_Facebookcommerce_Integration::FB_PRODUCT_ITEM_ID, true ) ); - } - - /** - * Tests get product facebook id calls facebook graph api with the endpoint with filter to find that the item doesn't exist. - * - * @return void - */ - public function test_get_product_fbid_calls_facebook_and_sets_post_meta_value_for_item_id_with_filter_endpoint_empty_data() { - add_option( WC_Facebookcommerce_Integration::OPTION_PRODUCT_CATALOG_ID, '1122334455' ); - - $product = WC_Helper_Product::create_simple_product(); - $fb_retailer_id = WC_Facebookcommerce_Utils::get_fb_retailer_id( new WC_Facebook_Product( $product->get_id() ) ); - - $this->api->expects( $this->once() ) - ->method( 'get_product_facebook_ids' ) - ->with( '1122334455', $fb_retailer_id ) - ->willReturn( new API\ProductCatalog\Products\Id\Response( '{"data":[]}' ) ); - - $facebook_product_id = $this->integration->get_product_fbid( WC_Facebookcommerce_Integration::FB_PRODUCT_ITEM_ID, $product->get_id() ); - - $this->assertEquals( null, $facebook_product_id ); - $this->assertEquals( null, get_post_meta( $product->get_id(), WC_Facebookcommerce_Integration::FB_PRODUCT_ITEM_ID, true ) ); - } - /** * Tests if meta diagnosis is enabled by default. *