I have a page where i'm using a button to generate a csv file from data being looped from an array, which when clicked is then automatically emailed to a user.
I also want the data to create a new wordpress post by using the wp_insert_post()
function.
So far i've added this but i'm not sure where it should go in the code:
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
));
?>
Can anyone help me please?
function cart_items_array() {
$carts2 = MultiCart\get_carts();
// All the products
$allTheCarts = array();
foreach ( $carts2 as $cart_id => $cart2 ) {
// get array of items contained in a cart ...
$items2 = MultiCart\get_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 protected]', $subject = 'Test email with attachment', $from = '[email protected]') {
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 = "--$multipartSep\r\n"
. "Content-Type: text/plain; charset=ISO-8859-1; format=flowed\r\n"
. "Content-Transfer-Encoding: 7bit\r\n"
. "\r\n"
. "$body\r\n"
. "--$multipartSep\r\n"
. "Content-Type: text/csv\r\n"
. "Content-Transfer-Encoding: base64\r\n"
. "Content-Disposition: attachment; filename=\"".date('Y-m-d')."-".str_replace(' ', '-', strtolower(get_user_meta($user_id, "wpcf-branch-active", true)))."-file.csv\"\r\n"
. "\r\n"
. "$attachment\r\n"
. "--$multipartSep--";
// Send the email, return the result
return @mail($to, $subject, $body, implode("\r\n", $headers));
}
$array = array(
array("Code", "Product Category", "Product Family", "Description", "Quantity", "Bay"),
);
send_csv_mail($array, "Hello World!!!\r\n This is simple text email message.");
if(!send_csv_mail) {
echo "Error";
}
else {
echo "<meta http-equiv=\"refresh\" content=\"0; url=./thank-you\" />";
}
}
?>