wojodesign / simplecart-js

A simple javascript shopping cart that easily integrates with your current website.
simplecartjs.org
1.79k stars 490 forks source link

Help with sending Cart data with email.php #317

Closed coldes closed 11 years ago

coldes commented 11 years ago

Hello - I am after the ability to send cart data along with basic form details like customer name, etc.

I have looked at the comments in alexjong's post and at the source code in http://playground.simplecartjs.org.

All makes sense, however, what I need to fully test this is the actual source code of "email.php".

Can anyone help with this?

thanks in advance, Col

DahmaniAdame commented 11 years ago

Fellow send form straggler :) here's a basic code for the email.php that fits Simple Cart V3.

Struggled but got it work, at last :

<?php
$to = 'mail@someshop.com';
$subject = 'Simple Cart Order';
$content = $_POST;
$body = '';
for($i=1; $i < $content['itemCount'] + 1; $i++) {
  $name = 'item_name_'.$i;
  $quantity =  'item_quantity_'.$i;
  $price = 'item_price_'.$i;
  $total = $content[$quantity] * $content[$price];
  $body .= $name.' '.$quantity.' '.$price;
  $body .= <br>;
}
$headers = 'From: webmaster@example.com' . "\r\n" .
          'Reply-To: webmaster@example.com' . "\r\n" .
          'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $body, $headers);
Header('Location: thankyou.php');
?>

It's up to you now to add a custom name to it, and hopefully share what you end up with for the sake of people like us.

http://www.adamedoe.com/2012/11/simplecartjs-v3-php-code-for-sendform.html

coldes commented 11 years ago

Hello Adamedoe, just a quick response to say a HUGE thank you, I'll let you know how it all goes implementing this with simplecartjs and YES will share any information once I have this all working. Regards, Col

On Thu, Nov 15, 2012 at 11:34 PM, adamedoe notifications@github.com wrote:

Fellow send form straggler :) here's a basic code for the email.php that fits Simple Cart V3.

Struggled but got it work, at last :

<?php $to = 'mail@someshop.com'; $subject = 'Simple Cart Order'; $content = $_POST; $body = ''; for($i=1; $i < $content['itemCount'] + 1; $i++) { $name = 'itemname'.$i; $quantity = 'itemquantity'.$i; $price = 'itemprice'.$i; $total = $content[$quantity] * $content[$price]; $body .= $name.' '.$quantity.' '.$price; $body .=
; } $headers = 'From: webmaster@example.com' . "\r\n" . 'Reply-To: webmaster@example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $body, $headers); Header('Location: thankyou.php'); ?>

It's up to you now to add a custom name to it, and hopefully share what you end up with for the sake of people like us.

http://www.adamedoe.com/2012/11/simplecartjs-v3-php-code-for-sendform.html

— Reply to this email directly or view it on GitHubhttps://github.com/wojodesign/simplecart-js/issues/317#issuecomment-10407074.

coldes commented 11 years ago

Hello - OK - no immediate success. Breaking it down, I have success with a basic email send

<?php $to = 'mail@someshop.com'; $subject = 'Simple Cart Order'; $content = $_POST; //body section removed// $headers = 'From: '.$email_from."\r\n". 'Reply-To: webmaster@example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $body, $headers); Header('Location: success.html'); ?>

Including your body section:

$body = ''; for($i=1; $i < $content['itemCount'] + 1; $i++) { $name = 'itemname'.$i; $quantity = 'itemquantity'.$i; $price = 'itemprice'.$i; $total = $content[$quantity] * $content[$price]; $body .= $name.' '.$quantity.' '.$price; $body .=
; }

I receive a server error HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.

My simplecartjs script is:

