How to include an 'Add to basket' button in a list view of products?

Author Message

Adolfo Barragán

Tuesday 16 September 2003 11:44:26 pm

In my list of products I need include the "Add to Basket" button for every product of the list.

I put this code, but it does not work:

{section name=Child loop=$:products sequence=array( bglight, bgdark )}
...
<input type="image" name="ActionAddToBasket" src={"addtocart.gif"|ezimage} width="32" height="32" alt="Add to Basket" />
...
{/section}

How can I assign $ContentObjectID to the correct value (The Object ID associated to the button pressed).

Thanks in advance
Adolfo Barragan

Paul Forsyth

Wednesday 17 September 2003 1:40:52 am

You are nearly there. This code should add an object to the basket.

{let content_object=$node.object}

<form action={"/content/action/"|ezurl} method="post">

<input type="hidden" name="ContentObjectID" value="{$content_object.id}" />

<input type="submit" name="ActionAddToBasket" value="Add to basket">

</form>

{/let}

Paul

Adolfo Barragán

Wednesday 17 September 2003 3:45:07 am

Yes, but not ... :-)

> You are nearly there. This code should add an object
> to the basket.

Remember that I have your code in a loop (and I'm newbie on web programming).

If I put the form sentences (<form ...> and </form>) outside the loop, when I push any button the form always send the ContentObjectID for last product.

I have a question: Can I put <form ..> and </form> inside the loop (as below)? If I can do, I have a form for each product? (So I have a ContentObjectID for each product). It is possible in HTML?

{let products=fetch(....)}
{section name=Product loop:$products}

{let content_object=$node.object}
<form action={"/content/action/"|ezurl} method="post">
<input type="hidden" name="ContentObjectID" value="{$content_object.id}" />
<input type="submit" name="ActionAddToBasket" value="Add to basket">
</form>
{/let}

{/section}
{/let}

Thanks in advance.
Adolfo Barragan

P.S.
I put this question in two forum becaus I'm not sure what is the appropiate. Excuse me.

Paul Forsyth

Wednesday 17 September 2003 3:53:11 am

Yes you can do something like that. It is good practise to give each of your forms a unique name. For example:

{let products=fetch(....)}

{section name=Products loop=$products}

{let content_object=$Products:item.object}

<form name={concat("AddBasket",$Products:number)} action={"/content/action/"|ezurl} method="post">

<input type="hidden" name="ContentObjectID" value="{$Products:content_object.id}" />
<input type="submit" name="ActionAddToBasket" value="Add to basket">
</form>

{/let}

{/section}

{/let}

The addition of the loop number for the form name should give a unique name to each form.

Try this out.

Paul

Adolfo Barragán

Wednesday 17 September 2003 4:01:09 am

Yes!!!, It's Ok. I just try it (without form name) and work corretly.

I'm going to modify the code to put the form name.

Againg, thank you very much :-)

Adolfo Barragan

Paul Forsyth

Wednesday 17 September 2003 4:05:09 am

Great!

I should actually stress that putting the form name in is more than good practise. If you need to refer to a particular form and you have several on your page you need unique names. Otherwise your page may fail in unexpected ways, and the effect is usually different on each browser. So, always try to put in a name, its safer :)

Oh, and when you do loops like this always have a look at the generated source to see that numbers are being appended/added/created in the way you want.

paul

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 31 2025 06:17:05
Script start
Timing: Jan 31 2025 06:17:05
Module start 'layout'
Timing: Jan 31 2025 06:17:05
Module start 'content'
Timing: Jan 31 2025 06:17:05
Module end 'content'
Timing: Jan 31 2025 06:17:05
Script end

Main resources:

Total runtime0.0134 sec
Peak memory usage2,048.0000 KB
Database Queries3

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0051 588.3906151.2422
Module start 'layout' 0.00510.0025 739.632836.7266
Module start 'content' 0.00760.0043 776.359494.1094
Module end 'content' 0.01190.0015 870.468837.9922
Script end 0.0134  908.4609 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.002518.6144140.0002
Check MTime0.00128.6677140.0001
Mysql Total
Database connection0.00086.289610.0008
Mysqli_queries0.002014.907530.0007
Looping result0.00000.103210.0000
Template Total0.00118.210.0011
Template load0.00096.535010.0009
Template processing0.00021.611510.0002
Override
Cache load0.00064.542910.0006
General
dbfile0.00032.134580.0000
String conversion0.00000.064040.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs