Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2012-05-10 15:16:04

wornout
Member
From: Italy
Registered: 2009-01-20
Posts: 256
Website

How create a wish-list widget with Textpattern

A client wants to put on his site a group of item to sell.
Visitors can put this item in a wish-list with the ability to print it.
A sort of shopping cart without transaction.
Anyone can suggest me a way to do that?

Thanks

Offline

#2 2012-05-10 15:52:53

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,599
Website

Re: How create a wish-list widget with Textpattern

I guess this is similar to a “my shortlist” function. I’d suggest saving the user’s choices in a cookie as a comma-separated list. You can retrieve this via php and use it as the id attribute for an article_custom list. Note, you’ll need to check the cookie data is clean before plugging it into your article_custom.

There’s an ancient and frustrating plugin named chs_cookie for setting and retrieving cookie data server-side. I have a rewritten version that functions much like txp:variable – send me a mail if you’d like a copy.


TXP Builders – finely-crafted code, design and txp

Offline

#3 2012-05-14 13:47:14

Kjeld
Member
From: Tokyo, Japan
Registered: 2005-02-05
Posts: 446
Website

Re: How create a wish-list widget with Textpattern

jakob wrote:

I have a rewritten version that functions much like txp:variable – send me a mail if you’d like a copy.

Hi Jacob, I am interested in this as well. Just happen to be working on exactly the same thing. Would it be possible to get a copy?


Old Photos of Japan – Japan in the 1850s~1960s (100% txp)
MeijiShowa – Stock photos of Japan in the 1850s~1960s (100% txp)
JapaneseStreets.com – Japanese street fashion (mostly txp)

Offline

#4 2012-05-15 17:20:55

Kjeld
Member
From: Tokyo, Japan
Registered: 2005-02-05
Posts: 446
Website

Re: How create a wish-list widget with Textpattern

Thanks again, Jacob, for the plugin.

I have figured out how to write and read a cookie with the plugin, but I am unable to adjust the expiration date… How do you set “expire”?

None of these work for me:

<txp:chs_cookie name="item" expire="Thu, 15 May 2014 17:10:07 GMT">
<txp:chs_cookie name="item" expire="24">
<txp:chs_cookie name="item" expire="time()+3600">


Old Photos of Japan – Japan in the 1850s~1960s (100% txp)
MeijiShowa – Stock photos of Japan in the 1850s~1960s (100% txp)
JapaneseStreets.com – Japanese street fashion (mostly txp)

Offline

#5 2012-05-15 17:55:51

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,599
Website

Re: How create a wish-list widget with Textpattern

It uses php’s setcookie function unfiltered so you’ll need to use that syntax. Try adding a value="cookie-value" attribute and closing the tag with a />.


TXP Builders – finely-crafted code, design and txp

Offline

#6 2012-05-15 18:31:39

Kjeld
Member
From: Tokyo, Japan
Registered: 2005-02-05
Posts: 446
Website

Re: How create a wish-list widget with Textpattern

jakob wrote:

It uses php’s setcookie function unfiltered so you’ll need to use that syntax. Try adding a value="cookie-value" attribute and closing the tag with a />.

Thanks, Jacob.

LOL, I actually got time()+3600 in my previous message from the page you linked to.
Went back to that page and I now got it to work with a straight unix time stamp (1893477600).

But, shouldn’t it work with the time function as well? Like this:

<txp:chs_cookie name="testkd2" value="hello" expire="time()+60*60*24*30" />

This gives an error message:

Warning: setcookie() expects parameter 3 to be long, string given on line 53

Additionally, no cookie is created at all.


Old Photos of Japan – Japan in the 1850s~1960s (100% txp)
MeijiShowa – Stock photos of Japan in the 1850s~1960s (100% txp)
JapaneseStreets.com – Japanese street fashion (mostly txp)

Offline

#7 2012-05-15 18:55:36

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,599
Website

Re: How create a wish-list widget with Textpattern

Does it work with:

<txp:chs_cookie name="testkd2" value="hello" expire='<txp:php>echo time()+(60*60*24*30);</txp:php>' />

