Skip to content
Closed
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
16 changes: 16 additions & 0 deletions includes/fbproduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,14 @@ public function get_fb_short_description() {
}
}

// If still no short description, check if main description is short enough
if (empty($short_description)) {
$main_description = WC_Facebookcommerce_Utils::clean_string($this->woo_product->get_description());
if (!empty($main_description) && strlen($main_description) <= 1000) {
$short_description = $main_description;
}
}

return apply_filters('facebook_for_woocommerce_fb_product_short_description', $short_description, $this->id);
}

Expand All @@ -763,6 +771,14 @@ public function get_fb_short_description() {
if (!empty($post_excerpt)) {
$short_description = $post_excerpt;
}

// If no short description (excerpt) found, check if main description is short enough
if (empty($short_description)) {
$post_content = WC_Facebookcommerce_Utils::clean_string($post->post_content);
if (!empty($post_content) && strlen($post_content) <= 1000) {
$short_description = $post_content;
}
}

/**
* Filters the FB product short description.
Expand Down
124 changes: 124 additions & 0 deletions tests/Unit/fbproductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,130 @@ public function test_get_fb_short_description() {
$filter->teardown_safely_immediately();
}

/**
* Test fallback to main description when it's less than 1000 characters.
*/
public function test_get_fb_short_description_fallback_to_short_main_description() {
// Arrange
$product = WC_Helper_Product::create_simple_product();
$short_description = 'Short main description';

// Set up the test conditions
$product->set_description($short_description);
$product->set_short_description(''); // Ensure short description is empty
$product->save();

// Act
$facebook_product = new \WC_Facebook_Product($product);
$result_description = $facebook_product->get_fb_short_description();

// Assert
$this->assertEquals(
$short_description,
$result_description,
'Short main description should be used when excerpt is empty'
);
}

/**
* Test fallback to main description when it's exactly 1000 characters.
*/
public function test_get_fb_short_description_fallback_to_exact_length_main_description() {
// Arrange
$product = WC_Helper_Product::create_simple_product();
$exact_length_description = str_repeat('a', 1000);

// Set up the test conditions
$product->set_description($exact_length_description);
$product->set_short_description(''); // Ensure short description is empty
$product->save();

// Act
$facebook_product = new \WC_Facebook_Product($product);
$result_description = $facebook_product->get_fb_short_description();

// Assert
$this->assertEquals(
$exact_length_description,
$result_description,
'Main description of exactly 1000 characters should be used when excerpt is empty'
);
$this->assertEquals(
1000,
strlen($result_description),
'Result description should be exactly 1000 characters long'
);
}

/**
* Test that main description is not used when it exceeds 1000 characters.
*/
public function test_get_fb_short_description_no_fallback_to_long_main_description() {
// Arrange
$product = WC_Helper_Product::create_simple_product();
$too_long_description = str_repeat('a', 1001);

// Set up the test conditions
$product->set_description($too_long_description);
$product->set_short_description(''); // Ensure short description is empty
$product->save();

// Act
$facebook_product = new \WC_Facebook_Product($product);
$result_description = $facebook_product->get_fb_short_description();

// Assert
$this->assertEquals(
'',
$result_description,
'Should not fallback to main description if it exceeds 1000 characters'
);
$this->assertNotEquals(
$too_long_description,
$result_description,
'Long description should not be used even if no other description is available'
);
$this->assertLessThan(
strlen($too_long_description),
strlen($result_description),
'Result description should be shorter than the too-long main description'
);
}

/**
* Test that short description is always preferred over main description, even when both are present
* and main description is short enough to be used.
*/
public function test_get_fb_short_description_prefers_short_over_main() {
// Arrange
$product = WC_Helper_Product::create_simple_product();
$short_description = 'Short product description';
$main_description = 'Main product description that is also short';

// Set up both descriptions
$product->set_description($main_description);
$product->set_short_description($short_description);
$product->save();

// Act
$facebook_product = new \WC_Facebook_Product($product);
$result_description = $facebook_product->get_fb_short_description();

// Assert
$this->assertEquals(
$short_description,
$result_description,
'Short description should be used when available, regardless of main description length'
);

// Verify we're not using the main description
$this->assertNotEquals(
$main_description,
$result_description,
'Main description should not be used when short description is available'
);
}

/**
* Test get_unmapped_attributes with no attributes
*/
Expand Down