Post by JNeuhoff » Tue Jun 30, 2009 4:36 am

Maybe someone who is a jquery guru can help me with this:

I am trying to add 'Add to Cart' buttons to each product listed on the the product/category page which seems to be a fairly straight forward job but it doesn't work. I am using something like this:

Code: Select all

        <form action="<?php echo $products[$j]['href2']; ?>" method="post" enctype="multipart/form-data" id="category_product">
          <input type="hidden" name="quantity" size="3" value="1" />
          <a onclick="$('#category_product').submit();" id="add_to_cart" class="button"><span><?php echo $button_add_to_cart; ?></span></a>
          <input type="hidden" name="product_id" value="<?php echo $products[$j]['product_id']; ?>" />
        </form>
where the values for each $products[$j][$href2] and $products[$j][$product_id] are correctly set in the corresponding controller file. However, each time the form submits a non-existing and always the same invalid product_id='14117033'. If I use something like

Code: Select all

        <form action="<?php echo $products[$j]['href2']; ?>" method="post" enctype="multipart/form-data" id="category_product">
          <input type="hidden" name="quantity" size="3" value="1" />
          <input type="button" onclick="form.submit();" id="add_to_cart" value="<?php echo $button_add_to_cart; ?>" />
          <input type="hidden" name="product_id" value="<?php echo $products[$j]['product_id']; ?>" />
        </form>
then it works fine, but has the dis-advantage of being able to style the <input type="button"> in every browser.

Any ideas how I can it to work with jquery?

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member
Online

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by Qphoria » Tue Jun 30, 2009 5:46 am

Well a few problems...

If you are wanting to use the ajax animation stuff with it, you will have problems with w3 validation as it triggers on <a id="add_to_cart" which means each add to cart button would have the same id.
Same with the form id of "category_product".

But anyway, if you just want it to submit normal POST style, this worked for me (assuming you added the href2 and product_id to the product array in the category controller):

Code: Select all

<form action="<?php echo $products[$j]['href2']; ?>" method="post" enctype="multipart/form-data" id="category_product">
          <input type="hidden" name="quantity" size="3" value="1" />
          <input type="hidden" name="product_id" value="<?php echo $products[$j]['product_id']; ?>" />
          <a onclick="$('#category_product').submit();" id="add_to_cart_1" class="button"><span><?php echo $button_add_to_cart; ?></span></a>
</form>
Note I changed the id of the add to cart button to not use the ajax method

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by JNeuhoff » Tue Jun 30, 2009 6:16 am

Bingo! :) I am now using unique ids and it works like a charm:

Code: Select all

        <form action="<?php echo $products[$j]['href2']; ?>" method="post" enctype="multipart/form-data" id="category_product_<?php echo $j; ?>">
          <input type="hidden" name="quantity" size="3" value="1" />
          <input type="hidden" name="product_id" value="<?php echo $products[$j]['product_id']; ?>" />
          <a onclick="$('#category_product_<?php echo $j; ?>').submit();" class="button"><span><?php echo $button_add_to_cart; ?></span></a>
        </form>
Thanks a lot for your help.

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member
Online

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by bthirsk » Tue Jun 30, 2009 7:35 am

You can use ajax add to cart and not have W3C Validation Problems.

I have it in all modules, category, search, and product pages and they all validate fine.

Active Member

Posts

Joined
Wed Sep 03, 2008 11:33 am
Location - Canada

Post by Qphoria » Tue Jun 30, 2009 7:56 am

Then I assume you changed the ajax function to use the name instead of id? or at least partial id?