Last edited by jakob (2012-05-15 18:56:45)


TXP Builders – finely-crafted code, design and txp

Offline

#8 2012-05-15 19:43:22

Kjeld
Member
From: Tokyo, Japan
Registered: 2005-02-05
Posts: 446
Website

Re: How create a wish-list widget with Textpattern

jakob wrote:

Does it work with:

<txp:chs_cookie name="testkd2" value="hello" expire='<txp:php>echo time()+(60*60*24*30);</txp:php>' />

Wow. That never entered my imagination… And it works. Thank you very much. Also for sending me back to php.net. That made me try the straight unix date, which works fine, too.

I have now figured out (a rather inelegant) way to add individual articles to the cookie to create a list that can be viewed in a special section. I haven’t figured out yet how to remove an article. I think that may require reading the array of id’s to remove a single article id. I am terrible at that…


Old Photos of Japan – Japan in the 1850s~1960s (100% txp)
MeijiShowa – Stock photos of Japan in the 1850s~1960s (100% txp)
JapaneseStreets.com – Japanese street fashion (mostly txp)

Offline

#9 2012-05-15 20:58:32

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,599
Website

Re: How create a wish-list widget with Textpattern

I have now figured out (a rather inelegant) way to add individual articles to the cookie to create a list that can be viewed in a special section. I haven’t figured out yet how to remove an article. I think that may require reading the array of id’s to remove a single article id. I am terrible at that…

I guess you may also need to scrub out duplicates too. Maybe there are some idea to be garnered from this thread and from here for removing items from the array. While you’re massaging your comma-separated list into an array and back, you might also want to make sure your array items are integers before plugging the result into txp:article_custom.


TXP Builders – finely-crafted code, design and txp

Offline

#10 2012-05-16 14:18:35

Kjeld
Member
From: Tokyo, Japan
Registered: 2005-02-05
Posts: 446
Website

Re: How create a wish-list widget with Textpattern

jakob wrote:

I guess you may also need to scrub out duplicates too. Maybe there are some idea to be garnered from this thread and from here for removing items from the array. While you’re massaging your comma-separated list into an array and back, you might also want to make sure your array items are integers before plugging the result into txp:article_custom.

I decided to go into a slightly different direction. I have no command of PHP or javascript, so working with arrays is beyond me. I came up with a simple solution. Have a checkbox for each article/image. In the lightbox (wishlist) section, a visitor can uncheck one or more of the images and then hit a button to remove them. I am trying to write a script that reads the remaining articles as a string and writes them into a cookie.

I had something that seemed to work yesterday, but when I cleaned it up things went wrong and now it doesn’t work very well anymore. I know I am close, but I just can’t figure out what I am doing wrong. What I have now is still extremely raw and basic, but let me post the code here so some fresh eyes can have a look. Besides, I think more people are interested in having this function on their site.

1. In the header:

