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 * diff --git a/tests/Unit/Product/Attributes/GtinMappingTest.php b/tests/Unit/Product/Attributes/GtinMappingTest.php new file mode 100644 index 0000000000..91fcf8674f --- /dev/null +++ b/tests/Unit/Product/Attributes/GtinMappingTest.php @@ -0,0 +1,131 @@ +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() ); + } +}