Hi there,
Just wondering if anyone could help me with a little something here. I suppose I had best explain what I am trying to do first and then I will show what I have so far.
I have a need for an exceptionally (and I do mean exceptionally) simple shopping cart. The way it will work is that entry_ids of items will get added to a session variable with pipe (|) characters between them. I am pretty sure I have this bit covered.
I then have this in a template :
{exp:shopping_cart parse="inward"}
{exp:weblog:entries weblog="default_site" entry_id="{entry_ids}"}
<h3>{title}</h3>
{embed="tests/shopping-cart-prices" entry_id="{entry_id}"}
<p>{qty}</p>
{/exp:weblog:entries}
{/exp:shopping_cart}
This is then the code for the shopping_cart plugin :
pi.shopping_cart.php
<?php
$plugin_info = array(
'pi_name' => 'Shopping Cart',
'pi_version' => '1.0.0',
'pi_author' => 'Mark Bowen',
'pi_author_url' => 'http://www.markbowendesign.com/',
'pi_description' => 'Simple shopping cart',
'pi_usage' => Shopping_cart::usage()
);
class Shopping_cart {
var $return_data = '';
function Shopping_cart()
{
global $TMPL, $DB, $SESS, $FNS;
$tagdata = $TMPL->tagdata;
$ids = "11|12|13|11";
$entry_ids = explode("|", $ids);
$unique_ids = array_unique($entry_ids);
foreach ($unique_ids as $entry_id)
{
$output .= $entry_id.'|';
}
$tagdata = $TMPL->swap_var_single('entry_ids', $output, $tagdata);
$this->return_data = $tagdata;
}
// ----------------------------------------
// Plugin Usage
// ----------------------------------------
function usage()
{
ob_start();
?>
<?php
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
// END
}
?>
At the moment the $ids=“11|12|13|11” variable is acting as though it is my session variable just for testing purposes. What I want to do is to take each unique entry_id from that variable and :
1 - Use it to power the weblog tag to spit out the entries title and any other information and,
2 - Spit out the cost of the item, quantity added and total cost
Part 1 of the above is working as I am using the $unique_ids = array_unique($entry_ids) part to get rid of any duplicate values and these then get passed inward to the weblog tag enabling me to get at any information stored in the particular weblog entry. I then have the {embed=“tests/shopping-cart-prices” entry_id=”{entry_id}”} and the code for that template which has this :
{exp:simple_commerce:purchase entry_id="{embed:entry_id}" success="site/success" cancel="site/index"}
<p><strong>{item_sale_price}</strong></p>
{/exp:simple_commerce:purchase}
so that I can retrieve the cost of the item and show that on the screen. This is all working fine but I was hoping that instead of embedding a template I could do all of this within my plugin using DB queries.
I tried just getting the cost back by placing a database query to find out the item_regular_price from the exp_simple_commerce_items table by placing the query inside the
foreach ($unique_ids as $entry_id)
part of my code above but I could never get it to swap the variable in my template - {cost} with each different cost. It would just use the last (or was it first) cost and place it in for all items.
I then have the problem of quantity, sub-total and total.
Clearer question ![]()
To re-iterate and to probably put things a lot easier as I probably haven’t really explained myself all that well in simplest terms what I have is this :
1 - A session variable which holds the entry_ids of items that have been added to the cart. $ids=“11|12|13|11”. This would mean two of item 11, one of item 12 and one of item 13.
I do already have this session variable and I can get items added to it so that bit is more or less covered - haven’t figured how to minus an item but I will figure that out for myself.
I then want to take each of these entry_ids and first of all use them within the enclosed weblog tag (this is working at the moment - can’t believe I even got that far
) to spit out the item information.
Then I want to have {cost}, {qty} and {total} variables within the weblog tag that I can exchange using the swap_var_single. The {cost} variable will simply be the item_regular_price. The {total} will only show the once (just can’t get my head around how to do that one but I suppose it would be great if I could use the {if count == total_results} conditional in the weblog to get that one, I just haven’t been able to figure that out at all yet!) and then the {qty} variable would just be however many times the entry_id appeared in the $ids=“11|12|13|11” session variable.
Putting all of this together though is proving a lot more harder than I thought. I can see in my mind what I want to do but getting it into code is proving quite difficult. I am quite proud of how far I have got so far but really could do with a helping hand to get the rest of the distance in my journey.
Thanks for any help on this.
Best wishes,
Mark
