Jump to content

Need help wraping the body of my shopping cart in an email


Agreaves

Recommended Posts

My shopping cart works fine but I dont know how to put the contents of it in a variable to send via email. Below is the body of webpage. In the middle of the table I have included my shopping cart file that captures the contents of the cart. I just need to know how to format it to send it in an html email. I have tried putting columns in a variable for example $message =  '<tr><td></td></tr>'; etc etc, for each line but when I test my cart nothing is displayed in the cart.

<body>
<table width="1024" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td valign="baseline"></td>
    <td width="151" align="right" valign="bottom"><table width="150" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td height="35" align="center"><a href="../index.php" id="btn"><span>Continue Shopping</span></a></td>
        </tr>
    </table></td>
  </tr>
  <tr>
    <td height="40" colspan="2" valign="middle" class="bdr" id="heading"><?php echo 'Shopping Cart'; ?></td>
  </tr>
</table>
<table width="1024" border="0" align="center" cellpadding="15" cellspacing="" class="main_bdr">
<tr>
<td align="center">
<?php include'cart.inc';?>
</td>
</tr>
</table>

<table width="1024" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="40" align="center" class="bdr">© <?php echo date("Y");?> MizBones</td>
  </tr>
</table>
</body>

Link to comment
Share on other sites

you don't need to change your cart into a smarty template just create an email template to put the cart around so when it is emailed it shows the cart.

 

look at the first link i posted at the answer using the fetch smarty method. it has examples on how to setup an example email template.

Link to comment
Share on other sites

Here is the code for the body of my shopping cart. My database connection is in another file

 

<body>
<table width="1024" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td valign="baseline"></td>
    <td width="151" align="right" valign="bottom"><table width="150" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td height="35" align="center"><a href="../index.php" id="btn"><span>Continue Shopping</span></a></td>
        </tr>
    </table></td>
  </tr>
  <tr>
    <td height="40" colspan="2" valign="middle" class="bdr" id="heading"><?php echo 'Shopping Cart'; ?></td>
  </tr>
</table>
<table width="1024" border="0" align="center" cellpadding="15" cellspacing="" class="main_bdr">
<tr>
<td align="center">
<?php
// Product Id from URL
$product_id = $_GET['id'];

// Action from URL 
$action = $_GET['action'];
//if there is an product_id and that product_id doesn't exist display an error message
if($product_id && !productExists($product_id)) {
    die("Product Doesn't Exist");
}

switch($action) { //decide what to do 

    case "add":
         $_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id 
    break;

    case "remove":
         $_SESSION['cart'][$product_id]--; //remove one from the quantity of the product with id $product_id 
        if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise it will show zero, then -1, -2 etc when the user keeps removing items. 
    break;

    case "empty":
        unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. 
    break; 
} 
?>
<?php			 			 			
if($_SESSION['cart']) { //if the cart isn't empty
//show the cart
	echo "<form method=\"post\" name=\"body\" action=\"$_SERVER[php_SELF]\">";
	echo "<table align=\"center\" id=\"tbl_bdr\" width=\"500\" border=\"0\" cellspacing=\"0\" cellpadding=\"10\">
<tr align=\"left\">
<td align=\"left\"><b>Name:</b></td>
<td><input id=\"txt_box\" type=\"text\" name=\"name\"/></td>
<td align=\"left\"><b>Email:</b></td>
<td><input id=\"txt_box\" type=\"text\" name=\"email\"/></td>
  	</tr>
<tr align=\"left\">
<td align=\"left\"><b>Address:</b></td>
<td><input id=\"txt_box\" type=\"text\" name=\"address1\"/></td>
<td align=\"left\"><b>Address(ext):</b></td>
<td><input id=\"txt_box\" type=\"text\" name=\"address2\"/></td>
  	</tr>
<tr align=\"left\">
<td align=\"left\"><b>Telephone:</b></td>
<td><input id=\"txt_box\" type=\"text\" name=\"telephone\"/></td>
  	</tr>
<tr><td height=\"10\"></td></tr>
</table>";
    echo "<table id =\"cart_bdr\" align =\"center\" border=\"0\" padding=\"15\" width=\"950\">"; //format the cart using a HTML table
echo "<tr id=\"hdg_bkgrnd\" height=\"40\"><td width=\"237\" align=\"center\"><b>Thumbnail</b></td><td width=\"237\" align=\"center\"><b>Id</b></td><td width=\"237\" align=\"center\"><b>Name</b></td><td width=\"237\" align=\"center\"><b>Quantity</b></td><td width=\"237\" align=\"center\"><b>Amount</b></td></tr>";

    //iterate through the cart, the $product_id is the key and $quantity is the value
foreach ($_SESSION['cart'] as $product_id => $quantity) {  

//get the name, price and pix from the database - this will depend on your database implementation.
    //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
    $sql = sprintf("SELECT Id, Name, Price, Pix FROM products WHERE Id = %d;", $product_id);

    $result = mysqli_query($con,$sql) or die("Couldnt run query");

$row = mysqli_num_rows($result);

//Only display the row if there is a product (though there should always be as we have already checked)
if($row > 0) {

list($Id, $Name, $Price, $Pix) = mysqli_fetch_row($result);

    $line_cost = $Price * $quantity; //work out the line cost
    $total = $total + $line_cost; //add to the total cost

    echo "<tr height=\"55\">";
echo "<td align=\"center\"><img id=\"thumb_bdr\" src=\"$Pix\" width=\"50\" height=\"50\"/></td>";
echo "<td align=\"center\" id=\"cols_bkgrnd\"><div name=\"id\">$Id</div></td>";
    //show this information in table cells
    echo "<td align=\"center\" id=\"cols_bkgrnd\">$Name</td>";
    //along with a 'remove' link next to the quantity - which links to this page, but with an action of remove, and the id of the current 			product
    echo "<td align=\"center\" id=\"cols_bkgrnd\">$quantity <a href=\"$_SERVER[php_SELF]?action=remove&id=$product_id\">X</a></td>";
    echo "<td align=\"right\" id=\"cols_bkgrnd\">$$line_cost</td>";
    echo "</tr>";
}
}
//show the total
    echo "<tr id=\"hdg_bkgrnd\" height=\"40\">";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
    echo "<td align='right'><div id=\"total\"><b>Total</b></div></td>";
    echo "<td align='right'>$$total</td>";
    echo "</tr>";
echo "</table>";

    //show the empty cart link - which links to this page, but with an action of empty. A simple bit of javascript in the onlick event of the link asks the user for confirmation
echo "<table width=\"950\" align=\"center\">";
    echo "<tr>"; 
   	echo "<td width=\"237\"></td>";
echo "<td width=\"237\"></td>";
echo "<td width=\"237\"></td>";
echo "<td width=\"237\" align=\"right\"><input id=\"submit_btn\" value=\"Send\" type=\"submit\"/></td>"; 
    echo "<td width=\"237\"><input id=\"cart_btn\" type=\"button\" value=\"Empty\" onclick=\"window.location.href='$_SERVER[php_SELF]?action=empty'\" /></td>";
    echo "</tr>";
    echo "</table>";
echo "</form>";

} else 
{
//otherwise tell the user they have no items in their cart 
    echo "You have no items in your shopping cart.";

}
//function to check if a product exists
function productExists($product_id) {
	global $product_id;
	global $con;
	global $host;
	global $user;
	global $pass;
	global $dbase;
		//use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
		$sql = sprintf("SELECT * FROM products WHERE id = %d;", $product_id);

		$result = mysqli_query($con,$sql) or die ('Could not run query');

		$row = mysqli_num_rows($result);

		return $row > 0;
}
?>
</td>
</tr>
</table>

