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

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