我可能会采用的方法是使用几个钩子和瞬态:
add_action( 'woocsv_start_import', 'action_woocsv_start_import', 10, 0 );
使用此操作创建一个瞬态,它将在文件中存储一个包含所有 ID/SKU 的数组
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;
}
使用此过滤器将 ID 添加到存储在瞬态中的数组中(确保返回 product_id)
add_action( 'woocsv_after_save', 'action_woocsv_after_save', 10, 1 );
function action_woocsv_after_save( $instance ) {
// make action magic happen here...
}
使用此操作访问(和删除)瞬态。现在您可以访问所有 ID/SKU,您可以使用它们来查找未包含的 ID/SKU,然后将其删除。
我个人只会使用 WP All Import
编辑:
我觉得无聊,所以我写了逻辑。对我来说这似乎有点低效,但它应该是这样的伎俩。未经测试。
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();
}