From 4b0afb497915c947197a166b0205c39dd80a530d Mon Sep 17 00:00:00 2001 From: Martyn Jones Date: Fri, 20 Sep 2024 10:40:09 +0100 Subject: [PATCH 1/3] Map gtin value from WC core if it's available --- src/Product/WCProductAdapter.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/Product/WCProductAdapter.php b/src/Product/WCProductAdapter.php index 42717f1222..3e8f45e80a 100644 --- a/src/Product/WCProductAdapter.php +++ b/src/Product/WCProductAdapter.php @@ -109,6 +109,7 @@ public function mapTypes( $properties ) { $this->map_woocommerce_product(); $this->map_attribute_mapping_rules( $mapping_rules ); $this->map_gla_attributes( $gla_attributes ); + $this->map_gtin(); // Allow users to override the product's attributes using a WordPress filter. $this->override_attributes(); @@ -927,6 +928,28 @@ protected function map_gla_attributes( array $attributes ): WCProductAdapter { return $this; } + /** + * Map the WooCommerce core global unique ID (GTIN) value if it's available. + * + * @since x.x.x + * + * @return $this + */ + protected function map_gtin(): WCProductAdapter { + // compatibility-code "WC < 9.2" -- Core global unique ID field was added in 9.2 + if ( ! method_exists( $this->wc_product, 'get_global_unique_id' ) ) { + return $this; + } + + $global_unique_id = $this->wc_product->get_global_unique_id(); + + if ( ! empty( $global_unique_id ) ) { + $this->setGtin( $global_unique_id ); + } + + return $this; + } + /** * @param string $targetCountry * From c4b11d7e75f64e1d4f85083999d332f83df7a34c Mon Sep 17 00:00:00 2001 From: Martyn Jones Date: Fri, 20 Sep 2024 10:41:47 +0100 Subject: [PATCH 2/3] Add unit tests --- .../Product/Attributes/GtinMappingTest.php | 123 ++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 tests/Unit/Product/Attributes/GtinMappingTest.php diff --git a/tests/Unit/Product/Attributes/GtinMappingTest.php b/tests/Unit/Product/Attributes/GtinMappingTest.php new file mode 100644 index 0000000000..897ae2f00b --- /dev/null +++ b/tests/Unit/Product/Attributes/GtinMappingTest.php @@ -0,0 +1,123 @@ +set_global_unique_id( $this->core_gtin ); + + $adapter = new WCProductAdapter(); + $adapter->mapTypes( [ + 'wc_product' => $mock_product, + 'targetCountry' => 'US', + 'gla_attributes' => [ + 'gtin' => $this->gla_gtin + ], + ] ); + + $this->assertEquals( $this->core_gtin, $adapter->getGtin() ); + } + + /** + * Test GTIN mapping from Google for WooCommerce GTIN attribute. + * + * @return void + */ + public function test_gtin_populated_from_gla_gtin_attribute() { + $mock_product = WC_Helper_Product::create_simple_product( false ); + $mock_product->set_sku( $this->gla_attribute_mapping_gtin ); + + $adapter = new WCProductAdapter(); + $adapter->mapTypes( [ + 'wc_product' => $mock_product, + 'targetCountry' => 'US', + 'mapping_rules' => [ + [ + 'attribute' => 'gtin', + 'source' => 'product:sku', + 'category_condition_type' => 'all', + 'categories' => '' + ] + ], + 'gla_attributes' => [ + 'gtin' => $this->gla_gtin + ], + ] ); + + $this->assertEquals( $this->gla_gtin, $adapter->getGtin() ); + } + + /** + * Test GTIN mapping from Google for WooCommerce attribute mapping rules. + * + * @return void + */ + public function test_gtin_populated_from_attribute_mapping_rules() { + $mock_product = WC_Helper_Product::create_simple_product( false ); + $mock_product->set_sku( $this->gla_attribute_mapping_gtin ); + + $adapter = new WCProductAdapter(); + $adapter->mapTypes( [ + 'wc_product' => $mock_product, + 'targetCountry' => 'US', + 'mapping_rules' => [ + [ + 'attribute' => 'gtin', + 'source' => 'product:sku', + 'category_condition_type' => 'all', + 'categories' => '' + ] + ], + ] ); + + $this->assertEquals( $this->gla_attribute_mapping_gtin, $adapter->getGtin() ); + } + + /** + * Test GTIN remains empty when no data is available. + * + * @return void + */ + public function test_gtin_remains_empty_when_no_data_available() { + $mock_product = WC_Helper_Product::create_simple_product( false ); + + $adapter = new WCProductAdapter(); + $adapter->mapTypes( [ + 'wc_product' => $mock_product, + 'targetCountry' => 'US', + ] ); + + $this->assertEquals( '', $adapter->getGtin() ); + } +} From 55ba9bc8f548ad09482325f0d249b2d083c1c004 Mon Sep 17 00:00:00 2001 From: Martyn Jones Date: Fri, 20 Sep 2024 13:07:53 +0100 Subject: [PATCH 3/3] phpcs fixes --- .../Product/Attributes/GtinMappingTest.php | 126 ++++++++++-------- 1 file changed, 67 insertions(+), 59 deletions(-) diff --git a/tests/Unit/Product/Attributes/GtinMappingTest.php b/tests/Unit/Product/Attributes/GtinMappingTest.php index 897ae2f00b..91fcf8674f 100644 --- a/tests/Unit/Product/Attributes/GtinMappingTest.php +++ b/tests/Unit/Product/Attributes/GtinMappingTest.php @@ -12,111 +12,119 @@ * * Unit tests to confirm that the GTIN value is mapped correctly. * The value should be prioritised in the following order: - * + * * 1. WooCommerce Core: Global Unique ID * 2. Google for WooCommerce: GTIN attribute * 3. Google for WooCommerce: Attribute mapping rules */ class GtinMappingTest extends TestCase { - /** @var string $core_gtin Mock value to be used as the core gtin field. */ - private $core_gtin = '219837492834'; + /** @var string $core_gtin Mock value to be used as the core gtin field. */ + private $core_gtin = '219837492834'; - /** @var string $gla_gtin Mock value to be used as the Google for WooCommerce gtin field. */ - private $gla_gtin = 'gla-gtin-field'; + /** @var string $gla_gtin Mock value to be used as the Google for WooCommerce gtin field. */ + private $gla_gtin = 'gla-gtin-field'; - /** @var string $gla_attribute_mapping_gtin Mock value to be used as the Google for WooCommerce attribute mapping gtin value. */ - private $gla_attribute_mapping_gtin = 'gla-attribute-mapping-gtin'; + /** @var string $gla_attribute_mapping_gtin Mock value to be used as the Google for WooCommerce attribute mapping gtin value. */ + private $gla_attribute_mapping_gtin = 'gla-attribute-mapping-gtin'; /** * Test GTIN mapping from WooCommerce Core Global Unique ID. - * - * @return void + * + * @return void */ public function test_gtin_populated_from_wc_core_global_unique_id() { - $mock_product = WC_Helper_Product::create_simple_product( false ); - $mock_product->set_global_unique_id( $this->core_gtin ); + $mock_product = WC_Helper_Product::create_simple_product( false ); + $mock_product->set_global_unique_id( $this->core_gtin ); $adapter = new WCProductAdapter(); - $adapter->mapTypes( [ - 'wc_product' => $mock_product, - 'targetCountry' => 'US', - 'gla_attributes' => [ - 'gtin' => $this->gla_gtin - ], - ] ); + $adapter->mapTypes( + [ + 'wc_product' => $mock_product, + 'targetCountry' => 'US', + 'gla_attributes' => [ + 'gtin' => $this->gla_gtin, + ], + ] + ); $this->assertEquals( $this->core_gtin, $adapter->getGtin() ); } /** * Test GTIN mapping from Google for WooCommerce GTIN attribute. - * - * @return void + * + * @return void */ public function test_gtin_populated_from_gla_gtin_attribute() { - $mock_product = WC_Helper_Product::create_simple_product( false ); - $mock_product->set_sku( $this->gla_attribute_mapping_gtin ); + $mock_product = WC_Helper_Product::create_simple_product( false ); + $mock_product->set_sku( $this->gla_attribute_mapping_gtin ); $adapter = new WCProductAdapter(); - $adapter->mapTypes( [ - 'wc_product' => $mock_product, - 'targetCountry' => 'US', - 'mapping_rules' => [ - [ - 'attribute' => 'gtin', - 'source' => 'product:sku', - 'category_condition_type' => 'all', - 'categories' => '' - ] - ], - 'gla_attributes' => [ - 'gtin' => $this->gla_gtin - ], - ] ); + $adapter->mapTypes( + [ + 'wc_product' => $mock_product, + 'targetCountry' => 'US', + 'mapping_rules' => [ + [ + 'attribute' => 'gtin', + 'source' => 'product:sku', + 'category_condition_type' => 'all', + 'categories' => '', + ], + ], + 'gla_attributes' => [ + 'gtin' => $this->gla_gtin, + ], + ] + ); $this->assertEquals( $this->gla_gtin, $adapter->getGtin() ); } /** * Test GTIN mapping from Google for WooCommerce attribute mapping rules. - * - * @return void + * + * @return void */ public function test_gtin_populated_from_attribute_mapping_rules() { - $mock_product = WC_Helper_Product::create_simple_product( false ); - $mock_product->set_sku( $this->gla_attribute_mapping_gtin ); + $mock_product = WC_Helper_Product::create_simple_product( false ); + $mock_product->set_sku( $this->gla_attribute_mapping_gtin ); $adapter = new WCProductAdapter(); - $adapter->mapTypes( [ - 'wc_product' => $mock_product, - 'targetCountry' => 'US', - 'mapping_rules' => [ - [ - 'attribute' => 'gtin', - 'source' => 'product:sku', - 'category_condition_type' => 'all', - 'categories' => '' - ] - ], - ] ); + $adapter->mapTypes( + [ + 'wc_product' => $mock_product, + 'targetCountry' => 'US', + 'mapping_rules' => [ + [ + 'attribute' => 'gtin', + 'source' => 'product:sku', + 'category_condition_type' => 'all', + 'categories' => '', + ], + ], + ] + ); $this->assertEquals( $this->gla_attribute_mapping_gtin, $adapter->getGtin() ); } /** * Test GTIN remains empty when no data is available. - * - * @return void + * + * @return void */ public function test_gtin_remains_empty_when_no_data_available() { $mock_product = WC_Helper_Product::create_simple_product( false ); $adapter = new WCProductAdapter(); - $adapter->mapTypes( [ - 'wc_product' => $mock_product, - 'targetCountry' => 'US', - ] ); + $adapter->mapTypes( + [ + 'wc_product' => $mock_product, + 'targetCountry' => 'US', + ] + ); $this->assertEquals( '', $adapter->getGtin() ); }