I want to get the name of the cart items of the non-logged in user and send these names to the customers email address.
However, after trying to send the item names, only one item name of cart is sent to users email address.
My code
// Change cart item name and price
add_action( 'woocommerce_before_calculate_totals', 'change_cart_item_name_and_price', 10, 1 );
function change_cart_item_name_and_price( $cart ) {
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Get new items names from WC_Session
$session_data = (array) WC()->session->get( 'new_item_names' );
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
$item_names = $cart_item['data']->get_name();
// If item name doesn't exist in WC_Session for this cart item, we do it
if( ! isset($session_data[$cart_item_key]) ) {
$session_data[$cart_item_key] = $item_names;
WC()->session->set( 'new_item_names', $session_data );
}
}
}
Here is handling of my form
if (isset($_POST['enquiry-cart']) && wp_verify_nonce( $_POST['enquiry_cart'], 'enquiry-cart' )) {
$session_data = (array) WC()->session->get( 'new_item_names' );
foreach (WC()->session->get('cart') as $key => $value) {
$data = $session_data[$key];
}
$contactName = htmlentities(stripslashes(trim($_POST['contactName'])));
$contactEmail = htmlentities(stripslashes(trim($_POST['contactEmail'])));
$contactMessage = htmlentities(stripslashes(trim($_POST['contactMessage'])));
$emailTo = 'dindarraashid@gmail.com';
$subject = 'Enquiry From: '.$contactName;
$body = "Name: $contactName \n\n Email: $contactEmail \n\n \n\n Message: $contactMessage $data";
$headers = 'From: '.$contactName.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $emailTo;
wp_mail([$emailTo, $contactEmail], $subject, $body, $headers);
}
Please point out where I am missing