Skip to content
Open
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
77 changes: 50 additions & 27 deletions includes/fbproduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,15 @@ class WC_Facebook_Product {
*/
public function check_attribute_mapping($attribute_name) {
$sanitized_name = \WC_Facebookcommerce_Utils::sanitize_variant_name($attribute_name, false);

foreach (self::$standard_facebook_fields as $fb_field => $possible_matches) {
foreach ($possible_matches as $match) {
if (stripos($sanitized_name, $match) !== false) {
return $fb_field;
}
}
}

return false;
}

Expand All @@ -205,10 +205,10 @@ public function get_unmapped_attributes() {

foreach ($attributes as $attribute_name => $_) {
$value = $this->woo_product->get_attribute($attribute_name);

if (!empty($value)) {
$mapped_field = $this->check_attribute_mapping($attribute_name);

if ($mapped_field === false) {
$unmapped_attributes[] = array(
'name' => $attribute_name,
Expand Down Expand Up @@ -542,7 +542,7 @@ public function set_fb_brand( $fb_brand ) {

/**
* Utility method to set basic Facebook product attributes
*
*
* @param string $key The meta key to store the value under
* @param string $value The value to store
* @return void
Expand All @@ -557,11 +557,11 @@ private function set_fb_attribute($key, $value) {
$value
);
}

public function set_fb_material( $fb_material ) {
$this->set_fb_attribute(self::FB_MATERIAL, $fb_material);
}

public function set_fb_pattern( $fb_pattern ) {
$this->set_fb_attribute(self::FB_PATTERN, $fb_pattern);
}
Expand All @@ -577,7 +577,7 @@ public function set_fb_condition( $fb_condition ) {
public function set_fb_age_group( $fb_age_group ) {
$this->set_fb_attribute(self::FB_AGE_GROUP, $fb_age_group);
}

public function set_fb_gender( $fb_gender ) {
$this->set_fb_attribute(self::FB_GENDER, $fb_gender);
}
Expand Down Expand Up @@ -653,7 +653,7 @@ public function get_fb_brand() {
if (empty($fb_brand)) {
$brand = get_post_meta($this->id, Products::ENHANCED_CATALOG_ATTRIBUTES_META_KEY_PREFIX . 'brand', true);
$brand_taxonomy = get_the_term_list($this->id, 'product_brand', '', ', ');

if ($brand) {
$fb_brand = $brand;
} elseif (!is_wp_error($brand_taxonomy) && $brand_taxonomy) {
Expand Down Expand Up @@ -744,34 +744,34 @@ public function get_fb_short_description() {
$short_description = WC_Facebookcommerce_Utils::clean_string($parent_post->post_excerpt);
}
}

// If no parent description found, try getting the variation's own excerpt
if (empty($short_description)) {
$post = $this->get_post_data();
if ($post && !empty($post->post_excerpt)) {
$short_description = WC_Facebookcommerce_Utils::clean_string($post->post_excerpt);
}
}

// 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);
}

// Use the product's short description (excerpt) from WooCommerce
$post = $this->get_post_data();
$post_excerpt = WC_Facebookcommerce_Utils::clean_string($post->post_excerpt);

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);
Expand Down Expand Up @@ -957,7 +957,7 @@ public function get_fb_age_group() {
// If this is a variation, get its specific age group value
if ($this->is_type('variation')) {
$attributes = $this->woo_product->get_attributes();

foreach ($attributes as $key => $value) {
$attr_key = strtolower($key);
if ($attr_key === 'age_group') {
Expand Down Expand Up @@ -988,7 +988,7 @@ public function get_fb_gender() {
// If this is a variation, get its specific gender value
if ($this->is_type('variation')) {
$attributes = $this->woo_product->get_attributes();

foreach ($attributes as $key => $value) {
$attr_key = strtolower($key);
if ($attr_key === 'gender') {
Expand Down Expand Up @@ -1025,7 +1025,7 @@ public function get_fb_size() {
// If this is a variation, get its specific size value
if ($this->is_type('variation')) {
$attributes = $this->woo_product->get_attributes();

foreach ($attributes as $key => $value) {
$attr_key = strtolower($key);
if ($attr_key === 'size') {
Expand Down Expand Up @@ -1062,7 +1062,7 @@ public function get_fb_color() {
// If this is a variation, get its specific color value
if ($this->is_type('variation')) {
$attributes = $this->woo_product->get_attributes();

foreach ($attributes as $key => $value) {
$attr_key = strtolower($key);
if ($attr_key === 'color' || $attr_key === 'colour') {
Expand Down Expand Up @@ -1098,7 +1098,7 @@ public function get_fb_material() {
// If this is a variation, get its specific material value
if ($this->is_type('variation')) {
$attributes = $this->woo_product->get_attributes();

// Check for material attribute
foreach ($attributes as $key => $value) {
$attr_key = strtolower($key);
Expand Down Expand Up @@ -1130,7 +1130,7 @@ public function get_fb_mpn() {
// If this is a variation, get its specific mpn value
if ($this->is_type('variation')) {
$attributes = $this->woo_product->get_attributes();

// Check for mpn attribute
foreach ($attributes as $key => $value) {
$attr_key = strtolower($key);
Expand Down Expand Up @@ -1167,7 +1167,7 @@ public function get_fb_pattern() {
// If this is a variation, get its specific material value
if ($this->is_type('variation')) {
$attributes = $this->woo_product->get_attributes();

// Check for material attribute
foreach ($attributes as $key => $value) {
$attr_key = strtolower($key);
Expand Down Expand Up @@ -1294,7 +1294,7 @@ public function prepare_product( $retailer_id = null, $type_to_prepare_for = sel
}

$image_urls = $this->get_all_image_urls();

// Replace WordPress sanitization's ampersand with a real ampersand.
$product_url = str_replace(
'&amp%3B',
Expand All @@ -1308,7 +1308,7 @@ public function prepare_product( $retailer_id = null, $type_to_prepare_for = sel
}

$categories = WC_Facebookcommerce_Utils::get_product_categories( $id );

$product_data = array();
$product_data[ 'description' ] = Helper::str_truncate( $this->get_fb_description(), self::MAX_DESCRIPTION_LENGTH );
$product_data[ 'short_description' ] = $this->get_fb_short_description();
Expand All @@ -1327,8 +1327,7 @@ public function prepare_product( $retailer_id = null, $type_to_prepare_for = sel
$product_data[ 'age_group' ] = $this->get_fb_age_group();
$product_data[ 'gender' ] = $this->get_fb_gender();
$product_data[ 'material' ] = Helper::str_truncate( $this->get_fb_material(), 100 );
$product_data[ 'woo_product_type' ] = $this->get_type();
$product_data[ 'unmapped_attributes' ] = $this->get_unmapped_attributes();
$product_data[ 'custom_fields' ] = $this->get_custom_fields();

if ( self::PRODUCT_PREP_TYPE_ITEMS_BATCH === $type_to_prepare_for ) {
$product_data['title'] = Helper::str_truncate( WC_Facebookcommerce_Utils::clean_string( $this->get_title() ), self::MAX_TITLE_LENGTH );
Expand All @@ -1345,7 +1344,7 @@ public function prepare_product( $retailer_id = null, $type_to_prepare_for = sel
$product_data['url'] = $product_url;
$product_data['price'] = $this->get_fb_price();
$product_data['currency'] = get_woocommerce_currency();

/**
* '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
Expand All @@ -1357,7 +1356,7 @@ public function prepare_product( $retailer_id = null, $type_to_prepare_for = sel
* @see https://github.com/woocommerce/facebook-for-woocommerce/issues/2593
*/
$product_data['category'] = $categories['categories'];

$product_data = $this->add_sale_price( $product_data );
}//end if

Expand Down Expand Up @@ -1730,4 +1729,28 @@ public function prepare_variants_for_group( $feed_data = false ) {
return $final_variants;
}

/**
* Get a set of custom fields not present in the facebook product data type
*
* @return array
*/
private function get_custom_fields(): array {
$custom_fields = array();
$product_type = $this->woo_product->get_type();

$custom_fields[ 'woo_product_type' ] = $product_type;

// All attributes set on the item. For variable and simple products, this is a map of
// [string => WC_Product_Attribute]. For product variations, this is a map of [string => ?string]. For now,
// sending only for product variations as those can lead to issues with the checkout page.
if ($product_type == 'variation') {
$custom_fields['product_variation_attributes'] = $this->woo_product->get_attributes();
}

// attributes that are not mapped to standard Facebook fields
$custom_fields[ 'unmapped_attributes' ] = $this->get_unmapped_attributes();

return $custom_fields;
}

}
69 changes: 35 additions & 34 deletions includes/fbproductfeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public function get_product_feed_header_row() {
'additional_image_link,sale_price_effective_date,sale_price,condition,' .
'visibility,gender,color,size,pattern,google_product_category,default_product,'.
'variant,gtin,quantity_to_sell_on_facebook,rich_text_description,external_update_time,'.
'external_variant_id'. PHP_EOL;
'external_variant_id,custom_fields'. PHP_EOL;
}


Expand Down Expand Up @@ -478,16 +478,16 @@ private function prepare_product_for_feed( $woo_product, &$attribute_variants )

// If this group has default variant value, log this product item
if ( isset( $parent_attribute_values['default_variant_id'] ) && ! empty( $parent_attribute_values['default_variant_id'] ) ) {
$this->has_default_product_count++;
$this->has_default_product_count ++;
} else {
$this->no_default_product_count++;
$this->no_default_product_count ++;
}
}

// log simple product
if ( ! isset( $product_data['default_product'] ) ) {

$this->no_default_product_count++;
$this->no_default_product_count ++;

$product_data['default_product'] = '';
}
Expand All @@ -509,36 +509,37 @@ private function prepare_product_for_feed( $woo_product, &$attribute_variants )
}

return $product_data['retailer_id'] . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'name' ) ) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'description' ) ) . ',' .
static::get_value_from_product_data( $product_data, 'image_url' ) . ',' .
static::get_value_from_product_data( $product_data, 'url' ) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'product_type' ) ) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'brand' ) ) . ',' .
static::format_string_for_feed( static::format_price_for_feed(
static::get_value_from_product_data( $product_data, 'price', 0 ),
static::get_value_from_product_data( $product_data, 'currency' )
)) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'availability' ) ) . ',' .
$item_group_id . ',' .
static::get_value_from_product_data( $product_data, 'checkout_url' ) . ',' .
static::format_additional_image_url( static::get_value_from_product_data( $product_data, 'additional_image_urls' ) ) . ',' .
static::format_string_for_feed( $sale_price_effective_date ) . ',' .
static::format_string_for_feed( $sale_price ) . ',' .
'new' . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'visibility' )) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'gender' )) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'color' )) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'size' )) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'pattern' )) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'google_product_category' )) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'default_product' )) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'variant' )) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'gtin' )) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'quantity_to_sell_on_facebook' )) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'rich_text_description' ) ) . ',' .
static::get_value_from_product_data( $product_data, 'external_update_time' ) . ',' .
static::get_value_from_product_data( $product_data, 'external_variant_id' ) . PHP_EOL;
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'name' ) ) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'description' ) ) . ',' .
static::get_value_from_product_data( $product_data, 'image_url' ) . ',' .
static::get_value_from_product_data( $product_data, 'url' ) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'product_type' ) ) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'brand' ) ) . ',' .
static::format_string_for_feed( static::format_price_for_feed(
static::get_value_from_product_data( $product_data, 'price', 0 ),
static::get_value_from_product_data( $product_data, 'currency' )
) ) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'availability' ) ) . ',' .
$item_group_id . ',' .
static::get_value_from_product_data( $product_data, 'checkout_url' ) . ',' .
static::format_additional_image_url( static::get_value_from_product_data( $product_data, 'additional_image_urls' ) ) . ',' .
static::format_string_for_feed( $sale_price_effective_date ) . ',' .
static::format_string_for_feed( $sale_price ) . ',' .
'new' . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'visibility' ) ) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'gender' ) ) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'color' ) ) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'size' ) ) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'pattern' ) ) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'google_product_category' ) ) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'default_product' ) ) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'variant' ) ) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'gtin' ) ) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'quantity_to_sell_on_facebook' ) ) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'rich_text_description' ) ) . ',' .
static::get_value_from_product_data( $product_data, 'external_update_time' ) . ',' .
static::get_value_from_product_data( $product_data, 'external_variant_id' ) . ',' .
static::get_value_from_product_data( $product_data, 'custom_fields' ) . PHP_EOL;
}

private static function format_additional_image_url( $product_image_urls ) {
Expand Down
Loading