Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prioritise core WooCommerce GTIN field in product adapter #2621

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/Product/WCProductAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
martynmjones marked this conversation as resolved.
Show resolved Hide resolved
*/
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
*
Expand Down
131 changes: 131 additions & 0 deletions tests/Unit/Product/Attributes/GtinMappingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php
declare( strict_types=1 );

namespace Automattic\WooCommerce\GoogleListingsAndAds\Tests\Unit\Product\Attributes;

use Automattic\WooCommerce\GoogleListingsAndAds\Product\WCProductAdapter;
use PHPUnit\Framework\TestCase;
use WC_Helper_Product;

/**
* Class GtinMappingTest
*
* 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 $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';

/**
* Test GTIN mapping from WooCommerce Core Global Unique ID.
*
* @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 );

$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() );
}
}
Loading