simpleCart({ //Setting the Cart Columns for the sidebar cart display. cartColumns: [ //{ attr: "image", label: false, view: "image"}, //Name of the item { attr: "name" , label: "Item" }, //Quantity displayed as an input { attr: "quantity", label: "Qty", view: "input"},

//Built in view for a remove link
{ view:'remove', text: "X", label: "Del"},

//Price of item
{ attr: "price", label: "Price"},
//Subtotal of that row (quantity of that item * the price)
{ attr: "total" , label: "SubTot", view: "currency"  }

], cartStyle: "table",

checkout: { type: "SendForm" , url: "sendcart.php" , method: "POST" , success: "success.html" , cancel: "cancel.php" }

});

Any thoughts?

regards, Col

DahmaniAdame commented 11 years ago

First thought, I suspect the last part of what I gave you above

$body .= 
;

should be

$body .= '<br/>';
DahmaniAdame commented 11 years ago

I'm positive that it runs on Linux using LAMP server.

Is your server configured to send mails? This might be the cause!

For testing purposes, can you run this script instead of sending an email?

<?php
$content = $_POST;
$body = '';
for($i=1; $i < $content['itemCount'] + 1; $i++) {
  $name = 'item_name_'.$i;
  $quantity =  'item_quantity_'.$i;
  $price = 'item_price_'.$i;
  $total = $content[$quantity] * $content[$price];
  $body .= $name.' '.$quantity.' '.$price;
  $body .= <br>;
}

echo $body;
?>

That way, you'll know if Simple Cart's data is processed correctly. You should basically see lines with product name, quantity and price showing up, depending on how many you've got in your cart.

coldes commented 11 years ago

thanks for the super quick response - same server error message with trying the above, my server [mac snow leopard] is setup for emails, as I mentioned, removing the "body" section from sendcart.php produced a successful email send, however it could be a server setting problem?!?

I uploaded my test site to dropbox incase that might work, however all that does is display the php code.

Would be good to upload to a temporary server that handles php to try all of this.

Any thoughts... thanks

On Sat, Nov 17, 2012 at 10:18 AM, adamedoe notifications@github.com wrote:

I'm positive that it runs on Linux using LAMP server.

Is your server configured to send mails? This might be the cause!

For testing purposes, can you run this script instead of sending an email?

<?php $content = $_POST; $body = ''; for($i=1; $i < $content['itemCount'] + 1; $i++) { $name = 'itemname'.$i; $quantity = 'itemquantity'.$i; $price = 'itemprice'.$i; $total = $content[$quantity] * $content[$price]; $body .= $name.' '.$quantity.' '.$price; $body .=
; }

echo $body; ?>

That way, you'll know if Simple Cart's data is processed correctly. You should basically see lines with product name, quantity and price showing up, depending on how many you've got in your cart.

— Reply to this email directly or view it on GitHubhttps://github.com/wojodesign/simplecart-js/issues/317#issuecomment-10466180.

DahmaniAdame commented 11 years ago

My bad, had a couple of missed quotes.

I just tested this couplet :

      simpleCart({
        cartColumns: [
        //Name of the item
        { attr: "name" , label: "Name" },
        //Quantity displayed as an input
        { attr: "quantity", label: "Quantity", view: "input"},
        //Built in view for a remove link
        { view:'remove', text: "Remove", label: false},
        //Price of item
        { attr: "price", label: "Price"},
        //Subtotal of that row (quantity of that item * the price)
        { attr: "total" , label: "Subtotal", view: "currency"  },
        ],
        cartStyle: "table"
        });
        simpleCart({
        taxRate: 0.2
        });
        simpleCart({
        checkout: {
        type: "SendForm",
        url: "testdump.php",
        method: "POST"
        }
      });
<?php
$content = $_POST;
$body = '';
for($i=1; $i < $content['itemCount'] + 1; $i++) {
  $name = 'item_name_'.$i;
  $quantity =  'item_quantity_'.$i;
  $price = 'item_price_'.$i;
  $body .= 'item #'.$i.': ';
  $body .= $content[$name].' '.$content[$quantity].' '.$content[$price];
  $body .= '<br>';
}

echo $body;
?>

