我有一个csv文件,用于在我的网站上批量导入产品。
当前文件仅支持2个图像字段。我想上传最多x张图片。
我尝试编辑下面的代码,但无济于事:
function facncy_design_menu(){
add_menu_page(__( 'Fancy Product CSV', 'textdomain' ), 'Fancy Product CSV','manage_options','csvimport','csv_import');
}
add_action( 'admin_menu', 'facncy_design_menu' );
function csv_import(){
global $wpdb;
$table_name = $wpdb->prefix.'fpd_products';
$table_name_views = $wpdb->prefix.'fpd_views';
if(isset($_POST["Import"])){
$filename=$_FILES["file"]["tmp_name"];
if($_FILES["file"]["size"] > 0)
{
$file = fopen($filename, "r");
$i=0;
while (($getData = fgetcsv($file, 10000, ",")) !== FALSE)
{
$i++;
if($i > 1){
require_once( ABSPATH . 'wp-admin/includes/file.php' );
$url = $getData[7];
$timeout_seconds = 5;
$temp_file = download_url( $url, $timeout_seconds );
if ( !is_wp_error( $temp_file ) ) {
$files_data = array(
'name' => basename($url), // ex: wp-header-logo.png
'type' => 'image/png',
'tmp_name' => $temp_file,
'error' => 0,
'size' => filesize($temp_file),
);
$overrides = array(
'test_form' => false,
'test_size' => true,
);
$result = wp_handle_sideload( $files_data, $overrides );
if ( !empty( $result['error'] ) ) {
// Insert any error handling here
} else {
$filename = $result['file'];
$local_url = $result['url'];
$type = $result['type'];
}
}
if(!empty($getData[8])){
$url2 = $getData[8];
$timeout_seconds2 = 5;
$temp_file2 = download_url( $url2, $timeout_seconds2 );
if ( !is_wp_error( $temp_file2 ) ) {
$files_data2 = array(
'name' => basename($url2), // ex: wp-header-logo.png
'type' => 'image/png',
'tmp_name' => $temp_file2,
'error' => 0,
'size' => filesize($temp_file2),
);
$overrides2 = array(
'test_form' => false,
'test_size' => true,
);
$result2 = wp_handle_sideload( $files_data2, $overrides2 );
if ( !empty( $result2['error'] ) ) {
// Insert any error handling here
} else {
$filename2 = $result2['file'];
$local_url2 = $result2['url'];
$type2 = $result2['type'];
}
}
}
$option_data = '{"stage_width":"500","stage_width":"500"}';
$results = $wpdb->insert($table_name, array( 'title' =>$getData[6] , 'options' => $option_data , 'thumbnail' => $local_url ));
$lastInsertId = $wpdb->insert_id;
if($results == true){
$product_ID = wp_insert_post( array( 'post_type' => 'product', 'post_status' => 'publish', 'post_title' => $getData[0], 'post_content' => $getData[1], 'post_excerpt' => $getData[3] ) );
if($product_ID == true){
$attr_val = explode("|", $getData[10]);
$attr_name = 'pa_'.$getData[9];
$attributes = array(
$attr_name => array(
'name' => $attr_name,
'value' =>'',
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '1'
),
);
update_post_meta( $product_ID, '_product_attributes', $attributes );
wp_set_object_terms( $product_ID, $attr_val, $attr_name );
wp_set_object_terms( $product_ID, 'variable', 'product_type', false );
$parent_id = $product_ID;
$variation = array(
'post_title' => 'Product #' . $parent_id . ' Variation',
'post_content' => '',
'post_status' => 'publish',
'post_parent' => $parent_id,
'post_type' => 'product_variation'
);
// The variation id
$variation_id = wp_insert_post( $variation );
// Regular Price ( you can set other data like sku and sale price here )
update_post_meta( $variation_id, '_regular_price', $getData[5]);
update_post_meta( $variation_id, '_price', $getData[5]);
// Assign the size and color of this variation
update_post_meta( $variation_id, 'attribute_pa_color', '' );
update_post_meta( $variation_id, 'attribute_pa_size', '' );
$wp_filetype = wp_check_filetype( $local_url, null );
$attachment_data = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name( $local_url ),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment_data, $local_url, $product_ID );
set_post_thumbnail( $product_ID, $attach_id );
$product_category = explode(", ",$getData[4]);
foreach ($product_category as $key ) {
$term = get_term_by('name', $key, 'product_cat');
$termID = $term->term_id;
$result = wp_set_post_terms( $product_ID, $tags = $termID, $taxonomy = 'product_cat', $append = true );
}
update_post_meta($product_ID, '_sku' , $getData[3]);
update_post_meta($product_ID, 'fpd_products', $lastInsertId);
update_post_meta($product_ID, 'fpd_source_type' , 'product');
update_post_meta($product_ID, '_regular_price' , $getData[5]);
update_post_meta($product_ID, '_price', $getData[5]);
// update_post_meta($product_ID, '_product_attributes', $product_attr_serial);
$json_data ='[{"type":"image","title":"Front","source":"'.$local_url.'","parameters":{"locked":0,"uploadZone":0,"left":250,"top":250,"scaleX":1,"scaleY":1,"angle":0,"price":0,"originX":"center","originY":"center","colors":"'.$getData[11].'","opacity":1,"removable":0,"draggable":0,"rotatable":0,"resizable":0,"zChangeable":0,"topped":0,"autoSelect":0,"uniScalingUnlockable":0,"excludeFromExport":0,"bounding_box_control":0,"boundingBoxMode":"inside","curvable":0}}]';
$json_results = $wpdb->insert($table_name_views, array( 'product_id' =>$lastInsertId , 'title' =>'Front', 'thumbnail' => $local_url , 'elements' => $json_data, 'view_order' => 0 , 'options' => ''));
if(!empty($getData[8])){
$json_data2 ='[{"type":"image","title":"Back","source":"'.$local_url2.'","parameters":{"locked":0,"uploadZone":0,"left":250,"top":250,"scaleX":1,"scaleY":1,"angle":0,"price":0,"originX":"center","originY":"center","colors":"'.$getData[11].'","opacity":1,"removable":0,"draggable":0,"rotatable":0,"resizable":0,"zChangeable":0,"topped":0,"autoSelect":0,"uniScalingUnlockable":0,"excludeFromExport":0,"bounding_box_control":0,"boundingBoxMode":"inside","curvable":0}}]';
$json_results2 = $wpdb->insert($table_name_views, array( 'product_id' =>$lastInsertId , 'title' =>'Back', 'thumbnail' => $local_url2 , 'elements' => $json_data2, 'view_order' => 0, 'options' => '' ));
}
}
}
}
}
fclose($file);
}
}
?>
<div class="container">
<h2> Fancy Product CSV Import </h2>
<div class="panel panel-primary">
<div class="panel-body">
<form class="form-horizontal" action="" method="post" name="upload_excel" enctype="multipart/form-data">
<fieldset>
<!-- Form Name -->
<legend>Form Name</legend>
<!-- File Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="filebutton">Select File</label>
<div class="col-md-4">
<input type="file" name="file" id="file" class="input-large">
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="singlebutton">Import data</label>
<div class="col-md-4">
<button type="submit" id="submit" name="Import" class="btn btn-primary button-loading" data-loading-text="Loading...">Import</button>
</div>
</div>
</fieldset>
</form>
</div>