我也有同样的需求,最后为此开发了自己的插件。
首先你需要在你的插件中使用PHPExcel。之后读取您的 xls 文件并使用 woocommerce 功能导入产品。
读取您的 xls 文件并将其转换为具有以下结构的 PHP 数组:
function fileInitializer($file, $needCells){
$array_data = array();
require_once 'libraries/PHPExcel/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$target_dir = untrailingslashit( dirname( __FILE__ ) )."/upload-file/".$file;
$objReader= new PHPExcel_Reader_Excel5();
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load( $target_dir);
$rowIterator = $objPHPExcel->getActiveSheet()->getRowIterator();
$rowIndex = 0;
foreach($rowIterator as $row){
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
if(1 == $row->getRowIndex ()) continue;
foreach ($cellIterator as $cell) {
foreach($needCells as $needCell){
if($needCell['cell_name'] == $cell->getColumn()){
$array_data[$rowIndex][$needCell['array_name']] = fai_convert_string_to_persian($cell->getCalculatedValue());
}
}
}
$rowIndex++;
}
return $array_data;
}
$file = 'FILEPATH';
$needCells = array(
array('cell_name'=>'A', 'array_name'=>'id')
, array('cell_name'=>'B', 'array_name'=>'full_name')
);
$array_data = fileInitializer($file, $needCells);
在上述过程之后,在 $array_data 中添加一段时间并添加如下产品:
$post = array(
'post_author' => $user_id,
'post_content' => '',
'post_status' => "publish",
'post_title' => $value['product_name'],
'post_parent' => '',
'post_type' => "product",
);
$post_id = wp_insert_post( $post, $wp_error );
//ADDING EXTERA FEATURES
update_post_meta( $post_id, 'main_code_text_field', $value['main_code']
);
这些也是 woocommerce 元数据:
update_post_meta( $post_id, 'total_sales', '0');
update_post_meta( $post_id, '_downloadable', 'yes');
update_post_meta( $post_id, '_virtual', 'yes');
update_post_meta( $post_id, '_regular_price', "" );
update_post_meta( $post_id, '_sale_price', "");
update_post_meta( $post_id, '_purchase_note', "" );
update_post_meta( $post_id, '_featured', "no" );
update_post_meta( $post_id, '_weight', "" );
update_post_meta( $post_id, '_length', "" );
update_post_meta( $post_id, '_width', "" );
update_post_meta( $post_id, '_height', "" );
update_post_meta( $post_id, '_sku', "");
update_post_meta( $post_id, '_product_attributes', array());
update_post_meta( $post_id, '_sale_price_dates_from', "" );
update_post_meta( $post_id, '_sale_price_dates_to', "" );
update_post_meta( $post_id, '_sold_individually', "" );
update_post_meta( $post_id, '_manage_stock', "no" );
update_post_meta( $post_id, '_backorders', "no" );
update_post_meta( $post_id, '_stock', "" );