Skip to content

Commit

Permalink
Add rich text description to woo product sync to meta
Browse files Browse the repository at this point in the history
  • Loading branch information
David Evbodaghe committed Feb 5, 2025
1 parent 5dc6b02 commit 0f7fcb9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion facebook-commerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ private function save_product_settings( WC_Product $product ) {
$woo_product->set_description( sanitize_text_field( wp_unslash( $_POST[ self::FB_PRODUCT_DESCRIPTION ] ) ) );
$woo_product->set_rich_text_description( $_POST[ self::FB_PRODUCT_DESCRIPTION ] );
}

if ( isset( $_POST[ WC_Facebook_Product::FB_PRODUCT_PRICE ] ) ) {
$woo_product->set_price( sanitize_text_field( wp_unslash( $_POST[ WC_Facebook_Product::FB_PRODUCT_PRICE ] ) ) );
}
Expand Down
20 changes: 17 additions & 3 deletions includes/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1321,8 +1321,7 @@ public function add_product_variation_edit_fields( $index, $variation_data, $pos

$sync_enabled = 'no' !== $this->get_product_variation_meta( $variation, Products::SYNC_ENABLED_META_KEY, $parent );
$is_visible = ( $visibility = $this->get_product_variation_meta( $variation, Products::VISIBILITY_META_KEY, $parent ) ) ? wc_string_to_bool( $visibility ) : true;

$rich_text_description = $this->get_product_variation_meta( $variation, \WC_Facebookcommerce_Integration::FB_RICH_TEXT_DESCRIPTION, $parent );
$description = $this->get_product_variation_meta( $variation, \WC_Facebookcommerce_Integration::FB_PRODUCT_DESCRIPTION, $parent );
$price = $this->get_product_variation_meta( $variation, \WC_Facebook_Product::FB_PRODUCT_PRICE, $parent );
$image_url = $this->get_product_variation_meta( $variation, \WC_Facebook_Product::FB_PRODUCT_IMAGE, $parent );
$image_source = $variation->get_meta( Products::PRODUCT_IMAGE_SOURCE_META_KEY );
Expand Down Expand Up @@ -1351,6 +1350,21 @@ public function add_product_variation_edit_fields( $index, $variation_data, $pos
)
);

woocommerce_wp_textarea_input(
array(
'id' => sprintf( 'variable_%s%s', \WC_Facebookcommerce_Integration::FB_PRODUCT_DESCRIPTION, $index ),
'name' => sprintf( "variable_%s[$index]", \WC_Facebookcommerce_Integration::FB_PRODUCT_DESCRIPTION ),
'label' => __( 'Facebook Description', 'facebook-for-woocommerce' ),
'desc_tip' => true,
'description' => __( 'Custom (plain-text only) description for product on Facebook. If blank, product description will be used. If product description is blank, shortname will be used.', 'facebook-for-woocommerce' ),
'cols' => 40,
'rows' => 5,
'value' => $description,
'class' => 'enable-if-sync-enabled',
'wrapper_class' => 'form-row form-row-full',
)
);

woocommerce_wp_radio(
array(
'id' => "variable_fb_product_image_source$index",
Expand Down Expand Up @@ -1449,14 +1463,14 @@ public function save_product_variation_edit_fields( $variation_id, $index ) {
Products::set_product_visibility( $variation, self::SYNC_MODE_SYNC_AND_HIDE !== $sync_mode );
$posted_param = 'variable_' . \WC_Facebookcommerce_Integration::FB_PRODUCT_DESCRIPTION;
$description = isset( $_POST[ $posted_param ][ $index ] ) ? sanitize_text_field( wp_unslash( $_POST[ $posted_param ][ $index ] ) ) : null;
$posted_param = 'variable_' . \WC_Facebookcommerce_Integration::FB_RICH_TEXT_DESCRIPTION;
$posted_param = 'variable_fb_product_image_source';
$image_source = isset( $_POST[ $posted_param ][ $index ] ) ? sanitize_key( wp_unslash( $_POST[ $posted_param ][ $index ] ) ) : '';
$posted_param = 'variable_' . \WC_Facebook_Product::FB_PRODUCT_IMAGE;
$image_url = isset( $_POST[ $posted_param ][ $index ] ) ? esc_url_raw( wp_unslash( $_POST[ $posted_param ][ $index ] ) ) : null;
$posted_param = 'variable_' . \WC_Facebook_Product::FB_PRODUCT_PRICE;
$price = isset( $_POST[ $posted_param ][ $index ] ) ? wc_format_decimal( wc_clean( wp_unslash( $_POST[ $posted_param ][ $index ] ) ) ) : '';
$variation->update_meta_data( \WC_Facebookcommerce_Integration::FB_PRODUCT_DESCRIPTION, $description );
$variation->update_meta_data( \WC_Facebookcommerce_Integration::FB_RICH_TEXT_DESCRIPTION, $description );
$variation->update_meta_data( Products::PRODUCT_IMAGE_SOURCE_META_KEY, $image_source );
$variation->update_meta_data( \WC_Facebook_Product::FB_PRODUCT_IMAGE, $image_url );
$variation->update_meta_data( \WC_Facebook_Product::FB_PRODUCT_PRICE, $price );
Expand Down
7 changes: 6 additions & 1 deletion tests/Unit/WCFacebookCommerceIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ public function test_on_product_save_existing_simple_product_sync_enabled_update

$_POST[ WC_Facebook_Product::FB_REMOVE_FROM_SYNC ] = $product_to_delete->get_id();

$_POST[ WC_Facebookcommerce_Integration::FB_PRODUCT_DESCRIPTION ] = 'Facebook product description.';
$_POST[ WC_Facebook_Product::FB_PRODUCT_PRICE ] = '199';
$_POST['fb_product_image_source'] = 'Image source meta key value.';
$_POST[ WC_Facebook_Product::FB_PRODUCT_IMAGE ] = 'Facebook product image.';
Expand Down Expand Up @@ -523,7 +524,8 @@ public function test_on_product_save_existing_simple_product_sync_enabled_update
$facebook_product_data = $facebook_product->prepare_product(null, \WC_Facebook_Product::PRODUCT_PREP_TYPE_ITEMS_BATCH );
$this->integration->product_catalog_id = '123123123123123123';
/* Data coming from _POST data. */
$facebook_product_data['description'] = 'Dummy Product';
$facebook_product_data['description'] = 'Facebook product description.';
$facebook_product_data['rich_text_description'] = 'Facebook product description.';
$facebook_product_data['price'] = '199 USD';
$facebook_product_data['google_product_category'] = 1718;
$facebook_product_data['custom_fields'] = [
Expand Down Expand Up @@ -560,6 +562,9 @@ public function test_on_product_save_existing_simple_product_sync_enabled_update
$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( 'Facebook product description.', get_post_meta( $facebook_product_to_update->get_id(), WC_Facebook_Product::FB_PRODUCT_DESCRIPTION, true ) );
$this->assertEquals( 'Facebook product description.', get_post_meta( $facebook_product_to_update->get_id(), WC_Facebook_Product::FB_RICH_TEXT_DESCRIPTION, true ) );

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

0 comments on commit 0f7fcb9

Please sign in to comment.