Search code examples
simplecart

How to pass SimpleCart JS v3 order content to PHP form?


I want to use the email checkout using the latest SimpleCart JS (v3). Unfortunatly, there's no source for the actual sendform php file.

I initiated the SimpleCart using :

simpleCart({
  checkout: { 
    type: "SendForm" , 
    url: "sendcart.php" ,
    method: "POST" , 
    success: "success.php" , 
    cancel: "cancel.php"
  }
});

And using this a sendcart.php

<?php
  $to      = 'mail@someshop.com';
  $subject = 'Simple Cart Order';
  $body = $_POST;
  $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');
?>

What I'm stuck with, is what should I call using $_POST to fill $body with the cart's content?

As a non coder, this is kinda hard for me to guess :) though, it might be obvious for some of you :)

Here's simplecart.js file

Thanks!

EDIT:

I finally understood that $_POST is an array. Now I need a loop to extract the array's values.


Solution

  • 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;
      $body .= 'item #'.$i.': ';
      $body .= $content[$name].' '.$content[$quantity].' '.$content[$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');
      ?>
    

    A bit sad coz no body helped this time, or even cared! :(