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.

  1. Get the name of the cart items of the non-logged in user.
  2. 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.

  1. Get the name of the cart items of the non-logged in user.
  2. 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
  • 1 Your $data seems overwrite itself while you foreach loop it. If you would like to push the $data to the email at once. You probably declare $data = ''; before foreach 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
  • Thanks @simongcc. Will you please also tell me how can I break each item to new line or separate item names with comma. Each time a item name will be added to $data it should separate that name with comma. Again Thanks. – Raashid Din Commented Apr 3, 2020 at 3:03
  • @simongcc apart from this question, will you please provide me some tutorials of php which give details of how php works like variable, loops etc – Raashid Din Commented Apr 3, 2020 at 3:18
  • I think you may refer to php or some unofficial tutorials by google around like this or grab some ebooks to have a more systematic method. Because online tutorials sometimes shows only quick and simple result but the concepts behind might not explain well. Books, in contrast, will have a more comprehensive explanations. When choose a book, you may try to choose who made PHP such as Rasmus Lerdorf. It is one of a good guideline. The creator usually gives a good insight of his own masterpiece. – 西門 正 Code Guy - JingCodeGuy Commented Apr 3, 2020 at 3:28
  • I asked your help for other problem. Will you please check 3 comment. I am stuck here. Once again, thanks. – Raashid Din Commented Apr 3, 2020 at 3:33
 |  Show 3 more comments

1 Answer 1

Reset to default 1

Here shows only the loop parts of the code

Since I don't know what is the data structure of $session_data.

  1. I suppose $session_data[$key] return a product name first in order to create the example.
  2. 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