Page 1 of 1
[solved] Compare Behavior and Clear All
Posted: Thu Jul 05, 2012 9:05 am
by Cartyhe
Hi Again.
I noticed, I can continue adding products to my comparison list without limits, but when comparing, the page only displays 4 items and these are the first 4 added to the list. I found this post
http://forum.opencart.com/viewtopic.php ... re#p309013 ...and will follow it to find out if this is possible to give a warning that the limit had been reached. That would be really great so people know what's going on (otherwise they may be wondering, get upset and leave... I would

)
But!, What I wanted to achieve is to also add a button "Clear All", to allow people to clear the entire comparison with one click of the button!
Possible?
Thanks a lot again!
Re: Compare Behavior and Clear All
Posted: Fri Jul 06, 2012 5:48 am
by Avvici
The clear all button would be pretty simple to do. You just have to unset the session.
In compare.php find this line:
Code: Select all
'remove' => $this->url->link('product/compare', 'remove=' . $product_id)
Right before it add this:
Code: Select all
'clearall' => $this->url->link('product/compare', 'clear=clearall'),
Now find this:
Code: Select all
if (isset($this->request->get['remove'])) {
Right before it add this:
Code: Select all
if (isset($this->request->get['clearall'])) {
unset($this->session->data['compare']);
$this->session->data['success'] = $this->language->get('text_remove');
$this->redirect($this->url->link('product/compare'));
}
Now open your compare.tpl and set up your clearall button like this:
Code: Select all
<a href="<?php echo $product['clearall']; ?>" class="button">Clear All</a>
Re: Compare Behavior and Clear All
Posted: Fri Jul 06, 2012 2:51 pm
by Cartyhe
@avvici
Thanks for that, legendary!
Everything works great but I get an error : The requested URL /shop/index.php was not found on this server.
Was thinking about and tried a few variations but to no avail. Logically, correct me if I'm wrong, but this "compare" page would not show if there weren't any products to compare, right? It would just tell me that I have no products yet added to the compare list. But the following code would call exactly call for the compare page, which - doesn't exist any longer after the products had been cleared, and that's why I get this error, right?
Code: Select all
$this->redirect($this->url->link('product/compare'));
So, I thought (don't laugh, ok,

) I add an onclick event - which would revert me back to the last page I'd visited, after I cleared all products, (And, of course, it should then also show a "0" - like ... "Product Compare (0)" ... but I couldn't get it going. (maybe it's because the href redirect is a PHP call? And I just don't know how to place that "back to the last page" into that PHP call... sorry for my poor PHP

Re: Compare Behavior and Clear All
Posted: Fri Jul 06, 2012 3:20 pm
by Cartyhe
I just tried with some other, e.g.
- $this->redirect($this->url->link('product/category'));
, but I'd still get the same error.
If I then use the browser back button, I can but also see that the products in the compare list are still in the list, yet, it's not only a wrong redirect but also, the compare list wasn't cleared neither (I refreshed the page, of course) ??
the error "the page ... could not be food, has the following URL in the browser;
Code: Select all
http://.../%3Cb%3ENotice%3C/b%3E:%20Undefined%20variable:%20product%20in%20%3Cb%3E/.../catalog/view/theme/theia/template/product/compare.tpl%3C/b%3E%20on%20line%20%3Cb%3E22%3C/b%3E
Still confused about this error... why it's looking for shop/index.php? ...?
The requested URL /shop/index.php was not found on this server
If I compare the "clearall" with the "remove" code, the redirect is the same and it works fine there! //

(see screen-shot)
Thanks
Re: Compare Behavior and Clear All
Posted: Fri Jul 06, 2012 4:46 pm
by Avvici
Slow down a little. First, the compare page can show at any time. If there are no products to show then it would say NO PRODUCTS. I made a little type in the code that was keeping it from working properly.
Replace"
Code: Select all
'clearall' => $this->url->link('product/compare', 'clear=clearall'),
With this:
Code: Select all
$this->data['clearall']=$this->url->link('product/compare', 'clear=clearall');
And use this instead:
Code: Select all
if (isset($this->request->get['clear'])) {
$this->session->data['compare'] = array();
$this->session->data['success'] = $this->language->get('text_remove');
$this->redirect($this->url->link('product/compare'));
}
Lastly, make your button like this:
Code: Select all
<a href="<?php echo $clearall; ?>" class="button">Clear All</a>
I just tested this on my own comparison list and it clears the list just fine. Do everything like I said to do it. Place the CLEAR BUTTON outside of the product loop somewhere after this code:
Code: Select all
<?php foreach ($products as $product) { ?>
<td class="remove"><a href="<?php echo $product['remove']; ?>" class="button"><?php echo $button_remove; ?></a></td>
<?php } ?>
Re: [solved] Compare Behavior and Clear All
Posted: Sat Jul 07, 2012 12:40 am
by Cartyhe
As I said, you'r a legend
and yes, I tend to run off sometimes, hope you don't mind.
It was indeed - the product loop ! (never would have thought about this myself, thus I need someone like you to knock on my head) I placed the button into the unused td/td, (line 113) left from and in line of the "remove" button and it works smooth as.
Thanks Avvici!
BTW... how to mark a post as "Resolved"?
Re: [solved] Compare Behavior and Clear All
Posted: Fri Jan 11, 2013 10:52 pm
by MikeSCC
Thank you, this is very helpful.
Mike