$('#add_to_cart').click(function () {
to
$('add_to_cart').click(function () {

or something?

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by bthirsk » Tue Jun 30, 2009 9:30 am

This is sample from category. ID. Everything including button, input, image, quantity have unique id.

Code: Select all

		  <?php if ($category_addtocart) { ?>	
			<?php $option = $product_options[$key]['option_status'];				
			If ($option) {?>
				<div class="options"><?php echo $options_text; ?></div>
			<?php } else { ?>
				<div id="category_<?php echo $product['product_id']; ?>" class="add">				
				<input class="button" type="button" id="category_add_<?php echo $product['product_id']; ?>" value="<?php echo $Add_to_Cart;?>" onclick="$.add2cart('category_image<?php echo $product['product_id']; ?>','cart_content'); $('#mini_cart').load('index.php?controller=addtocart&action=add&item=<?php echo $product['product_id']; ?>&quantity='+document.getElementById('category_quantity_<?php echo $product['product_id']; ?>').value),document.getElementById('category_add_<?php echo $product['product_id']; ?>').value='<?php echo $Added_to_Cart; ?>';">&nbsp;
				<?php if($product['min_qty'] > '1'){
				$i = $product['min_qty'];
				} else {
				$i = 1; }?>
				<select name="category_quantity" id="category_quantity_<?php echo $product['product_id']; ?>">
					<?php while ($i <= '20'){ ?>
						<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
					<?php $i ++;
					} ?>				
				</select>
				</div>
			<?php } ?>
			<?php } ?>

Active Member

Posts

Joined
Wed Sep 03, 2008 11:33 am
Location - Canada

Post by Qphoria » Tue Jun 30, 2009 10:03 am

Looks like a custom ajax function.. not the one from cart.tpl

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by BAJR » Thu Jul 16, 2009 8:38 pm

been battling this.. and have to admit to being a dunce.. :(

in teh category.php controller file, where and how to set the $products[$j][$href2] and $products[$j][$product_id]

many thanks in advance... I am battering my head senseless.. and have only manged to tie myself in knots

New member

Posts

Joined
Sun Jul 12, 2009 6:17 pm

Post by BAJR » Sat Jul 18, 2009 3:37 am

seriously folks.. even a hint... I have gone past banging ma head! it is now mushed to brain jelly.

New member

Posts

Joined
Sun Jul 12, 2009 6:17 pm

Post by bthirsk » Sat Jul 18, 2009 3:58 am

$this->data['products'][] = array(
'name' => $result['name'],
'model' => $result['model'],
'product_id' => $result['product_id'] // THIS WOULD BE A GOOD PLACE TO START
'rating' => $rating,
'stars' => sprintf($this->language->get('text_stars'), $rating),
'thumb' => HelperImage::resize($image, 120, 120),
'price' => $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax'))),
'special' => $special,
'href' => $this->url->http('product/product&path=' . $this->request->get['path'] . '&product_id=' . $result['product_id'])
);

Active Member

Posts

Joined
Wed Sep 03, 2008 11:33 am
Location - Canada

Post by Qphoria » Sat Jul 18, 2009 4:07 am

Code: Select all

$this->data['products'][] = array(
	'name'    => $result['name'],
	'model'   => $result['model'],
	'rating'  => $rating,
	'stars'   => sprintf($this->language->get('text_stars'), $rating),
	'thumb'   => HelperImage::resize($image, 120, 120),
	'price'   => $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax'))),
	'special' => $special,
	'href'    => $this->model_tool_seo_url->rewrite($this->url->http('product/product&path=' . $this->request->get['path'] . '&product_id=' . $result['product_id'])),
	'product_id'    => $result['product_id'],
	'href2'   => $this->url->http('checkout/cart')
);
Add the last 2 entries in the array.

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by BAJR » Sat Jul 18, 2009 4:19 am

ahhhhhhhhhhhhhhhhhhhh bless you!

and thanks for not just totally spoon feeding me

I need to learn.

New member

Posts

Joined
Sun Jul 12, 2009 6:17 pm

Post by BAJR » Sun Jul 19, 2009 6:25 am

right i'm tying myself in more knots than the gordian one..

i wanted to do that custom mini-cart update... and need talked through it all... i'm lost and not afraid to say it... curently i run an asp shop.. and i am an asp sort of guy... php... its like speaking swahili to me.

I note that Daniel is creating a new version.. tonight... tthere was another issue about weight based shipping and going to checkout, which i hope is sorted.. but if add to cart is an option for categories etc .. i am more than happy with this cart.. more than happy..

I pledge a donation of 50 quid and any help i can offer

i jst need help.. and a cart that won't leave me out of pocket for shipping..

basically I need step by step help on what to do and where for the add to cart categories, and in return, i will donate and offer any help i can provide.

oh... and please ;D

New member

Posts

Joined
Sun Jul 12, 2009 6:17 pm

Post by BAJR » Tue Jul 21, 2009 1:25 am

oh well... it would have made a real difference to my intended shop...

sorry... I realise that the release of 1.3 has been on people minds...

I do think this is one of those elements that should be standard.

New member

Posts

Joined
Sun Jul 12, 2009 6:17 pm

Post by Qphoria » Tue Jul 21, 2009 2:20 am

There is nothing more to do than we said.

Add those last 2 array entries that I showed above to the category controller
Then add the form from the template example that JN showed to the category tpl.

Perhaps just easier to ask if JN or bthirsk will share their pre-done versions with you.

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by BAJR » Tue Jul 21, 2009 3:35 am

I tried all that... perhaps I am just daft.. as I obviously did not do it right!

I keep getting an undefinded variable button add to cart in category.tpl

I will ask.. and I also will donate... as this is a great resource and worth supporting.

many thanks

New member

Posts

Joined
Sun Jul 12, 2009 6:17 pm

Post by Qphoria » Tue Jul 21, 2009 4:36 am

BAJR wrote:perhaps I am just daft..
Perhaps :P

edit: catalog/controller/product/category.php
FIND:

Code: Select all

'special' => $special,
AFTER ADD:

Code: Select all

'product_id'    => $result['product_id'],
'href2'   => $this->url->http('checkout/cart'),

edit: catalog/view/theme/default/template/product/category.tpl
FIND:

Code: Select all

<a href="<?php echo $products[$j]['href']; ?>"><?php echo $products[$j]['name']; ?></a><br />
AFTER ADD:

Code: Select all

<form action="<?php echo $products[$j]['href2']; ?>" method="post" enctype="multipart/form-data" id="category_product_<?php echo $j; ?>">
          <input type="hidden" name="quantity" size="3" value="1" />
          <input type="hidden" name="product_id" value="<?php echo $products[$j]['product_id']; ?>" />
          <a onclick="$('#category_product_<?php echo $j; ?>').submit();" class="button"><span><?php echo $button_add_to_cart; ?></span></a>
        </form>

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by BAJR » Tue Jul 21, 2009 7:27 am

Thanks again... but all I get is an error saying button_add_to_cart is undefined.

so I will just be happy as I am...

OR ask thirsk :)

appreciate your help... I am missing something... just dunno!

New member

Posts

Joined
Sun Jul 12, 2009 6:17 pm

Post by Qphoria » Tue Jul 21, 2009 7:39 am

Then define it

edit: catalog/controller/product/category.php
FIND:

Code: Select all

$this->data['order'] = $order;
AFTER ADD:

Code: Select all

$this->data['button_add_to_cart'] = $this->language->get('button_add_to_cart');

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by bthirsk » Tue Jul 21, 2009 7:47 am

As I am not that familiar with this bit of code, I can only suggest you are missing the setting of the variable
$button_add_to_cart so you get the proper language.
To test, why don't you try changing the php echo from the variable $button_add_to_cart to a string like
"AddToCart".
That will at least eliminate that error.

Active Member

Posts

Joined
Wed Sep 03, 2008 11:33 am
Location - Canada
Who is online

Users browsing this forum: No registered users and 5 guests