From 8071f4fd8639c8e69560546965cbc5994435d76d Mon Sep 17 00:00:00 2001 From: David Evbodaghe Date: Wed, 5 Feb 2025 19:18:06 +0000 Subject: [PATCH] Add rich text description to woo product sync to meta --- facebook-commerce.php | 2 +- includes/fbproduct.php | 34 +++++++++++----------------------- 2 files changed, 12 insertions(+), 24 deletions(-) diff --git a/facebook-commerce.php b/facebook-commerce.php index c089166e..0fc573f5 100644 --- a/facebook-commerce.php +++ b/facebook-commerce.php @@ -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 ] ) ) ); } diff --git a/includes/fbproduct.php b/includes/fbproduct.php index 9286a6cf..6a355050 100644 --- a/includes/fbproduct.php +++ b/includes/fbproduct.php @@ -44,7 +44,6 @@ class WC_Facebook_Product { const MAX_TIME = 'T23:59+00:00'; const MIN_TIME = 'T00:00+00:00'; - public static $rich_text_description_source = WC_Facebookcommerce_Utils::WC_DESCRIPTION; static $use_checkout_url = array( 'simple' => 1, 'variable' => 1, @@ -67,11 +66,6 @@ class WC_Facebook_Product { private $fb_description; - /** - * @var string Facebook Rich Text Description. - */ - private $fb_rich_text_description; - /** * @var array Gallery URLs. */ @@ -476,9 +470,9 @@ public function get_fb_description() { * Get the rich text description for a product. * * This function retrieves the rich text product description based on the following logic: - * 1. Check if the facebook rich text description is set and not empty. + * 1. Check if the Facebook rich text description is set and not empty. * 2. If the rich text description is available, use it as the preferred description. - * 3. Otherwise, fall back to the plain text description made available by Woocommerce. + * 3. Otherwise, fall back to the plain text description made available by WooCommerce. * * @return string The rich text description for the product. */ @@ -494,16 +488,12 @@ public function get_rich_text_description() { // Try to get rich text description from post meta if description has been set if ( empty( $rich_text_description ) ) { - $temp_rich_text_description = get_post_meta( + $rich_text_description = get_post_meta( $this->id, self::FB_RICH_TEXT_DESCRIPTION, true ); - if ( $temp_rich_text_description ) { - self::$rich_text_description_source = WC_Facebookcommerce_Utils::FB_DESCRIPTION; - $rich_text_description = $temp_rich_text_description; - } } // For variable products, we want to use the rich text description of the variant. @@ -543,7 +533,7 @@ public function get_rich_text_description() { */ public function add_sale_price( $product_data, $for_items_batch = false ) { - $sale_price = $this->woo_product->get_sale_price(); + $sale_price = $this->woo_product->get_sale_price(); $sale_price_effective_date = ''; $sale_start = ''; $sale_end = ''; @@ -562,7 +552,7 @@ public function add_sale_price( $product_data, $for_items_batch = false ) { ( $sale_start == self::MIN_DATE_1 . self::MIN_TIME && $sale_end == self::MAX_DATE . self::MAX_TIME ) ? '' : $sale_start . '/' . $sale_end; - $sale_price = + $sale_price = intval( round( $this->get_price_plus_tax( $sale_price ) * 100 ) ); // Set Sale start and end as empty if set to default values @@ -738,7 +728,7 @@ public function prepare_product( $retailer_id = null, $type_to_prepare_for = sel WC_Facebookcommerce_Utils::get_product_categories( $id ); // Get brand attribute. - $brand = get_post_meta( $id, Products::ENHANCED_CATALOG_ATTRIBUTES_META_KEY_PREFIX . 'brand', true ); + $brand = get_post_meta( $id, Products::ENHANCED_CATALOG_ATTRIBUTES_META_KEY_PREFIX . 'brand', true ); $brand_taxonomy = get_the_term_list( $id, 'product_brand', '', ', ' ); if ( $brand ) { @@ -749,15 +739,14 @@ public function prepare_product( $retailer_id = null, $type_to_prepare_for = sel $brand = wp_strip_all_tags( WC_Facebookcommerce_Utils::get_store_name() ); } - $rich_text_description = $this->get_rich_text_description(); $custom_fields = $this->get_facebook_specific_fields(); if ( self::PRODUCT_PREP_TYPE_ITEMS_BATCH === $type_to_prepare_for ) { - $product_data = array_merge( + $product_data = array( array( 'title' => WC_Facebookcommerce_Utils::clean_string( $this->get_title() ), 'description' => $this->get_fb_description(), - 'rich_text_description' => $rich_text_description, + 'rich_text_description' => $this->get_rich_text_description(), 'image_link' => $image_urls[0], 'additional_image_link' => $this->get_additional_image_urls( $image_urls ), 'link' => $product_url, @@ -767,7 +756,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, + 'custom_fields' => $custom_fields, ), ); $product_data = $this->add_sale_price( $product_data, true ); @@ -776,14 +765,14 @@ public function prepare_product( $retailer_id = null, $type_to_prepare_for = sel $product_data['video'] = $video_urls; } } else { - $product_data = array_merge( + $product_data = array( array( 'name' => WC_Facebookcommerce_Utils::clean_string( $this->get_title() ), 'description' => $this->get_fb_description(), 'image_url' => $image_urls[0], 'additional_image_urls' => $this->get_additional_image_urls( $image_urls ), 'url' => $product_url, - 'rich_text_description' => $rich_text_description, + 'rich_text_description' => $this->get_rich_text_description(), /** * 'category' is a required field for creating a ProductItem object when posting to /{product_catalog_id}/products. * This field should have the Google product category for the item. Google product category is not a required field @@ -1175,4 +1164,3 @@ private function get_facebook_specific_fields(): array { } } -