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

Fix - Sync product GTIN when available #2810

Merged
merged 2 commits into from
Dec 5, 2024
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
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);
}
}
Loading