admin管理员组文章数量:1390495
I was creating a code which will help me to send the current items in cart to be sent to customers email address. While I got help from previous asked questions but I failed.
Here is what I want the code to do.
- Get the name of the cart items of the non-logged in user.
- send these names to the customers email address.
But when I send the item names, only one item name of cart is sent to users email address.
Here Is 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 );
}
}
}
Then 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 = '[email protected]';
$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);
}
I am confused why this code just sends one item name in email.
Might be I'm doing it wrong. Will you please help me in this regard.
Help will be most appreciated.
I was creating a code which will help me to send the current items in cart to be sent to customers email address. While I got help from previous asked questions but I failed.
Here is what I want the code to do.
- Get the name of the cart items of the non-logged in user.
- send these names to the customers email address.
But when I send the item names, only one item name of cart is sent to users email address.
Here Is 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 );
}
}
}
Then 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 = '[email protected]';
$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);
}
I am confused why this code just sends one item name in email.
Might be I'm doing it wrong. Will you please help me in this regard.
Help will be most appreciated.
Share Improve this question asked Apr 2, 2020 at 14:50 Raashid DinRaashid Din 2182 silver badges19 bronze badges 8 | Show 3 more comments1 Answer
Reset to default 1Here shows only the loop parts of the code
Since I don't know what is the data structure of $session_data
.
- I suppose
$session_data[$key]
return a product name first in order to create the example. - Because you are creating a custom email, I assume HTML email is supported.
// examples to show how to correctly loop the data with separation
$data = '';
foreach (WC()->session->get('cart') as $key => $value) {
$data .= $session_data[$key] . ', ';
}
echo $data; // output something or put $data to somewhere to use
本文标签: woocommerce offtopicGet the name of all the Items of cart in current session
版权声明:本文标题:woocommerce offtopic - Get the name of all the Items of cart in current session 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744604292a2615258.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$data = '';
beforeforeach
and then$data .= $session_data[$key];
. Then your $data will contain all keys instead of overwriting itself. Is it what you want to do? – 西門 正 Code Guy - JingCodeGuy Commented Apr 3, 2020 at 1:28