Delete product if SKU does not exist in csv when importing with the woocommerce built-in CSV importer

1.9k views Asked by At

I am using the woocommerce plugin of wordpress to build a shop.

It seems that the built-in CSV importer has no option to delete products which are not in the CSV file used to create / update the current products:

For example: If in my shop the products with the SKUs 'a1', 'b1' and 'c1' exist and the admin imports a product list with the SKUs 'a1', 'b1' the product with with the SKU 'c1' should be deleted / removed.

I reviewed the code of the product importer and the abstract importer and it seems that no such option exist.

Anyone knows howto solve that problem without installing yet another plugin ? Maybe a hook where the SKUs of the CSV file used to import are aggregated so I could load the existing SKUs from the database, compare with the SKUs in the CSV and delete them myself ?

Thanks for any ideas / assistance ! Kind Regards, m.

1

There are 1 answers

0
David_2002 On

The way I'd probably go about is to use several hooks and transients:

add_action( 'woocsv_start_import', 'action_woocsv_start_import', 10, 0 ); 

Use this action to create a transient, which will store an array of all the IDs/SKUs in the file

add_filter( 'woocsv_get_product_id', 'filter_woocsv_get_product_id', 10, 2 );
function filter_woocsv_get_product_id( $product_id, $sku ) { 
    // make filter magic happen here... 
    return $product_id; 
}

Use this filter to add the ID to the array stored in the transient (making sure you return product_id)

add_action( 'woocsv_after_save', 'action_woocsv_after_save', 10, 1 ); 
function action_woocsv_after_save( $instance ) { 
    // make action magic happen here... 
}

Use this action to access (and delete) the transient. Now you have all the ID's/SKUs accessible to you and you can use them to find the ones that weren't included and delete them.

Personally I'd just use WP All Import

EDIT:

I got bored so I wrote the logic. It seems a bit inefficient to me but it should so the trick. UNTESTED.


add_action( 'woocsv_start_import', 'action_woocsv_start_import', 10, 0 ); 

function action_woocsv_start_import(){
    set_transient('namespace_skus_in_csv',array(),0);
}

add_filter( 'woocsv_get_product_id', 'filter_woocsv_get_product_id', 10, 2 );
function filter_woocsv_get_product_id( $product_id, $sku ) { 
    $skus = get_transient('namespace_skus_in_csv');
    $skus[] = $sku;
    set_transient('namespace_skus_in_csv',$ids,0);
    return $product_id; 
}

add_action( 'woocsv_after_save', 'action_woocsv_after_save', 10, 1 ); 
function action_woocsv_after_save( $instance ) { 
    $skus = get_transient('namespace_skus_in_csv');
    delete_transient('namespace_skus_in_csv');
    $args = array(
        'post_type' => 'product',
        'posts_per_page' => -1
    );
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ){ 
        while ( $loop->have_posts() ){ 
            $loop->the_post();
            global $product;
            $sku = $product->get_sku();
            if(!array_search($sku, $skus)){
                wp_delete_post(get_the_ID());
            }
        }
    }
    wp_reset_postdata();
}