Skip to content

Instantly share code, notes, and snippets.

@tamarazuk
Created September 22, 2020 03:07
Show Gist options
  • Select an option

  • Save tamarazuk/3eb26c7f7b29a0ac20c2593719feaca1 to your computer and use it in GitHub Desktop.

Select an option

Save tamarazuk/3eb26c7f7b29a0ac20c2593719feaca1 to your computer and use it in GitHub Desktop.
sv_wc_facebook_delete_all_products
<?php // only copy this line if needed
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' );
@atikullamerchant

Copy link
Copy Markdown

Hi Team,
I've added the snippet to my function.php file. However, I'm not sure whether it's running or not. Could you tell me how do I check if the snippet is running or not?

@modshoes

Copy link
Copy Markdown

Must agree with the chap above, wish there was clear instructions on how to get this to actually work !

Also copied into functions.php file, but there is not telling if its doing anything.

Any ideas Admin ???????

@atikullamerchant

Copy link
Copy Markdown

What exactly do I have to do to make the script run second time?

@sotirisa

Copy link
Copy Markdown

Hi, did you make to run? How exactly?

@CryoWebToken

Copy link
Copy Markdown

where in the world does this go and why is it not just a button in the facebook woocommerce plugin? always making things more difficult than they should be is facebooks way.

@sotirisa

Copy link
Copy Markdown

You must go to another plugin and you don't have problems. Facebook things doesn't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment