Hi there,
Just wondering if anyone would be kind enough to help out on something which has me literally tearing my hair out at the moment!
I am in the throws of making a plugin to help out on a site that I am creating. The basics is that I need very very simple shopping cart functionality which will basically remember what a person has added to the cart and then allow them to send these details via e-mail.
I am using session variables to simply store every time someone adds a product to the cart. Say they have nothing in their cart then the cart session variable will be empty (obviously 😉 ) let’s say they now add an item say entry_id 1. The session variable will now hold that number. Next up they add entry_id 3 the session variable will now have the data stored as 1|3.
All of this I have down to a tee now, that was the simple part, well after about 12 hours or so it was simple 😉
What I now want to be able to do is to show these items in the cart. I have all the entry_ids stored in the correct string format for what could be used in a weblog tag and so I thought it would be really easy to just add a function to my plugin which will spit out the contents of the session variable and use them from within a weblog tag.
The code that I have in my plugin to handle this part is this :
function ids() {
session_start();
echo $_SESSION['cart'];
}
$_SESSION[‘cart’] above has stored in it the 1|3 values
I then have this code for my weblog tag :
{exp:weblog:entries weblog="products" dynamic="off" entry_id="{exp:cart_sessions:ids}" parse="inward"}
<h2>{title}</h2>
{/exp:weblog:entries}
The problem is that this brings back all[ the entries in the weblog as opposed to just the ones that I want to show which in this case would be 1 & 3. If I place the plugin code for that function outside of the weblog tag then I can see that it is indeed spitting out the correctly formatted string of 1|3 but it just doesn’t seem to want to work within the weblog tag!
On the other hand if I make my plugin function that is spitting out the IDs look like this :
function ids() {
session_start();
echo "IDs - ".$_SESSION['cart'];
}
so that now it is giving out an incorrectly formatted string that the weblog tag won’t know what to do with it I can see from turning on the Display SQL Queries option that it gives a load of letters and numbers in the query that is trying to bring back the information. Obviously placing IDs - 1|3 into a weblog tag would come back with an incorrect result but why then when I place the correct string, namely 1|3 from my plugin into the entry_id parameter do I see in the query that it just brings back all entries instead of limiting it to those two?
Am I missing something really really obvious here or something?
Any help with this would be greatly appreciated. I still have a really really long way to go with all of this but this step has really made me bang my head up against the wall at the moment!!
Thanks.
Best wishes,
Mark
Okay never mind. I think I may have this one sorted possibly. Just needed to place in an if conditional into my code. Need to test loads but pretty sure that this should cover it for now.
I will be back though as I have another question based on this which I really don’t think I am going to be able to figure out so if anyone’s interested…
… watch this space!! 😉
Best wishes,
Mark
P.S. Moderators please don’t close this thread as it isn’t done with just yet 😉
Okay the pest is back again!! 😉
I have literally just spent the whole of the day on this one and I am nearing to admitting defeat unfortunately :down:
What I have working so far is a plugin that simply allows me to add entry_ids to a session variable and store them in the standard format required for using in a weblog tag ie 1|3|5|2|4.
This all works fine and I even have a function in the plugin to remove an item if I want to as well.
What I now need to do is this.
Say I add into the cart 4 of the entry_id 3 and 1 of entry_id 1. My session variable will look something like this (depending on what order the items are added) :
3|1|3|3|3
I can now use those within my weblog tag to return the information about the entries without a problem by doing this sort of thing :
{exp:weblog:entries weblog="products" dynamic="off" entry_id="{exp:cart_sessions:ids}" parse="inward"}
<h2>{title}</h2>
{/exp:weblog:entries}
This luckily works. I say luckily as the weblog tag is obviously receiving the entry_id of 3 four times but luckily knows to only allow it the once, phew!!
This then allows me to show the contents of the cart which is absolutely great! The only problem I have though is that I now obviously need to show how many of the item the person has added to their cart and also the price and sub-total of each item.
That is where I have come unstuck! :down: I really can’t think of an easy way of doing this. What I would like to be able to do is to count up how many of each entry_id there are from the session variable and then somehow when the weblog tag spits out that certain entry exchange a custom variable inside the weblog tag to show how many of this item were added to the cart.
I don’t know if this is possible but I was thinking somewhere along these lines perhaps?
{exp:cart_sessions:quantity}
{exp:weblog:entries weblog="products" dynamic="off" entry_id="{exp:cart_sessions:ids}" parse="inward"}
<h2>{title}</h2> - {quantity}
{/exp:weblog:entries}
{exp:cart_sessions:quantity}
So what would happen is the {exp:cart_sessions:quantity} tag pair function would find out how many of each item was added and then swap out the {quantity} variable for each corresponding weblog entry.
I then also need some way of taking the {quantity} variable and multiplying it by the {item_regular_price} that is held in the exp_simple_commerce_items section so that I can find out a sub-total for each item!!
If anyone has any ideas on how I could go about doing this or any ideas of how I could go about doing this perhaps easier than I am doing then I would be very very grateful to hear any suggestions at all on this one as I really have gone way too deep into code today and I think that I have perhaps swum just a little too deep!
Thanks for any help at all on this one.
Best wishes,
Mark
Sorry for the annoying bump but anyone? Please?
Just wondering if any of the developer team could lend me a hand on this one at all? I know in my mind how I want to do this but just can’t get that into code. I feel I am totally at a stumbling block on this as I really really want to create this myself but I have got to a place in the ExpressionEngine coding which is just over my head.
Unfortunately I need to get this done as soon as possible and I’m not being stingy in saying I would rather not pay for someone else to do this as I really do want to learn how to do this for myself (with help from someone obviously 😉 ) but if I do have to pay then I will have to I suppose. Would still love to do this myself though.
Thanks for any help on this one.
Best wishes,
Mark
Wow it sure is quiet in this thread. Starting to think I am the only one in here 😉
Just wondering if any of the Dev team or moderators wouldn’t possibly mind helping me out on this one?
I am now trying to figure something out which I think should get me most of what I need if I can figure it out.
I have a plugin that I am just messing about with at the moment which is this :
Plugin Name - weblog_ids
<?php
/*
=====================================================
Author: Mark Bowen
http://www.markbowendesign.com
=====================================================
File: pi.weblog_ids.php
-----------------------------------------------------
Purpose: Plugin provides weblog IDs
=====================================================
*/
$plugin_info = array(
'pi_name' => 'Weblog IDs',
'pi_version' => '1.0',
'pi_author' => 'Mark Bowen',
'pi_author_url' => 'http://www.markbowendesign.com',
'pi_description' => 'Weblog IDs provided by a plugin',
'pi_usage' => Weblog_ids::usage()
);
class Weblog_ids {
/** ----------------------------------------
/** Return IDs to weblog tag
/** ----------------------------------------*/
function weblog_ids() {
global $TMPL, $FNS;
$ids = '1|2|3|4|5|6|7';
$my_ids = 'my_ids';
$tagdata = $TMPL->tagdata;
$tagdata = $TMPL->swap_var_single($my_ids, $ids, $tagdata);
$tagdata = $TMPL->swap_var_single('my_test', $ids, $tagdata);
$this->return_data = $tagdata;
}
// ----------------------------------------
// Plugin Usage
// ----------------------------------------
// This function describes how the plugin is used.
// Make sure and use output buffering
function usage()
{
ob_start();
?>
{exp:weblog_ids parse="inward"}
{exp:weblog:entries weblog="products" entry_id="{my_ids}"}
<h2>{title}</h2>
ID = {my_test}
{/exp:weblog:entries}
{/exp:weblog_ids}
<?php
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
/* END */
}
// END CLASS
?>
Basically at the moment all this is doing is taking the $ids variable that I have hard-coded to 1|2|3|4|5|6|7 and using that within a weblog tag like below :
{exp:weblog_ids parse="inward"}
{exp:weblog:entries weblog="products" entry_id="{my_ids}"}
<h2>{title}</h2>
ID = {my_test}
{/exp:weblog:entries}
{/exp:weblog_ids}
This then spits out the entries as you would expect it to. What I would like to be able to do instead though would be to take the 1|2|3|4|5|6|7 string (which I will actually be providing from another source (session variable)) and split this up so that for every entry_id I can make the weblog tag spit out the details.
At the moment as I am just sending all the entry_ids to the weblog tag at once then I have only been able to find out how to output anything between the weblog tags the once.
What I want to be able to do is for every entry id that is given to the weblog tag spit out some variables between the weblog tags so that I can do something like this instead :
{exp:weblog_ids parse="inward"}
{exp:weblog:entries weblog="products" entry_id="{my_ids}"}
<h2>{title}</h2>
{qty} - {price} - {total}
{/exp:weblog:entries}
{/exp:weblog_ids}
The {qty}, {price} and {total} variables I would be figuring out with some SQL queries within the plugin but would like to be able to effectively loop the weblog tag the same amount of times that I have entry ids for it so in this case I would first run my SQL queries to provide myself with the variables and then spit out entry_id 1 with its associated variables and then entry_id 2 and its variables and so on and so forth for every entry_id.
I’m just not sure how you get the plugin to loop the weblog tag as at the moment I am only able to pass in all the entry_ids at once and I want to pass them in one at a time and then use the swap_var_single type functions to spit out the wanted variables.
Hopefully some of that made some sense somewhere to someone! 😉
Basically I would like to loop the weblog tag and swap out the variables with values instead of just passing in the entry_ids all at once.
Any help with this would be very much appreciated as I have tried many many things now and just can’t get this to work. I’m sure I am just missing something really really obvious here and just need a good shove in the right direction 😉
Thanks anyone who reads this.
Best wishes,
Mark
Hi Mark,
First of all, I’d recommend storing your entry IDs in an array. You should probably create an array like this:
<?php
// entry_id => quantity
$entry_ids = array(
1 => 3,
2 => 1
);
?>
Where you need to get the pipe delimited list as the value of the entry_id param on weblog:entries, do
join( '|', array_keys( $entry_ids ) )
Where you want to display the quantity within weblog:entries, create a function in your plugin that you pass the entry_id into, which you would use as a key on that array, and then you’d have something like this in your template:
{exp:weblog:entries}
{exp:marks_plugin:get_quantity entry_id="{entry_id}"}
{/exp:weblog:entries}
Hi Jesse,
Wow there is life in here after all 😉 I was starting to think I was all alone 😊
Actually I was going to post my findings but got too busy to do so. I do now have a lot of this working after just WAY too many hours of testing and pulling my hair out!
I hope to have this all up and running fully fairly soon and if I can get this really easy for people to use (pretty sure it will be just the one plugin) then I will release it so that everyone can use it as a sort of session based shopping cart. It would need me to give simple template code but generally everything I now have is based upon standard tags which is great as that is what I wanted to achieve from the start and not have to resort to any hacks so I am now very very happy.
I did actually in the end go with an array. Not quite as you have said there as the entry_ids are coming from a session variable but they are placed into an array never the less and then I loop over them and replace the variables as I go. I am very very happy with what I have done so far. Probably exceptionally easy for others to create but I’m pleased that I got it figured on my own. Had to really as no-one (apart from yourself that is) seemed to answer this post. Did get a LOT of views though 😉
Thanks again.
Best wishes,
Mark
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.