diff --git a/includes/fbproduct.php b/includes/fbproduct.php index 9724541f..6121bc12 100644 --- a/includes/fbproduct.php +++ b/includes/fbproduct.php @@ -11,6 +11,7 @@ require_once __DIR__ . '/fbutils.php'; +use WooCommerce\Facebook\Framework\Plugin\Compatibility; use WooCommerce\Facebook\Framework\Helper; use WooCommerce\Facebook\Products; @@ -735,6 +736,11 @@ public function prepare_product( $retailer_id = null, $type_to_prepare_for = sel } } + // Add GTIN (Global Trade Item Number) + if ( Compatibility::is_wc_version_gte( '9.1.0' ) && $gtin = $this->woo_product->get_global_unique_id() ) { + $product_data['gtin'] = $gtin; + } + // Only use checkout URLs if they exist. $checkout_url = $this->build_checkout_url( $product_url ); if ( $checkout_url ) { diff --git a/tests/Unit/fbproductTest.php b/tests/Unit/fbproductTest.php index c8fbbdb0..8b5e7636 100644 --- a/tests/Unit/fbproductTest.php +++ b/tests/Unit/fbproductTest.php @@ -304,4 +304,60 @@ public function test_quantity_to_sell_on_facebook_when_manage_stock_is_off_for_v $this->assertEquals( $data['quantity_to_sell_on_facebook'], 128 ); } + + /** + * Test GTIN is added for simple product + * @return void + */ + public function test_gtin_for_simple_product_set() { + $woo_product = WC_Helper_Product::create_simple_product(); + $woo_product->set_global_unique_id(9504000059446); + + $fb_product = new \WC_Facebook_Product( $woo_product ); + $data = $fb_product->prepare_product(); + + $this->assertEquals( $data['gtin'], 9504000059446 ); + } + + /** + * Test GTIN is not added for simple product + * @return void + */ + public function test_gtin_for_simple_product_unset() { + $woo_product = WC_Helper_Product::create_simple_product(); + $fb_product = new \WC_Facebook_Product( $woo_product ); + $data = $fb_product->prepare_product(); + $this->assertEquals(isset($data['gtin']), false); + } + + /** + * Test GTIN is added for variable product + * @return void + */ + public function test_gtin_for_variable_product_set() { + $woo_product = WC_Helper_Product::create_variation_product(); + $woo_variation = wc_get_product($woo_product->get_children()[0]); + $woo_variation->set_global_unique_id(9504000059446); + + $fb_parent_product = new \WC_Facebook_Product($woo_product); + $fb_product = new \WC_Facebook_Product( $woo_variation, $fb_parent_product ); + $data = $fb_product->prepare_product(); + + $this->assertEquals( $data['gtin'], 9504000059446 ); + } + + /** + * Test GTIN is not added for variable product + * @return void + */ + public function test_gtin_for_variable_product_unset() { + $woo_product = WC_Helper_Product::create_variation_product(); + $woo_variation = wc_get_product($woo_product->get_children()[0]); + + $fb_parent_product = new \WC_Facebook_Product($woo_product); + $fb_product = new \WC_Facebook_Product( $woo_variation, $fb_parent_product ); + $data = $fb_product->prepare_product(); + + $this->assertEquals(isset($data['gtin']), false); + } }