Jump to content

echo an array inside form confirmation email


JordanC
Go to solution Solved by kicken,

Recommended Posts

Hi! My question is probably simple but I've been scratching my head for hours now... I'm pretty new to php. I have an online form for orders; when submitted, an email is sent to the shop manager containing the info the client has filled in. So I pass all the info in the code below, but I can't manage to echo the array containing the order's items/qty/item_code. (I'm using Wordpress, Oxygen Builder, Metabox and Code Snippets, if that helps). Here's the code (used as a Code Snippet):

add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) {
    if ( 'command-form' !== $config['id'] ) {
        return;
    }

    $name = rwmb_meta( 'name', '', $post_id );
    $email = rwmb_meta( 'email', '', $post_id );
    $phone = rwmb_meta( 'phone', '', $post_id );
    $orders = rwmb_meta( 'order', '', $post_id ); */ ---> this is the array I wish to show the elements from (array --> [0]:{item:"item", qty:"qty", code:"code"]}, [1]:{}, etc...} */
    $comments = rwmb_meta( 'comments', '', $post_id );
    $reception = rwmb_meta( 'reception', '', $post_id );
    $destination = rwmb_meta( 'destination', '', $post_id );
    $payment = rwmb_meta( 'payment', '', $post_id );

    $body = "Vous avez reçu une nouvelle commande en ligne!

Nom: $name
Courriel: $email
Téléphone: $phone
Commande: */ ---> this is where I want it displayed, but since it is within $body = " ", I can't seem to be able to use foreach( ) or even print_r( )... */
Commentaires: $comments
Réception: $reception
Paiement: $payment";
     

    $headers = ['Content-type: text/html', "Reply-To: $email"];
    wp_mail( 'address@domain.com', 'Nouvelle commande en ligne', $body );
}, 10, 2 );

 

Help with this would be greatly appreciated! Please let me know if any details are missing. Thanks in advance!

Jordan

Edited by JordanC
Link to comment
Share on other sites

  • Solution

Use a foreach loop above the $body and create a new variable containing the order details as a string, then use that variable in $body.

$orderDetails = '';
foreach ($orders as $order){
    $orderDetails .= $order . '\n';
}

It seems your orders are JSON encoded, so you'll probably want to json_decode them and format them nicely within that foreach loop.

Edited by kicken
Link to comment
Share on other sites

Thanks a lot! With some tinkering it worked, except for one small detail:

I doubled the 'foreach' loop since it is an array in an array, like this:

$orderDetails = '';
	foreach ($orders as $order){	
		$order_d = '';
		foreach ($order as $order_element){
			$order_d .= $order_element . ' / ';
		}
		$orderDetails .= '- ' . $order_d . '<br>';
	}

and the outcome is:

Nom: Jordan
Courriel: ---
Téléphone: 555-5555
Commande: - feux d'artifice / 1 / 3546 / <br>- ampoule 100w / 1 / 3456 / <br>
Commentaires:
Réception: cueillette
Paiement: comptant

Small problem is, whichever I use, '/n' or '<br>', it just shows up as plain text...

I've tried using 'var_dump(json_decode($variable));' on my variables, but then nothing shows at all in the output message. 

I've read the 'json_decode' link twice and tried a bunch of stuff within the loop, but must be missing something.

Your reply was right on, though, I'll mark it as the solution! 

Link to comment
Share on other sites

Sure! Here's 3 tests I added to the code (the submitted form contained 2 arrays/order-elements):

	$test_raw = '';
	foreach ($orders as $order){	
		$test_raw .= $order . '/n';
	}
	
	$test_json = '';
	foreach ($orders as $order){
		$json_dec = var_dump(json_decode($order));
		$test_json .= $json_dec . '/n';
	}
	
	$test_json_2 = '';
	$dec_orders = var_dump(json_decode($orders));
	foreach ($dec_orders as $dec_order){
		$test_json_2 .= $dec_order . '/n';
	}

And here's the output:

Test_raw: Array/nArray/n
Test_json: /n/n
Test_json_2:  

 

Link to comment
Share on other sites

That was from memory of how the array is structured, not an output. Just tried a few more things trying to access the raw data, with poor results:

$test_orderDetails = '';
	foreach ($orders as $order){	
		$dec_order = var_dump(json_decode($order));
		$order_d = '';
		foreach ($dec_order as $order_element){
			$order_d .= $order_element . ' - ';
		}
		$test_orderDetails .= '- ' . $order_d . '|||';
	}
	
	$test_printr = '';
	foreach ($orders as $order){
		$p_order = print_r($order);
		$test_printr .= $p_order . '/n';
	}
	
	$test_ordersPr = print_r($orders);

To get this:

Test_orderDetails: - |||- |||
Test_printr: 1/n1/n
Test_ordersPr: 1

When this code:

	$orderDetails = '';
	foreach ($orders as $order){	
		$order_d = '';
		foreach ($order as $order_element){
			$order_d .= $order_element . ' - ';
		}
		$orderDetails .= '- ' . $order_d . '|||';
	}

Outputs this:

Commande: - feux d'artifice - 1 - |||- vis 3/4 - 1 - |||

 

Edited by JordanC
Link to comment
Share on other sites

If you simply var_dump($orders); then it will show you what exactly is contained in that variable.

I'm guessing is just a multi-dimensional array and not JSON like your original post implied.  If so, you'd just access whatever keys you need or foreach over them all.

foreach ($orders as $order){
    foreach ($order as $key => $value){
    	$orderDetails .= $key.': '.$value.'\n';
	}
	//...
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.