Support » Plugin: Facebook for WooCommerce » Snippet to delete products in FB catalog

  • Resolved halloy

    (@halloy)


    Hello,
    during the plugin configuration, I add a lot of product categories to be not added in the FB catalog. Otherwhise, they are all added. Before, I already added 40 products with a XML file. In this catalog, I find only these 40 products but not all them added by your plugin. But all products are in the FB shop of my page ! So, I found your snippet to delete products in the catalog. I added it in functions.php. But how can I launch it ?? Because all products are always displayed on the FB Shop page !

    Thanks a lot !
    The code snippet added :

    function sv_wc_facebook_delete_all_products() {
    
    	if ( ! function_exists( 'facebook_for_woocommerce' ) ) {
    		return;
    	}
    
    	if ( get_option( 'sv_wc_facebook_delete_all_products', false ) || ! is_admin() ) {
    		return;
    	}
    
    	$offset         = (int) get_option( 'sv_wc_facebook_delete_all_products_offset', 0 );
    	$posts_per_page = 500;
    
    	do {
    
    		$products = get_posts( array(
    			'post_type'      => 'product',
    			'post_status'    => 'any',
    			'fields'         => 'ids',
    			'offset'         => $offset,
    			'posts_per_page' => $posts_per_page,
    			// uncomment and update the lines below to select specific taxonomy terms to update
    			// 'tax_query'      => array(
    			// 	array(
    			// 		'taxonomy' => 'product_cat',
    			// 		'field'    => 'term_id',
    			// 		'terms'    => array_merge( array( 849, 850, 851 ) ),
    			// 	),
    			// ),
    		) );
    
    		if ( ! empty( $products ) ) {
    
    			foreach ( $products as $product_id ) {
    				
    				$product = wc_get_product( $product_id );
    
    				if ( $product ) {
    					$retailer_ids[] = \WC_Facebookcommerce_Utils::get_fb_retailer_id( $product );
    				}
    
    				facebook_for_woocommerce()->get_products_sync_handler()->delete_products( $retailer_ids );
    			}
    		}
    
    		// increment offset
    		$offset += $posts_per_page;
    
    		// and keep track of how far we made it in case we hit a script timeout
    		update_option( 'sv_wc_facebook_delete_all_products_offset', $offset );
    
    	} while ( count( $products ) == $posts_per_page );  // while full set of results returned  (meaning there may be more results still to retrieve)
    
    	if ( count( $products ) !== $posts_per_page ) {
    		update_option( 'sv_wc_facebook_delete_all_products', 1 );
    	}
    }
    add_action( 'init', 'sv_wc_facebook_delete_all_products' );
Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter halloy

    (@halloy)

    Hello,
    It’s seems that your plugin automatically synchronize products at its installation, before the sync configuration. It’s not a good idea ! Because if we have products not accepted by FB (like medical products or wine…), they are sent to FB and it’s very bad !
    Waiting your reply, I unchecked FB sync for all products not accepted by FB and launched a sync. But these products are always in FB Shop ! The risk is to be blacklisted by FB ! I really need help to delete all before that !
    thanks !

    Plugin Support nathvi V. a11n

    (@nathvi)

    Hi there @halloy

    Thanks for your message!

    I recommend Code Snippets: https://wordpress.org/plugins/code-snippets/ this is a very useful plugin, you can add and activate the snippet from there. It removes the need to add custom snippets to your theme’s functions.php file.

    Regarding your second question, you are correct. Facebook will sync all products that are elegible as per these guidelines: https://woocommerce.com/document/facebook-for-woocommerce/#section-27

    If you need some products not to be synced, you can apply some of the suggestions there: stock being 0, disable sync from the product settings, or product is set to one of the not visible settings in the product Publish > Catalog visibility section

    Thread Starter halloy

    (@halloy)

    Hello,
    Thanks for your reply.
    About the snippet : added by Code Snippets or directly in functions.php, what is the difference ? How is it launched ? Is it necessary to add the name of the option in the url ? How ? Because it didn’t work. So I finally deleted all the catalog because I was afraid to be blacklisted by FB. This happened, I had not made the link with your plugin. Despite the selection of categories not to synchronize, it had sent everything to FB. Unable to remove products,and finally FB blocked my account !

    Regarding the FB sync, you tell me that I can apply somme of your suggestions. But what is the purpose of setting the selection of categories to exclude ? If the plugin send all products directly after its installation, this setting is useless 🙁 Your plugin works fine, is easy to configure. But only for them who want to send all products to FB. If you have products not accepted by FB, it’s dangerous…

    Plugin Support Sol J. a11n

    (@solstudioim)

    Hi @halloy

    About the snippet : added by Code Snippets or directly in functions.php, what is the difference ? How is it launched ?

    When you added directly to your theme functions.php file, as far as I understand it’s not saved to your database so it doesn’t add an extra data to your database.

    When it’s added to the Code Snippets plugin, you can easily turn it of and on without having to remove or modify the code in the functions.php file where you have to open it on file manager first etc.

    It’s executed when the theme loaded or as per the setting on the Code Snippets plugin.

    Because it didn’t work. So I finally deleted all the catalog because I was afraid to be blacklisted by FB. This happened, I had not made the link with your plugin. Despite the selection of categories not to synchronize, it had sent everything to FB. Unable to remove products,and finally FB blocked my account !

    I am sorry that you had that experience and it didn’t work.

    My understanding, it should not cause you to get banned or blocked by Facebook. Is your Facebook account or Commerce account banned?

    What is the purpose of setting the selection of categories to exclude ? If the plugin send all products directly after its installation, this setting is useless

    I haven’t tested this if that’s the case. I think the workaround is that, before installing Facebook for WooCommerce, you can set all products that are not to be synced to your Facebook catalog, to be in private mode.

    The exclusion setting is useful for the next product synchronization after the plugin is installed.

    I hope that helps!

    Thread Starter halloy

    (@halloy)

    Hi ol,
    Thanks for your reply.
    If I understand well (sorry, I’m French), if I add the snippet with Code Snippets, it’s just necessary to open a page of the admin to launch it ?

    Yes, this Commerce account is banned, only because the feed contained COVID masks ! I didn’t understand why because I had configured the plugin to not sync this category. So we created a new account but we have to be very careful !
    When I installed again your plugin, I added directly this rule, before to save the configuration. But it was too late ! All products are already in the FB Shop ! It should be a good idea to test it and to send products only after the configuration 🙂 Because when the sync is gone, it’s very difficult to fix this error. Perhaps it should be also a good idea to create a tuto for them who need to filter products like me, to read before to start anything, to avoid wasting time correcting everything after ;-). When I read threads, it seems I’m not alone in this case.

    Thus, if I understand well :
    1. All products to not sync -> private mode
    2. Install the plugin and configure it with exclusion
    3. Change status of these products to public mode
    4. Make a prayer that everything is correct
    Is it correct ?

    Thanks !

    • This reply was modified 1 year ago by halloy.
    Plugin Support Praveen a11n

    (@spraveenitpro)

    Automattic Happiness Engineer

    Hi @halloy ,

    1. All products to not sync -> private mode
    2. Install the plugin and configure it with exclusion
    3. Change status of these products to public mode
    4. Make a prayer that everything is correct
    Is it correct ?

    That is correct, I also recommend testing on a staging site that is a clone of your live site:

    ### Staging site and Backup:

    You can create a staging site using the  WP-Staging – https://wordpress.org/plugins/wp-staging/. This plugin lets you copy and redeploy a WordPress site with minimal work.

    Cheers!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Snippet to delete products in FB catalog’ is closed to new replies.