The Foxy forums are on the move!

We're in the process of moving our forums over to a new system, and so these forums are now read-only.
If you have a question about your store in the meantime, please don't hesitate to reach out to us via email.

PreProcess Javascript

RickWRickW Member
in Help edited November 2011
A while back I upgraded to FC7.1 and failed to test the following code. It no longer requires the terms & conditions checkbox to be checked. Can you help?
I'm assuming it was due to the upgrade. Thanks in advance.

http://www.wetzelandcompany.com/Registration4.php

<script type="text/javascript" charset="utf-8">
function fc_PreProcess(MyPPFoxyData, MyPPFoxyID)
{
// check for cart empty...if so, we always want success
if(MyPPFoxyData.indexOf('cart=empty') != -1)
{
//ignore validaton when clearing the cart
return true;
}
else
{
//we're adding an item, so check for the terms checkbox
if(MyPPFoxyData.indexOf('Terms=Terms') != -1)
{
return true;
}
else
{
alert('You must accept the Terms and Conditions for this product.');
return false;
}
}
}
</script>
Comments
  • brettbrett FoxyCart Team
    edited November 2011
    Hi Rick.
    Is that checkbox after that initial signup page, and if so, are you ok with us creating dummy accounts as we test? I don't see it on that link itself, but I don't want to create dummy users if that's not acceptable.

    As far as the code, the fc_PreProcess() has been replaced with much more flexible functionality outlined here:
    http://wiki.foxycart.com/static/redirect/javascript

    Specifically:
    http://wiki.foxycart.com/v/0.7.1/advanced/javascript#preprocess_before_an_add-to-cart

    I haven't tested this, and your arguments (the MyPPFoxyID and MyPPFoxyData) might not be in the right order (or might need slight modification wrt how you access them, since the first argument (where you had MyPPFoxyID) isn't an ID anymore but a reference to the element itself), but this is the basic idea. Not too many changes. Let us know if you need help with it:
    <script type="text/javascript" charset="utf-8">
    fcc.events.cart.preprocess.add(function(MyPPFoxyID, MyPPFoxyData) {
    	// check for cart empty...if so, we always want success
    	if(MyPPFoxyData.indexOf('cart=empty') != -1)
    	{
    	//ignore validaton when clearing the cart
    	return true;
    	}
    	else
    	{
    	//we're adding an item, so check for the terms checkbox
    	if(MyPPFoxyData.indexOf('Terms=Terms') != -1)
    	{
    	return true;
    	}
    	else
    	{
    	alert('You must accept the Terms and Conditions for this product.');
    	return false;
    	}
    	}
    });
    </script>
    
    
  • I tried your new code and it still didn't work.
  • fc_adamfc_adam FoxyCart Team
    Hey Rick,

    Two things to try.

    Firstly, put the code Brett gave you after the FoxyCart includes, as it needs that code before what Brett has done for you can function.
    Second, remove the jQuery you're including yourself
    <script type="text/javascript" src="jquery.js"></script>
    
    , as FoxyCart's includes has a jQuery with that.
  • Hi fc_adam
    That didn't help either. What I did was to strip out all code unrelated to my issue, made adjustments as suggested, and uploaded it to

    http://www.wetzelandcompany.com/Registration7.php

    There must be some small syntax or typo that I've missed.
  • fc_adamfc_adam FoxyCart Team
    @RickW,

    Sorry, I may not have been clear, you need to move the code Brett pasted above to be below the FoxyCart includes that you get from the Sample Code section of the administration. Give that a try.
  • Hi FC_Adam
    It doesn't matter where the code snippet goes that Brett provided. It just doesn't work. I've eliminated more coding in an effort to isolate the problem. It is now just bare bones.
    check out the following...
    http://www.wetzelandcompany.com/Registration8.php
    http://www.wetzelandcompany.com/Registration9.php
    http://www.wetzelandcompany.com/Registration10.php
  • brettbrett FoxyCart Team
    Bah, I'm sorry @RickW.
    http://pastie.org/2850131
    I missed a closing parenthesis on my first post (now corrected, but use that pastie just in case).

    I just tested that and it seems to work. Sorry for the wasted energy.
  • That worked! Thank you!!!
  • Just when I thought everything was working fine, it's not. The script for the terms box is still not right. If you check off the terms box the alert still comes up! Can you help? I had to disable the script on the actual registration form, but you can view the simplified version below.

    http://www.wetzelandcompany.com/Registration10.php
  • lukeluke FoxyCart Team
    Hey Rick. Have you tried adding some debugging statements to see what you have there? For example:
    if(MyPPFoxyData.indexOf('Terms=Terms') != -1) {
    
    If that isn't evaluating to true, it won't work. So the process to debug the script and figure things out would first include understanding what is in MyPPFoxyData. So add something like this:
    alert(MyPPFoxyData)
    
    or, better yet, with FireBug enabled, this:
    console.log(MyPPFoxyData)
    
    From there you can try different things to see what is there. You can also set a break point using Firebug to inspect things also. Here's the result of the console.log statement:
    jq5pgi.jpg
    That shows how MyPPFoxyData is an associative array so instead of searching for Terms=Terms, we need to see if the key exists for the form field we've added like so:
        	if (MyPPFoxyData['h%3ATerms']) {
    
    Use that instead of
    if(MyPPFoxyData.indexOf('Terms=Terms') != -1) {
    
    and that seems to work.

    Cool?
  • Thanks you!!! Works like a charm.
Sign In or Register to comment.