VirtueMart Forum

VirtueMart General => Commercial Jobs => Topic started by: mrbizzy on February 03, 2011, 13:30:17 PM

Title: Sold-out product filter - few lines for cup of coffee
Post by: mrbizzy 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
Title: Re: Sold-out product filter - few lines for cup of coffee
Post by: Joseph Kwan 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;
Title: Re: Sold-out product filter - few lines for cup of coffee
Post by: lowmips 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:

<?
$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:

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 =)
Title: Re: Sold-out product filter - few lines for cup of coffee
Post by: mrbizzy 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 ????
Title: Re: Sold-out product filter - few lines for cup of coffee
Post by: lowmips 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:

if($_POST['oosclick']=='1'){
if($_POST['showoos']=='1'){
$_SESSION['showoos']=1;
}else{
$_SESSION['showoos']=0;
}
}
Title: Re: Sold-out product filter - few lines for cup of coffee
Post by: lowmips on February 07, 2011, 19:20:23 PM
and the first bit of code javascript should have parens..

<?
$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>
Title: Re: Sold-out product filter - few lines for cup of coffee
Post by: mrbizzy on February 07, 2011, 19:36:38 PM
GREAT - it is functioning perfectly!
Now when it works it looks even better than i imagined :)
Title: Re: Sold-out product filter - few lines for cup of coffee
Post by: karlozzz 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)