Skip to content

Commit

Permalink
Merge pull request #2403 from woocommerce/fix/2370-fb_sync_status_is_…
Browse files Browse the repository at this point in the history
…incorrect_for_catalog_visibility_hidden
  • Loading branch information
rawdreeg authored Dec 1, 2022
2 parents f06d038 + cc1421c commit 79fa971
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
9 changes: 9 additions & 0 deletions facebook-commerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -769,13 +769,22 @@ public function on_product_save( int $wp_id ) {
$sync_mode = isset( $_POST['wc_facebook_sync_mode'] )
? sanitize_text_field( wp_unslash( $_POST['wc_facebook_sync_mode'] ) )
: Admin::SYNC_MODE_SYNC_DISABLED;

// Restore sync mode if product is marked as visible and meet all the other criteria for sync.
$catalog_visibility = isset( $_POST['_visibility'] ) ? wc_clean( wp_unslash( $_POST['_visibility'] ) ) : null;
if ( $catalog_visibility && 'hidden' !== $catalog_visibility && $product->is_visible() && $sync_mode !== Admin::SYNC_MODE_SYNC_AND_HIDE ) {
$sync_mode = facebook_for_woocommerce()->get_product_sync_validator( $product )->passes_all_checks_except_sync_field()
? Admin::SYNC_MODE_SYNC_AND_SHOW : $sync_mode;
}

// phpcs:enable WordPress.Security.NonceVerification.Missing
$sync_enabled = Admin::SYNC_MODE_SYNC_DISABLED !== $sync_mode;

if ( Admin::SYNC_MODE_SYNC_AND_SHOW === $sync_mode && $product->is_virtual() && 'bundle' !== $product->get_type() ) {
// force to Sync and hide
$sync_mode = Admin::SYNC_MODE_SYNC_AND_HIDE;
}

$products_to_delete_from_facebook = $this->get_removed_from_sync_products_to_delete();
if ( $product->is_type( 'variable' ) ) {
// check variations for deletion
Expand Down
7 changes: 6 additions & 1 deletion includes/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1142,10 +1142,16 @@ public function add_product_settings_tab( $tabs ) {
public function add_product_settings_tab_content() {
global $post;


// all products have sync enabled unless explicitly disabled
$sync_enabled = 'no' !== get_post_meta( $post->ID, Products::SYNC_ENABLED_META_KEY, true );
$is_visible = ( $visibility = get_post_meta( $post->ID, Products::VISIBILITY_META_KEY, true ) ) ? wc_string_to_bool( $visibility ) : true;

$product = wc_get_product( $post );
if ( $product && ! facebook_for_woocommerce()->get_product_sync_validator( $product )->passes_all_checks() ) {
$sync_enabled = false;
}

$description = get_post_meta( $post->ID, \WC_Facebookcommerce_Integration::FB_PRODUCT_DESCRIPTION, true );
$price = get_post_meta( $post->ID, \WC_Facebook_Product::FB_PRODUCT_PRICE, true );
$image_source = get_post_meta( $post->ID, Products::PRODUCT_IMAGE_SOURCE_META_KEY, true );
Expand Down Expand Up @@ -1240,7 +1246,6 @@ public function add_product_settings_tab_content() {
?>
</div>
<?php
$product = wc_get_product( $post );
$commerce_handler = facebook_for_woocommerce()->get_commerce_handler();
?>
<?php if ( $commerce_handler->is_connected() && $commerce_handler->is_available() ) : ?>
Expand Down
33 changes: 33 additions & 0 deletions includes/ProductSync/ProductValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,22 @@ public function validate_but_skip_status_check() {
$this->validate_product_title();
}

/**
* Validate whether the product should be synced to Facebook but skip the sync field check.
*
* @since x.x.x
* @throws ProductExcludedException|ProductInvalidException If product should not be synced.
*/
public function validate_but_skip_sync_field() {
$this->validate_sync_enabled_globally();
$this->validate_product_stock_status();
$this->validate_product_price();
$this->validate_product_visibility();
$this->validate_product_terms();
$this->validate_product_description();
$this->validate_product_title();
}

/**
* Validate whether the product should be synced to Facebook.
*
Expand Down Expand Up @@ -186,6 +202,23 @@ public function passes_product_sync_field_check(): bool {
return true;
}

/**
* Validate whether the product should be synced to Facebook, but skip the sync field validation.
*
* @return bool
*/
public function passes_all_checks_except_sync_field(): bool {
try {
$this->validate_but_skip_sync_field();
} catch ( ProductExcludedException $e ) {
return false;
} catch ( ProductInvalidException $e ) {
return false;
}

return true;
}

/**
* Check whether product sync is globally disabled.
*
Expand Down

0 comments on commit 79fa971

Please sign in to comment.