i want write a function to sent out email. within the mail i wants to sent customers their ordered item with each of their product permalinks that link to wordpress product pages.
public function mailout($post_id = 106){
global $wpdb;
$mail = get_post_meta( $post_id, '_billing_email', true );
$product_name = $wpdb->get_results("SELECT order_item_name FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = '$post_id' AND order_item_type = 'line_item'");
for($i = 0; $i < count($product_name); $i++){
$post_data[$i] = $product_name[$i]->order_item_name;
$get_postID[$i] = $wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE post_title = '$post_data[$i]' ");
$prod_url[$i] = get_post_permalink($get_postID[$i]);
}
$t= get_post_permalink(106);
// $to = $mail;
// $subject = 'Test';
// $message = 'Thank you for purchasing! Please help us in reviewing our products.';
// $headers[] = 'From: <donotreply@test.com>';
// wp_mail( $to, $subject, $message, $headers );
}
i manage to get the post ids right and use it to get post permalink but it seems to return me all same "http://localhost/domainname" for the loop which all lead to homepage.
how do i change that and make it show on email some example format like the following format where permalinks are beside the product name? or if you have any other way to shorten this code
example result---(Product one : http://localhost/domainname)
Solved getting permalink for each product page
public function mailout($post_id = 106){
global $wpdb;
$mail = get_post_meta( $post_id, '_billing_email', true );
$product_name = $wpdb->get_results("SELECT order_item_name FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = '$post_id' AND order_item_type = 'line_item'");
for($i = 0; $i < count($product_name); $i++){
$post_data[$i] = $product_name[$i]->order_item_name;
foreach ($post_data as $pd){
$pd = $wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE post_title = '$post_data[$i]' ");
}
foreach($pd as $pdd){
$prod_url[] = get_post_permalink($pdd);
}
$data_array = array('Product name' => $post_data, 'permalink' => $prod_url);
}
return $data_array;
// $to = $mail;
// $subject = 'Product Review';
// $message = 'Thank you for purchasing! Please help us in reviewing our products.'. $data_array;
// $headers[] = 'From: <donotreply@leminiscate.com>';
// wp_mail( $to, $subject, $message, $headers );
}
for some problem i do not know why or how to put this $prod_url in to the $message to sent. still working on it..........