Author Topic: Sold-out product filter - few lines for cup of coffee  (Read 4232 times)

mrbizzy

  • Beginner
  • *
  • Posts: 5
Sold-out product filter - few lines for cup of coffee
« on: February 03, 2011, 13:30:17 pm »
Hello Friends!
Idea: Put a checkbox on product browse page:

*when checked - show products that are out of stock
*when un-checked do not show out-of-stock products.

I can make it work for one page load by using form POST or GET.
I can even make it work for the whole session by using joomla sessions.

But i can't make it change the session value when unchecked.

So basically i need a few lines that will:

1. Set session to state "A"  when checkbox is checked
2. Set session to state "B"  when checkbox is un-checked
 
Also - after setting it to state "A" it should stay in that state and checkbox should appear as CHECKED until it is unchecked by the user.

The product sorting and filtering i will do myself.

I am open to any other solutions too.

It looks to me that this will only take a few lines of code but i can't find the correct approach.

I would love to buy you a cup of coffee if you help me out with this :)


Regards, Casper

Joseph Kwan

  • Advanced
  • Full Member
  • *****
  • Posts: 2344
Re: Sold-out product filter - few lines for cup of coffee
« Reply #1 on: February 04, 2011, 09:02:24 am »
You can use the $_SESSION global variable to record the state. Say $_SESSION['out_of_stock']=1;
Joomla/VM Upgrade Services. Problems with your migration? We can help.
Custom extensions to VM. Performance Tuning. Template modifications and advices.
Pay service to make VM work according to your needs. Your Joomla/VM solutions are just a PM away.

lowmips

  • Global Moderator
  • Full Member
  • *
  • Posts: 1464
    • lowmips.com
Re: Sold-out product filter - few lines for cup of coffee
« Reply #2 on: February 04, 2011, 14:26:26 pm »
You'll need to use 2 inputs...the checkbox and a hidden input to indicate that the user has clicked the checkbox  (when a checkbox is unchecked, its value is not posted).

The show out of stock form:

Code: [Select]
<?
$checked = '';
if($_SESSION['showoos']=='1') $checked=' checked ';
?>
<form name="showoos-form" method="post">
<input type="checkbox" name="showoos" <?=$checked ?> onClick="document.showoos-form.submit" /> Show out of stock items
<input type="hidden" name="oosclick" value="1" />
</form>


Then, place the following in a global file, probably the best would be /components/com_virtuemart/virtuemart.php:

Code: [Select]
if($_POST['oosclick']=='1'){
if($_POST['showoos']=='1'){
$_SESSION['showoos']==1;
}else{
$_SESSION['showoos']==0;
}
}


You'll probably want to use a few 'isset()' in there to stop any warnings on the front end.


Large, two sugars, extra cream pls =)
Visit my website at www.lowmips.com
View my newsletters Here (sign up for newsletters on the front page of my website)

mrbizzy

  • Beginner
  • *
  • Posts: 5
Re: Sold-out product filter - few lines for cup of coffee
« Reply #3 on: February 07, 2011, 18:10:19 pm »
Thank you for the effort. but something is missing/not-working here.
After submitting the form checkbox is unchecked again..
Also onClick does not work.. so i tried to add a submit button to test it.

Please keep in mind that when it comes to the coding i am not the brightest person around. So maybe there was something i should ADD in there which i did not understand i have to ????

lowmips

  • Global Moderator
  • Full Member
  • *
  • Posts: 1464
    • lowmips.com
Re: Sold-out product filter - few lines for cup of coffee
« Reply #4 on: February 07, 2011, 18:41:57 pm »
I goofed the second piece of code...I did a boolean comparison instead of actually setting the variables.  Change it to this:

Code: [Select]
if($_POST['oosclick']=='1'){
if($_POST['showoos']=='1'){
$_SESSION['showoos']=1;
}else{
$_SESSION['showoos']=0;
}
}
Visit my website at www.lowmips.com
View my newsletters Here (sign up for newsletters on the front page of my website)

lowmips

  • Global Moderator
  • Full Member
  • *
  • Posts: 1464
    • lowmips.com
Re: Sold-out product filter - few lines for cup of coffee
« Reply #5 on: February 07, 2011, 19:20:23 pm »
and the first bit of code javascript should have parens..

Code: [Select]
<?
$checked = '';
if($_SESSION['showoos']=='1') $checked=' checked ';
?>
<form name="showoos-form" method="post">
<input type="checkbox" name="showoos" <?=$checked ?> onClick="document.showoos-form.submit()" /> Show out of stock items
<input type="hidden" name="oosclick" value="1" />
</form>
Visit my website at www.lowmips.com
View my newsletters Here (sign up for newsletters on the front page of my website)

mrbizzy

  • Beginner
  • *
  • Posts: 5
Re: Sold-out product filter - few lines for cup of coffee
« Reply #6 on: February 07, 2011, 19:36:38 pm »
GREAT - it is functioning perfectly!
Now when it works it looks even better than i imagined :)

karlozzz

  • Beginner
  • *
  • Posts: 6
Re: Sold-out product filter - few lines for cup of coffee
« Reply #7 on: March 26, 2011, 22:00:49 pm »
Hi guys, please could you guide how and where to put which code to make it work ?
It looks like great feature, but i cant make it work just with these code //

Free coffees includes ! 8)