Skip to content

Commit

Permalink
Merge pull request #2810 from mshymon/gtin
Browse files Browse the repository at this point in the history
Fix - Sync product GTIN when available
  • Loading branch information
vahidkay-meta authored Dec 5, 2024
2 parents c263bd6 + 9861603 commit 20397ef
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
6 changes: 6 additions & 0 deletions includes/fbproduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

require_once __DIR__ . '/fbutils.php';

use WooCommerce\Facebook\Framework\Plugin\Compatibility;
use WooCommerce\Facebook\Framework\Helper;
use WooCommerce\Facebook\Products;

Expand Down Expand Up @@ -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 ) {
Expand Down
56 changes: 56 additions & 0 deletions tests/Unit/fbproductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 20397ef

Please sign in to comment.