Skip to content

Commit

Permalink
feat: Add custom fields to product data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
David Evbodaghe committed Jan 21, 2025
1 parent 6530a5a commit a60ab23
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 4 deletions.
18 changes: 17 additions & 1 deletion includes/fbproduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,8 @@ public function prepare_product( $retailer_id = null, $type_to_prepare_for = sel
$brand = wp_strip_all_tags( WC_Facebookcommerce_Utils::get_store_name() );
}

$custom_fields = $this->get_facebook_specific_fields();

if ( self::PRODUCT_PREP_TYPE_ITEMS_BATCH === $type_to_prepare_for ) {
$product_data = array(
'title' => WC_Facebookcommerce_Utils::clean_string( $this->get_title() ),
Expand All @@ -675,6 +677,7 @@ public function prepare_product( $retailer_id = null, $type_to_prepare_for = sel
'price' => $this->get_fb_price( true ),
'availability' => $this->is_in_stock() ? 'in stock' : 'out of stock',
'visibility' => Products::is_product_visible( $this->woo_product ) ? \WC_Facebookcommerce_Integration::FB_SHOP_PRODUCT_VISIBLE : \WC_Facebookcommerce_Integration::FB_SHOP_PRODUCT_HIDDEN,
'custom_fields' => $custom_fields
);
$product_data = $this->add_sale_price( $product_data, true );
$gpc_field_name = 'google_product_category';
Expand Down Expand Up @@ -706,6 +709,7 @@ public function prepare_product( $retailer_id = null, $type_to_prepare_for = sel
'currency' => get_woocommerce_currency(),
'availability' => $this->is_in_stock() ? 'in stock' : 'out of stock',
'visibility' => Products::is_product_visible( $this->woo_product ) ? \WC_Facebookcommerce_Integration::FB_SHOP_PRODUCT_VISIBLE : \WC_Facebookcommerce_Integration::FB_SHOP_PRODUCT_HIDDEN,
'custom_fields' => $custom_fields
);

if ( self::PRODUCT_PREP_TYPE_NORMAL !== $type_to_prepare_for && ! empty( $video_urls ) ) {
Expand All @@ -731,7 +735,7 @@ public function prepare_product( $retailer_id = null, $type_to_prepare_for = sel
$product_data['quantity_to_sell_on_facebook'] = (int) max( 0, $this->woo_product->get_stock_quantity() );
} else if ( $this->woo_product->is_type( 'variation' ) ) {
$parent_product = wc_get_product( $this->woo_product->get_parent_id() );
if ( $parent_product && $parent_product->managing_stock() ) {
if ( $parent_product && $parent_product->managing_stock() ) {
$product_data['quantity_to_sell_on_facebook'] = (int) max( 0, $parent_product->get_stock_quantity() );
}
}
Expand Down Expand Up @@ -1064,5 +1068,17 @@ public function prepare_variants_for_group( $feed_data = false ) {
return $final_variants;
}

/**
* Returns information about which fields are using Facebook-specific values.
*
* @return array
*/
private function get_facebook_specific_fields(): array {
return array(
'has_fb_description' => (bool) get_post_meta($this->id, self::FB_PRODUCT_DESCRIPTION, true),
'has_fb_price' => (bool) get_post_meta($this->id, self::FB_PRODUCT_PRICE, true),
'has_fb_image' => (bool) get_post_meta($this->id, self::FB_PRODUCT_IMAGE, true)
);
}

}
12 changes: 12 additions & 0 deletions tests/Unit/WCFacebookCommerceIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,11 @@ public function test_on_product_save_existing_simple_product_sync_enabled_update
$facebook_product_data['description'] = 'Facebook product description.';
$facebook_product_data['price'] = '199 USD';
$facebook_product_data['google_product_category'] = 1718;
$facebook_product_data['custom_fields'] = [
'has_fb_description' => 'yes',
'has_fb_price' => 'yes',
'has_fb_image' => 'yes'
];

$requests = WC_Facebookcommerce_Utils::prepare_product_requests_items_batch($facebook_product_data);

Expand All @@ -553,8 +558,15 @@ public function test_on_product_save_existing_simple_product_sync_enabled_update
$this->assertEquals( 'yes', get_post_meta( $product_to_update->get_id(), Products::COMMERCE_ENABLED_META_KEY, true ) );
$this->assertEquals( 1718, get_post_meta( $product_to_update->get_id(), Products::GOOGLE_PRODUCT_CATEGORY_META_KEY, true ) );

// Verify Facebook-specific fields were saved
$facebook_product_to_update = new WC_Facebook_Product( $product_to_update->get_id() );
$updated_product_data = $facebook_product_to_update->prepare_product(null, \WC_Facebook_Product::PRODUCT_PREP_TYPE_ITEMS_BATCH );

$this->assertEquals('yes', $updated_product_data['custom_fields']['has_fb_description']);
$this->assertEquals('yes', $updated_product_data['custom_fields']['has_fb_price']);
$this->assertEquals('yes', $updated_product_data['custom_fields']['has_fb_image']);

// Verify the actual values are still stored in meta
$this->assertEquals( 'Facebook product description.', get_post_meta( $facebook_product_to_update->get_id(), WC_Facebook_Product::FB_PRODUCT_DESCRIPTION, true ) );
$this->assertEquals( '199', get_post_meta( $facebook_product_to_update->get_id(), WC_Facebook_Product::FB_PRODUCT_PRICE, true ) );
$this->assertEquals( 'http://example.orgFacebook product image.', get_post_meta( $facebook_product_to_update->get_id(), WC_Facebook_Product::FB_PRODUCT_IMAGE, true ) );
Expand Down
90 changes: 87 additions & 3 deletions tests/Unit/fbproductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,29 @@
class fbproductTest extends WP_UnitTestCase {
private $parent_fb_product;

/** @var \WC_Product_Simple */
protected $product;

/** @var \WC_Facebook_Product */
protected $fb_product;

public function setUp(): void {
parent::setUp();

// creating a simple product
$this->product = new \WC_Product_Simple();
$this->product->set_name('Test Product');
$this->product->set_regular_price('10');
$this->product->save();

$this->fb_product = new WC_Facebook_Product($this->product);
}

public function tearDown(): void {
parent::tearDown();
$this->product->delete(true);
}

/**
* Test it gets description from post meta.
* @return void
Expand Down Expand Up @@ -254,7 +277,7 @@ public function test_quantity_to_sell_on_facebook_when_manage_stock_is_on_for_va

$woo_variation = wc_get_product($woo_product->get_children()[0]);
$woo_variation->set_manage_stock('yes');
$woo_variation->set_stock_quantity(23);
$woo_variation->set_stock_quantity(23);

$fb_parent_product = new \WC_Facebook_Product($woo_product);
$fb_product = new \WC_Facebook_Product( $woo_variation, $fb_parent_product );
Expand Down Expand Up @@ -306,13 +329,13 @@ public function test_quantity_to_sell_on_facebook_when_manage_stock_is_off_for_v
}

/**
* Test GTIN is added for simple product
* 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();

Expand Down Expand Up @@ -360,4 +383,65 @@ public function test_gtin_for_variable_product_unset() {

$this->assertEquals(isset($data['gtin']), false);
}

public function test_prepare_product_with_default_fields() {
// test when no fb specific fields are set
$product_data = $this->fb_product->prepare_product();

$this->assertArrayHasKey('custom_fields', $product_data);
$this->assertEquals(false, $product_data['custom_fields']['has_fb_description']);
$this->assertEquals(false, $product_data['custom_fields']['has_fb_price']);
$this->assertEquals(false, $product_data['custom_fields']['has_fb_image']);
}

public function test_prepare_product_with_custom_fields() {
// Set facebook specific fields
$fb_description = 'Facebook specific description';
$fb_price = '15';
$fb_image = 'https:example.com/fb-image.jpg';

update_post_meta($this->product->get_id(), WC_Facebook_Product::FB_PRODUCT_DESCRIPTION, $fb_description);
update_post_meta($this->product->get_id(), WC_Facebook_Product::FB_PRODUCT_PRICE, $fb_price);
update_post_meta($this->product->get_id(), WC_Facebook_Product::FB_PRODUCT_IMAGE, $fb_image);

$product_data = $this->fb_product->prepare_product();

$this->assertArrayHasKey('custom_fields', $product_data);
$this->assertEquals(true, $product_data['custom_fields']['has_fb_description']);
$this->assertEquals(true, $product_data['custom_fields']['has_fb_price']);
$this->assertEquals(true, $product_data['custom_fields']['has_fb_image']);
}

public function test_prepare_product_with_mixed_fields() {
// Set only facebook description
$fb_description = 'Facebook specific description';

update_post_meta($this->product->get_id(), WC_Facebook_Product::FB_PRODUCT_DESCRIPTION, $fb_description);

$product_data = $this->fb_product->prepare_product();

$this->assertArrayHasKey('custom_fields', $product_data);
$this->assertEquals(true, $product_data['custom_fields']['has_fb_description']);
$this->assertEquals(false, $product_data['custom_fields']['has_fb_price']);
$this->assertEquals(false, $product_data['custom_fields']['has_fb_image']);
}

public function test_prepare_product_items_batch() {
// Test the PRODUCT_PREP_TYPE_ITEMS_BATCH preparation type
$fb_description = 'Facebook specific description';

update_post_meta($this->product->get_id(), WC_Facebook_Product::FB_PRODUCT_DESCRIPTION, $fb_description);

$product_data = $this->fb_product->prepare_product(null, WC_Facebook_Product::PRODUCT_PREP_TYPE_ITEMS_BATCH);

$this->assertArrayHasKey('custom_fields', $product_data);
$this->assertEquals(true, $product_data['custom_fields']['has_fb_description']);
$this->assertEquals(false, $product_data['custom_fields']['has_fb_price']);
$this->assertEquals(false, $product_data['custom_fields']['has_fb_image']);

// Also verify the main product data structure for items batch
$this->assertArrayHasKey('title', $product_data);
$this->assertArrayHasKey('description', $product_data);
$this->assertArrayHasKey('image_link', $product_data);
}
}

0 comments on commit a60ab23

Please sign in to comment.