http://www.youtube.com/watch?v=V4agYhqHZx4

Should work fine with your server too.

coldes commented 11 years ago

OK - SUCCESS - thank you - now I will see about adding some form information, name, email, etc plus see how I can add some styling to the email

On Sat, Nov 17, 2012 at 11:32 AM, adamedoe notifications@github.com wrote:

My bad, had a couple of missed quotes.

I just tested this couplet :

  simpleCart({
    cartColumns: [
    //Name of the item
    { attr: "name" , label: "Name" },
    //Quantity displayed as an input
    { attr: "quantity", label: "Quantity", view: "input"},
    //Built in view for a remove link
    { view:'remove', text: "Remove", label: false},
    //Price of item
    { attr: "price", label: "Price"},
    //Subtotal of that row (quantity of that item * the price)
    { attr: "total" , label: "Subtotal", view: "currency"  },
    ],
    cartStyle: "table"
    });
    simpleCart({
    taxRate: 0.2
    });
    simpleCart({
    checkout: {
    type: "SendForm",
    url: "testdump.php",
    method: "POST"
    }
  });

<?php $content = $_POST; $body = ''; for($i=1; $i < $content['itemCount'] + 1; $i++) { $name = 'itemname'.$i; $quantity = 'itemquantity'.$i; $price = 'itemprice'.$i; $body .= 'item #'.$i.': '; $body .= $content[$name].' '.$content[$quantity].' '.$content[$price]; $body .= '
'; }

echo $body; ?>

http://www.youtube.com/watch?v=V4agYhqHZx4

Should work fine with your server too.

— Reply to this email directly or view it on GitHubhttps://github.com/wojodesign/simplecart-js/issues/317#issuecomment-10468128.

coldes commented 11 years ago

Quick question, the break rule
works with echo $body; ie onscreen view, but with an email send display as text with the cart data - eg Order #1: Menu Item 1 Qty 1 x $15
Order #2: Menu Item 2 Qty 2 x $11.5

Any thoughts? thanks, Col

coldes commented 11 years ago

OK - got it - remove $body .= '
';

add ."\n" to end of the below example $body .= 'Order #'.$i.': '.$content[$name].' --- Qty x '.$content[$quantity].' --- Unit Price $'.$content[$price]."\n";

Question: My cart shows a subtotal price rather than a unit price, so how would this be written in the php $body?

say: $subtotal = 'xxxxxx'.$i; [and to include 2 decimal places as the cart shows

plus how to email the Grand Total?

Any answers will be appreciated

Anyway I'll keep plugging away this end

coldes commented 11 years ago

My current tweak

<?php $to = 'mail@someshop.com'; $subject = 'Simple Cart Order'; $content = $_POST;

$body = ''; for($i=1; $i < $content['itemCount'] + 1; $i++) { $name = 'itemname'.$i; $quantity = 'itemquantity'.$i; $price = 'itemprice'.$i; $body .= 'Order #'.$i.': '.$content[$name].' --- Qty x '.$content[$quantity].' --- Unit Price $'.$content[$price]."\n";
}

$headers = 'From: webmaster@example.com'."\r\n". 'Reply-To: webmaster@example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $body, $headers);
Header('Location: success.html'); ?>

Email received looks like this

Reply-To: webmaster@example.com

X-Mailer: PHP/5.3.15 Message-Id: 20121118034855.C2E70E06F12@someshop.com Date: Sun, 18 Nov 2012 14:48:55 +1100 (EST)

Order #1: Menu Item 1 --- Qty x 1 --- Unit Price $15 Order #2: Menu Item 2 --- Qty x 2 --- Unit Price $11.5

NOW, how to add form data to simpla cart which github issue #305 was heading in the right direction but now stopped. http://playground.simplecartjs.org is no longer to at least look at the source code

My money is on "iamstevemckinney" - he is the demi-god with the answers

Steve, if you are able to help us mere mortals....

DahmaniAdame commented 11 years ago

Dude, I don't even need custom fields, but it pains me seeing unanswered questions... and I believe that a lot of people need to understand how to use customs fields.

Anyway, here's how I got work!

simpleCart({
  checkout: {
  type: "SendForm",
  url: "testdump.php",
  method: "POST",
  extra_data: {
    customercode: document.getElementById("customercode").value,
    cartid: "12321321"
    }
  }
});
simpleCart.bind( 'beforeCheckout' , function( data ){
  data.customercode = document.getElementById("customercode").value;
});

testdump.php code

<?php
$content = $_POST;
$body = '';
$customer = 'customercode';
$body = $content[$customer];
$body .= ' just ordered: <br>';

for($i=1; $i < $content['itemCount'] + 1; $i++) {
  $name = 'item_name_'.$i;
  $quantity =  'item_quantity_'.$i;
  $price = 'item_price_'.$i;
  $body .= 'item #'.$i.': ';
  $body .= $content[$name].' '.$content[$quantity].' '.$content[$price];
  $body .= '<br>';
}

echo $body;
?>

The trick is, you need to put the simpleCart declaration code AFTER your input, because the method getElementById won't return a result unless the input field is already declared and has content on it. You need to put the declaration on the page's footer, right before the closing tag.

PS.: don't ask me how each part works coz I simple gathered all the intel I could and somehow it ended up working :D

Non coders rule! :D Yeaaaaaaaaah!

coldes commented 11 years ago

Brilliant, brilliant, brillant... oh it makes so much sense when someone take the time to simply explain it - adamedoe you are now elevated to demi-god status

I now have an email send showing first name, last name, email address PLUS cart data

A few more tweaks I need to do and I will post what I have come up with for anybody else who is interested

Pushing the "friendship"... any thoughts on how to show the price with 2 decimal places, plus show the [Grand] Total?

Muchas gracias!!

DahmaniAdame commented 11 years ago

I'm only human mate :) I'm not even a coder :D it's just that I needed some answers for a project of mine using Simple Cart.

As they said ~ Necessity is the mother of invention. You get your hands dirty when you need something so bad. It happened that you needed the same thing :)

Anyways.

About decimals, Simple Cart is pretty intelligent. If you put prices with decimals, it shows them in its calculus. So, whatever price you put, just make sure it ends with a .00 and it should be included in all prices.

About the grand total stuff, couldn't find anything hinting about Simple Cart passing it into the array. So, I got this crazy idea of crating a variable to hold / accumulation totals.

Came up with the following code that I tested and it worked!

<?php
$content = $_POST;
$body = '';
$customer = 'customername';
$body = $content[$customer];
$body .= ' just ordered: <br>';
$grandTotal = '';
for($i=1; $i < $content['itemCount'] + 1; $i++) {
  $name = 'item_name_'.$i;
  $quantity =  'item_quantity_'.$i;
  $price = 'item_price_'.$i;
  $total = $content[$quantity]*$content[$price];
  $grandTotal += $total;
  $body .= 'item #'.$i.': ';
  $body .= $content[$name].' '.$content[$quantity].' '.$content[$price].' '.$total;
  $body .= '<br>';
}

echo $body;
echo '<br>';
echo 'this is the Grand Total: '.$grandTotal;
?>
coldes commented 11 years ago

Hello - just a touch base - all working pretty well, now have a "Total" amount with email send, thank you, had to tweak your code to work with the email side... php in echo mode works 100%, but not when $headers, etc are included. I am still tweaking some more until I am happy with posting here final code of a solid working scenario. A few php things I am hoping to work thru...

Not wanting to sound like a broken record, decimals for prices, simplecart table in browser displays prices with 2 decimal places [ ie $15.00] - GOOD

Email displays prices with none or 1 decimal place, depending on prices - NOT SO GOOD

Email received example

Name Name name@someone.com 12345678 Comments test

Has placed an order as per below: Order Total: $26.5

Order #1: Menu Item 1 --- Qty x 1 --- Unit Price $15 --- Subtotal $15

Order #2: Menu Item 2 --- Qty x 1 --- Unit Price $11.5 --- Subtotal $11.5

Any thoughts/suggestions?

+++++++++++++++++++++++++++++++++++++ SENDCART.PHP +++++++++++++++++++++++++++++++++++++ <?php $to = 'mail@someshop.com'; $subject = 'Simple Cart Order';

$content = $_POST; $email_message = '';

$firstname = 'first_name'; $lastname = 'last_name'; $email_from = 'email'; $phone = 'phone'; $comments = 'comments'; $email_message = $content[$firstname].' '.$content[$lastname]."\n".$content[$email_from]."\n".$content[$phone]."\n".$content[$comments]."\n"; $email_message .= "\n".'====================================================='."\n"; $email_message .= 'Has placed an order as per below:'."\n";

for($i=1; $i < $content['itemCount'] + 1; $i++) { $name = 'itemname'.$i; $quantity = 'itemquantity'.$i; $price = 'itemprice'.$i; $total = $content[$quantity]*$content[$price]; $grandTotal += $total; $body .= 'Order #'.$i.': '.$content[$name].' --- Qty x '.$content[$quantity].' --- Unit Price $'.$content[$price].' --- Subtotal $'.$total."\n";
$body .= '====================================================='."\n"; }

$email_message .= 'Order Total: $'.$grandTotal;

$headers = 'From: '.$email_from."\r\n". 'Reply-To: webmaster@example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $body, $email_message, $headers);
Header('Location: success.html'); ?>

SIMPLECARTJS SCRIPT ++++++++++++++++++++++++++++++++++++++++++++++++ simpleCart({ //Setting the Cart Columns for the sidebar cart display. cartColumns: [ //{ attr: "image", label: false, view: "image"}, //Name of the item { attr: "name" , label: "Item" }, //Quantity displayed as an input { attr: "quantity", label: "Qty", view: "input"},

        //Built in view for a remove link
        { view:'remove', text: "X", label: "Del"},

        //Price of item
        //{ attr: "price", label: "Price", view: "currency"},
        //Subtotal of that row (quantity of that item * the price)
        { attr: "total" , label: "SubTot", view: "currency"  }
    ],
    cartStyle: "table",

    checkout: {
    type: "SendForm" ,
    url: "sendcart.php" ,
    method: "POST" ,
    success: "success.html" ,
    cancel: "cancel.php" ,
    extra_data: {
        first_name: document.getElementById("first_name").value,
        last_name: document.getElementById("last_name").value,
        email: document.getElementById("email").value,
        email: document.getElementById("phone").value,
        email: document.getElementById("comments").value,
    cartid: "12321321"
}

}

});

simpleCart.currency( "AUD" );

simpleCart.bind( 'beforeCheckout' , function( data ){ data.first_name = document.getElementById("first_name").value; data.last_name = document.getElementById("last_name").value; data.email = document.getElementById("email").value; data.phone = document.getElementById("phone").value; data.comments = document.getElementById("comments").value; });

Anyway, onwards & upwards!!

DahmaniAdame commented 11 years ago

Good news. Found the answer for decimals! You need to use a PHP function to formal the price and total : number_format ---> http://php.net/manual/en/function.number-format.php

All you need to do is wrap your variable into the function and set it to 2 decimals. In your particular case :

$content[$price] becomes number_format($content[$price], 2, '.', '')

and

$total becomes number_format($total, 2, '.', '')

and

$grandTotal becomes number_format($grandTotal, 2, '.', '')

Peace!

coldes commented 11 years ago

Dude - just awesome - that is now the icing on the cake... and that pretty well wraps this up. I will create a new post with the final solution, If anybody can add to this, particularly in being able to stye the email php with basic html formatting then great, but for now I am happy with this current state.

Your help with this has been sublime

May all the Gods smile in your general direction

regards, Col