how to create plugin for upload csv file in wordpress ? please explain with full code

import csv file-
submit

wp_insert_post(array (
‘post_type’ => ‘saved-orders’,
‘post_title’ => ‘Test’,
‘post_content’ => $allTheCarts,
‘post_status’ => ‘publish’,
‘comment_status’ => ‘closed’, // if you prefer
‘ping_status’ => ‘closed’, // if you prefer
));

function cart_items_array() {

$carts2 = MultiCartget_carts();

// All the products
$allTheCarts = array();

foreach ( $carts2 as $cart_id => $cart2 ) {

// get array of items contained in a cart …
$items2 = MultiCartget_cart( $cart_id );

foreach ( $items2 as $item2_id => $item2 ) {
$compProduct = array();

$product_name = get_post($item2[‘product_id’])->post_title;
$familyterms = wp_get_post_terms( $item2[‘product_id’], ‘pa_product-family’);
$cat_terms = wp_get_post_terms( $item2[‘product_id’], ‘pa_product-category’);
$product_sku = get_post_meta( $item2[‘product_id’], ‘_sku’, true );

$compProduct[] = $product_sku;

foreach ($cat_terms as $cat_term) { $compProduct[] = $cat_term->name; };
foreach ($familyterms as $family) { $compProduct[] = $family->name; };
$compProduct[] = $product_name;
$compProduct[] = $item2[‘quantity’];
$compProduct[] = $cart2[‘name’];

// Store the complete product info
$allTheCarts[] = $compProduct;
}

}

return $allTheCarts;

}

// Create csv
function create_csv_string($data) {

// Open temp file pointer
if (!$fp = fopen(‘php://temp’, ‘w+’)) return FALSE;

// Loop data and write to file pointer
foreach ($data as $line) fputcsv($fp, $line);

$cartsdata = cart_items_array();

foreach ($cartsdata as $cartrow) fputcsv($fp, $cartrow);

// Place stream pointer at beginning
rewind($fp);

// Return the data
return stream_get_contents($fp);

}

if ( isset( $_POST[‘submit’] ) ) {

function send_csv_mail ($csvData, $body, $to = ’email@email.co.uk’, $subject = ‘Test email with attachment’, $from = ‘webmaster@example.com’) {

global $post;
$user_id = get_current_user_id();

// This will provide plenty adequate entropy
$multipartSep = ‘—–‘.md5(time()).’—–‘;

// Arrays are much more readable
$headers = array(
“From: $from”,
“Reply-To: $from”,
“Content-Type: multipart/mixed; boundary=”$multipartSep””
);

// Make the attachment
$attachment = chunk_split(base64_encode(create_csv_string($csvData)));

// Make the body of the message
$body = “–$multipartSeprn”
. “Content-Type: text/plain; charset=ISO-8859-1; format=flowedrn”
. “Content-Transfer-Encoding: 7bitrn”
. “rn”
. “$bodyrn”
. “–$multipartSeprn”
. “Content-Type: text/csvrn”
. “Content-Transfer-Encoding: base64rn”
. “Content-Disposition: attachment; filename=””.date(‘Y-m-d’).”-“.str_replace(‘ ‘, ‘-‘, strtolower(get_user_meta($user_id, “wpcf-branch-active”, true))).”-file.csv”rn”
. “rn”
. “$attachmentrn”
. “–$multipartSep–“;

// Send the email, return the result
return @mail($to, $subject, $body, implode(“rn”, $headers));

}

$array = array(
array(“Code”, “Product Category”, “Product Family”, “Description”, “Quantity”, “Bay”),
);

send_csv_mail($array, “Hello World!!!rn This is simple text email message.”);
if(!send_csv_mail) {
echo “Error”;
}
else {
echo “”;
}

}

Read more here:: how to create plugin for upload csv file in wordpress ? please explain with full code

Leave a Reply

Your email address will not be published. Required fields are marked *