<!-- get "item" variable from url (to get data from form in #2 below --><txp:adi_gps name="item" quiet="1" />
<!-- read cookie and put in variable "lightbox" --><txp:variable name="lightbox"><txp:chs_cookie name="lightbox" /></txp:variable>

2. On the article page in the photo section (the “article” actually shows a photo, not an article), there is an “add to lightbox button” (still a very basic button right now) and some code to manipulate the data:

<!-- if variable "item" has a value, add a comma to the array -->
<txp:if_variable name="item" value=""><txp:else /><txp:variable name="item"><txp:variable name="item" />,</txp:variable></txp:if_variable>
<!-- add to lightbox button --><form method="get" action="<txp:permlink />"><input type="submit" name="item" value="<txp:article_id />" /></form>
<!-- Write previous cookie data and new item id into a new cookie -->
<txp:chs_cookie name="lightbox" expire="1893477600"><txp:variable name="lightbox" /><txp:variable name="item" /></txp:chs_cookie>

3. In the lightbox section, there is a button (and script) to mass remove unneeded items, and article_custom tags to show the articles (id’s are read from the cookie):

<!-- button to remove unchecked items; I will use a css button class to show an image -->
<form method="post" action="<txp:site_url />lightbox/">Remove unselected images: <input name="submit1" class="button_class" type="submit" />
<!-- article_custom --> <txp:article_custom listform="article_listing" limit="9" id="<txp:variable name="lightbox" />" />
<!-- form closes below the articles containing the checkboxes --> </form> 
<!-- write a new cookie with the info gleaned from the "remove" button (it gives an array of the remaining article id's) -->
<txp:php>
if (isset($_POST['submit1'])) {
setcookie("lightbox",substr(implode(',', $_POST['tags']),0),time()+(60*60*24*30));
}
</txp:php>

4. In the article form there is some code for the checkbox:

<input type="checkbox" checked name="tags[]" value="<txp:article_id />" id="<txp:article_id />"><label for="<txp:article_id />">
<!-- Using [] behind the name allows the PHP POST function in #3 above to create an array from the data gleaned from the checkboxes in the article-->

I run into the following problems (among others):

1. I have put <txp:php>echo $_COOKIE["lightbox"];</txp:php> on the pages to see if the cookie is being written correctly. I noticed that it displays different information for the cookie in the photos and lightbox section. So for example, in the photos section it may show, “21,31,41” and in the lightbox section, “19, 21.” I am using exactly the same echo cookie function on both pages. It appears there are two different cookies being written, but I use only “lightbox” as the cookie name and in my browser I can only find one… This is the most important issue.

2. In 3 it doesn’t seem to write the cookie correctly

3. If an article is removed it still shows on the lightbox page. It only disappears after I click on the lightbox link (refreshing would repeat the previous action)

4. When you remove all articles (or the last article) from the lightbox, it gives tag errors:

Tag error: <txp:php> ->  Warning: implode() [function.implode]: Invalid arguments passed  on line 3
Tag error: <txp:php> ->  Warning: implode() [function.implode]: Invalid arguments passed  on line 4

EDITED at 11:23 PM JST

5. I just discovered a new issue. It appears to remove previous articles from the lightbox now. I didn’t encounter that behavior earlier…

You can see it in action and play with it at:

Individual article (photo) page (link removed as test site is now on new domain and in status 503)
Lightbox page (link removed as test site is now on new domain and in status 503)

The site is far from finished. Especially the lightbox functions and layout are still at a draft stage.

Other considerations:
— I wonder if it isn’t better to use post instead of get in 2
— Is the general approach I am taking at the moment safe?

By the way, in the examples above, I have removed most of the if statements that I use to make the code easier to follow

I haven’t done any cross-browser testing yet… I am on a Mac, so if you are on a Windows system there may be some lay-out issues as well.

Last edited by Kjeld (2012-05-17 15:40:22)


Old Photos of Japan – Japan in the 1850s~1960s (100% txp)
MeijiShowa – Stock photos of Japan in the 1850s~1960s (100% txp)
JapaneseStreets.com – Japanese street fashion (mostly txp)

Offline

#11 2012-05-16 15:16:14

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: How create a wish-list widget with Textpattern

Kjeld wrote:

in the photos section it may show, “21,31,41” and in the lightbox section, “19, 21.” I am using exactly the same echo cookie function on both pages. It appears there are two different cookies being written, but I use only “lightbox” as the cookie name and in my browser I can only find one… This is the most important issue.

By default, when a specific path isn’t set, a cookie only applies to the current requested directory. If you need the cookie for whole domain, you can use single slash (/) as the path.

3. If an article is removed it still shows on the lightbox page. It only disappears after I click on the lightbox link (refreshing would repeat the previous action)

Cookies will start applying on the next HTTP request from when the cookie was sent to the client. That’s the first time the client will send the cookie back to the server. The setcookie() function itself doesn’t create anything on the server’s end, it just sends a HTTP header to the client. In addition to sending the cookie to the client, you will also need handle the data on the server’s end by other means (by storing the value in memory as a variable or something like that).

4. When you remove all articles (or the last article) from the lightbox, it gives tag errors:

The implode expects arrays. When you have no articles selected, you will have no POST parameter defined. So, you will have to check if it is defined and an array, or cast it as array. i.e. (array) ps('tags').

Offline

Board footer

Powered by FluxBB