sending shopping cart contents to a outside php script

astralbaby

Joined: 2003-12-13
Posts: 35
Posted: Sat, 2005-04-30 17:30

Ok.. long story short...

I would like to send the contents of the shopping cart to another page where i can grab the info then display it so it may be mailed as a form to the owners email address..

what I am trying to accomplish is.. instead of using a payment gateway to finallize the transaction, the owner of the site I am working on just wants the items that a customer orders to be sent to his email address along with some other pieces of info...

so here we go..

right now I am working inside of ViewCart.tpl.

as a test... at the bottom of the file.. i did the following to attempt to send the loaded array $ViewCart[items] to the target page using a form action POST


<form action="http://www.carmelsilver.com/docs/orderform.php" method="post">


<input type="hidden" name="cart" value="{$ViewCart.items}">


<input type="submit" value="Order These Items"/>


</form>


then on the target orderform.php page.. I have this...


<?php 
  
  $contents = $_POST[cart];
  
  $many = count($contents);

  echo $many;

?>

what is displaying in the browser is 1. I am using the php count() function to see what I have in the array that I shipped from viewcart.tpl to orderform.php as a test

will this method even work? Can I pass variables from a template to an external php page using this method? It seems to me that the var $ViewCart[items] is loaded with all the items that the cart has.. and in the orderform.php file I was going to use that array to fill the orderform with the sent info from viewcart.tpl and then add some other stuff before finally sending it on to the owners email account using phpmail.

what am i doing wrong?

 
bharat
bharat's picture

Joined: 2002-05-21
Posts: 7994
Posted: Sat, 2005-04-30 23:45

{$ViewCart.items} is an array of items and you can't blindly put that into a form field. It'll probably just get sent over as the string "Array", which does you no good. Instead, you should iterate over the items and create an input for each, eg:

{if !empty($ViewCart.items)} 
<form action="http://www.carmelsilver.com/docs/orderform.php" method="post"> 
<form action="orderform.php" method="post"> 
{foreach from=$ViewCart.items item=item} 
{counter assign=index} 
<input type="hidden" name="cart[{$index}][count]" value="{$form.counts[$item.id]}"> 
<input type="hidden" name="cart[{$index}][id]" value="{$item.id}"> 
{/foreach} 
<input type="submit" value="Order These Items"/> 
</form>  
{/if} 

This will give you the following result:

Array
(
    [cart] => Array
        (
            [1] => Array
                (
                    [count] => 1
                    [id] => 238
                )

            [2] => Array
                (
                    [count] => 1
                    [id] => 7443
                )

            [3] => Array
                (
                    [count] => 1
                    [id] => 7456
                )

        )

)

I don't know exactly what information you really need. The id is good enough if you're writing code to use G2's api. Otherwise, if you need more information we can show you how to get at it.

This is an easy solution, but it's not durable. When you upgrade G2, it'll break. If you really want to make a good permanent solution, you should code a new module that uses the CartPlugin API so that it can interact with the cart module. Take a look at the ZipCart module to get an idea of how to do that.

 
astralbaby

Joined: 2003-12-13
Posts: 35
Posted: Sun, 2005-05-01 01:20

thanks very much bharat... that works perfectly.. I was having trouble to trying to understand how to do this.. and you were right.. it was just printing a string "Array" ...

now that I am having good luck doing this.. I am trying to construct a path to the image on the harddrive so i can get the thumbs to print in the browser along with the other info.... I can see in the array there is a item called "pathComponent" which holds the image name of the photo.. but how can i go about getting the rest of the path? by looking in the database.. in the G2_Item row.. I cannot really see where i may find anything to help me there.. I guess i could write a seperate query in my orderform.php file and use the id that was POSTED to get the other info.. would this be the way to go about it?, or could i send that info along with the form from ViewCart.tpl?

you guys are just plain awesome.... hands down....

 
astralbaby

Joined: 2003-12-13
Posts: 35
Posted: Sun, 2005-05-01 06:04

ok.. this works great..

{if !empty($ViewCart.items)} 
<form action="http://www.carmelsilver.com/docs/orderform.php" method="post"> 
{foreach from=$ViewCart.items item=item} 
{counter assign=index} 
<input type="hidden" name="cart[{$index}][title]" value="{$item.title}">

{if !empty($item.summary)}
<input type="hidden" name="cart[{$index}][summary]" value="{$item.summary}">
{else}
<input type="hidden" name="cart[{$index}][summary]" value="No Description Available">
{/if}

<input type="hidden" name="cart[{$index}][count]" value="{$form.counts[$item.id]}">
<input type="hidden" name="cart[{$index}][description]" value="{$item.description}">
<input type="hidden" name="cart[{$index}][href]" value="{g->url arg1="view=core:ShowItem" arg2="itemId=`$item.id`"}">
<input type="hidden" name="cart[{$index}][img]" value="{g->url arg1="view=core:DownloadItem" arg2="itemId=`$item.id`"}">
                                                                               

{/foreach} 
<br>
<input type="submit" value="Send these items to Order Form"/> 
<br>
</form>  
{/if}

all the info I need is passed along to the next page.. then I loop through the array and do this..

<?php 
  
$cart = $_POST[cart]; 

include ("header.php");
  
print "
<table width='100%' cellpadding='0' cellspacing='0' border='0' align='left' valign='top'>
<tr>

<td>Item</td>
<td>SN</td>
<td>Description</td>
<td>Quantity</td>
<td>Unit Price</td>
<td>Price</td>
</tr>
";


foreach ($cart as $sub_cart) {

#just alittle math here to make extended price look good
$extended_price = $sub_cart[description] * $sub_cart[count];

#lets strip the slashes out to make summary look good
$new_summary = stripslashes($sub_cart[summary]);

print "
<tr>
<td><a href='$sub_cart[href]' target='_new'><img src='$sub_cart[img]' border='0' width='90'></a></td>
<td>$sub_cart[title]</td>
<td>$new_summary</td>
<td>$sub_cart[count]</td>
<td>\$$sub_cart[description].00</td>
<td>\$$extended_price.00</td>
</tr>
<tr>
<td colspan='5'>&amp;</td>
</tr>
";

}


print "</table>";
  
  include ("footer.php");
  
?>

thanks so much bharat... you da man.... :)