if ( isset($_POST["csp_variation_submit"]) ) {
if ( isset($_FILES["csp_variation_file"])) {
//if there was an error uploading the file
if ($_FILES["csp_variation_file"]["error"] > 0) {
echo "File Upload Error Code: " . $_FILES["csp_variation_file"]["error"] . "<br />";
}
else {
// Print file details
// echo "Uploaded File: " . $_FILES["file"]["name"] . "<br />";
// echo "Type: " . $_FILES["file"]["type"] . "<br />";
// echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
// echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
$update_count = 0;
$tmpName = $_FILES["csp_variation_file"]["tmp_name"];
$variation_data = array_map('str_getcsv', file($tmpName));
array_shift($variation_data);
if (!empty($variation_data)):
foreach ($variation_data as $variation):
$variation_id = trim($variation[0]);
$regular_price = trim($variation[1]);
$minimum_price = trim($variation[2]);
if ( !$variation_id || $variation_id == '') {
continue;
}
$_product = wc_get_product( $variation_id );
if($_product){
update_post_meta($variation_id, '_regular_price', $regular_price);
update_post_meta($variation_id, 'minimum_price', $minimum_price);
$update_count++;
}
else{
echo 'Product Variation with ID '.$variation_id.' Not Exists <br>';
}
endforeach;
echo $update_count. ' total update done.';
endif;
//if file already exists
// if (file_exists(wp_upload_dir().'/variation_update/' . $_FILES["file"]["name"])) {
// echo $_FILES["file"]["name"] . " already exists. ";
// }
// else {
// //Store file in directory "upload" with the name of "uploaded_file.txt"
// $storagename = "uploaded_file.csv";
// move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $storagename);
// echo "Stored in: " . wp_upload_dir().'/variation_update/' . $_FILES["file"]["name"] . "<br />";
// }
}
} else {
echo "No file selected <br />";
}
}
// Set class property
// $this->product_carousal_setting = get_option( 'product_carousal_setting' );
?>
<div class="wrap">
<h2>Variation product CSV</h2>
<form action="" method="post" enctype="multipart/form-data">
<!-- <input type="file" name="csp_variation_file" value="" />-->
<!-- <input type="submit" name="submit" value="Save" /></form>-->
<?php
submit_button(__( 'Process Update', 'textdomain' ),'primary','csp_variation_submit'); ?>
</div>
<?php
Leave a Reply