This small plugin provide the functionality to add a button to upload CSV and then search the products by sku which are in first column of csv.
<?php
/*
Plugin Name: Multiple SKU Search in CSV
Plugin URI: https://www.tektalkpals.com/
Description: Add search by multiple sku with csv upload
Author: Tektalkpals
Version: 0.1.0
Author URI: https://www.tektalkpals.com/
Text Domain: multiple-sku-search
Domain Path: /languages/
License:
Released under the GPL license
http://www.gnu.org/copyleft/gpl.html
Copyright 2018
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Load text-domain of plugin
*/
load_plugin_textdomain( 'multiple-sku-search', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
$post_type = 'product';
/* hook my code */
add_action( 'restrict_manage_posts', 'mss_add_filter_to_product_list');
add_filter( 'parse_query', 'mss_filter_posts_by_multiple_sku');
add_action('admin_footer','mss_sku_csv_upload_callback',10);
function mss_add_filter_to_product_list(){
$post_type = isset($_GET['post_type'])?$_GET['post_type']:'product';
if ('product' == $post_type){ ?>
<input type="button" name="sku_csv" id="sku_csv" value="Upload SKU CSV to search" class="button">
<input type="hidden" name="sku_csv_id" id="sku_csv_id">
<?php
}
}
function mss_sku_csv_upload_callback(){?>
<script type="text/javascript">
jQuery(document).ready(function(){
$('#sku_csv').click(function(e) {
e.preventDefault();
var csv = wp.media({
title: 'Upload CSV',
multiple: false // mutiple: true if you want to upload multiple files at once
}).open()
.on('select', function(e){
// This will return the selected file from the Media Up-loader, the result is an object
var uploaded_csv = csv.state().get('selection').first();
// We convert uploaded file to a JSON object to make accessing it easier
var csv_id = uploaded_csv.toJSON().id;
$('#sku_csv_id').val(csv_id);
});
});
});
</script>
<?php
}
function mss_filter_posts_by_multiple_sku($query){
global $pagenow;
$post_type = isset($_GET['post_type'])?$_GET['post_type']:'product';
$sku_csv_id = isset($_GET['sku_csv_id'])?$_GET['sku_csv_id']:'';
if ( ('product'==$post_type) && is_admin() && ($pagenow=='edit.php') && ($sku_csv_id != '')) {
$sku_strings = mss_read_sku_csv($sku_csv_id);
if($sku_strings!=''){
$query->query_vars['meta_key'] = '_sku';
$query->query_vars['meta_value'] = $sku_strings;
$query->query_vars['meta_compare'] = 'IN';
}
}
}
function mss_read_sku_csv($sku_csv_id){
$file_path = get_attached_file( $sku_csv_id );
$sku_string = '';
if( file_exists($file_path) ){ /// check CSV file exists
$handle = fopen("$file_path", 'r');
$counter = 0;
while ( ($data = fgetcsv($handle)) !== FALSE ) {
if($counter == 0){ /// read csv headers
$counter++;
continue;
}
$counter++;
$sku_string .= ($counter==1)?'':',';
$sku_string .= $data[0];
}
}
return $sku_string;
}
Create a folder by name ‘multiple-sku-search’ under plugins directory of wordpress site. In this folder create a file ‘multiple-sku-search.php’ and copy-paste the above code in this file.
Now activate the plugin in wordpress site admin, It will add a button on woocommerce product listing panel to choose csv file. On choosing a csv file click on filter button there. It will show the desired results…