<table width="1024" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="40" align="center" class="bdr">© <?php echo date("Y");?> MizBones</td>
  </tr>
</table>
</body>

Link to comment
Share on other sites

did you look at the example i gave you :confused:

 

 

smarty email template:

 

Dear {$contact_info.name},

Welcome and thank you for signing up as a member of our user group.

Click on the link below to login with your user name
of '{$contact_info.username}' so you can post in our forums.

{$login_url}

List master

{textformat wrap=40}
This is some long-winded disclaimer text that would automatically get wrapped
at 40 characters. This helps make the text easier to read in mail programs that
do not wrap sentences for you.
{/textformat}

    

 

<?php

// get $contact_info from db or other resource here

$smarty->assign('contact_info',$contact_info);
$smarty->assign('login_url',"http://{$_SERVER['SERVER_NAME']}/login");

mail($contact_info['email'], 'Thank You', $smarty->fetch('email_body.tpl'));

?>

 

this shows you how to send an email with variables in it.

Link to comment
Share on other sites

Im creating the smarty template but im stuck at how to assign the variables, all the variables are assigned through php and I cant find any documentation on how to pass php assigned variables in smarty. All the examples are assigned through smarty. So how do I pass the php variables inside smarty to use on my template?

Link to comment
Share on other sites

In my code below im getting a notice undefined index and notice undefined offset message from php. I checked the line I cant figure out whats wrong with it, I have never gotten this notice before. I just updated my testing server to the latest version and all the error options are turned on. What is it im doing wrong here?

<?php
// Product Id from URL
$product_id = array_key_exists('id',$_GET) ? $_GET['id']: null;

// Action from URL 
$action =  array_key_exists('action',$_GET) ? $_GET['action']: null;

//if there is an product_id and that product_id doesn't exist display an error message
if($product_id && !productExists($product_id)) {
    die("Product Doesn't Exist");
}

switch($action) { //decide what to do 

    case "add":
         $_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id 
    break;

    case "remove":
         $_SESSION['cart'][$product_id]--; //remove one from the quantity of the product with id $product_id 
        if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise it will show zero, then -1, -2 etc when the user keeps removing items. 
    break;

    case "empty":
        unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. 
    break; 
} 
?>

Link to comment
Share on other sites

In my code above I tried to define the $_GET variables as NULL to get rid of notices of undefined variables but it messes with the action of my switch for the cart, now the empty button doesnt clear the cart. This is what I have right now for both $_GET['id'] and $_GET['action']. What else could I do to define the variables and still have the switch function as needed?

if(isset($_GET['id']))
{
$product_id = $_GET['id'];
} else
{
$product_id = NULL;
}

Link to comment
Share on other sites

Try changing $action =  array_key_exists('action',$_GET) ? $_GET['action']: null; to $action =  array_key_exists('action',$_GET) ? $_GET['action']: "empty";

And your switch

    case "empty":
        unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. 
    break; 

 

to

 

    case "empty":
default:
        unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. 
    break; 

 

Edit: you can also just use isset() instead of array_key_exists.

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.