Forum Replies Created

Viewing 15 replies - 1 through 15 (of 29 total)
  • Anyone?

    Resolve the issue.

    I wasn’t passing $contents as a param into build_home_page($contents){}. And then I just needed to return $contents in the else.

    Hope this helps someone.

    Thanks for all the help with this Mike. I finally got it working. Thanks again.

    Ok I understand about scope. Sorry to have to ask you beyond the boundaries.

    But just to clarify, you’re suggesting combining all the forms on the calculator display page? There are 4 in total, separate forms. Are you saying that I can’t catch the posting of a specific form by checking if $_POST[‘submitBtnName’] is set using the custom form handler you suggested?

    Right. Well I guess the goal of this is to:

    1) Display the calculator on the home page.
    2) Upon submit from the home page calculator, display recommended products to user on calculator-display.php based upon adults/kids/months entries from previous page (products are hard coded as you can see above. and it’s all real simple calculations)
    3) There are 4 forms on the calculator-display page, the user can select one. using if (isset($_POST['buttonName'])) the goal is to capture the submit button from the form the user selected, and pass certain products to the add_to_cart function based upon their selection.

    Based upon the above, and the code that I’ve copied here, do you feel there is another approach I could or should be following to develop this plugin?

    Regarding your statement about submit-essential being outside of the form with adultsField, I am only posting one form at a time. When I place the custom_form_handler on the same page as the submit-essential form (calculator display page) that handler isn’t touched. It doesn’t execute.

    Haven’t yet implemented sanitizing as of yet. Was just trying to get this core functionality up and running first.

    Another thing I’m finding…

    This chunk of code here:

    $temp = $_POST['adultsField'];
    
            echo 'LOOK HERE '. $temp; // THIS HERE - NO VALUE
    
            // Populate shopping cart
            WC()->cart->add_to_cart($adultsProductID,2);
            WC()->cart->add_to_cart($kidsProductID,2);

    If I replace the ‘2’ with ‘$temp’ as a param of add_to_cart(), it of course doesn’t populate the cart with anything because $_POST[‘adultsField’] isn’t bringing any data back, but it also doesn’t hit the custom_form_handler() at all. I’m not sure what behind the scenes would be causing this. Any thoughts?

    Also I really appreciate all the help you have provided with this. Truly.

    Yeah I hear you. Im not sure how this is occurring. Here’s some code, let me know if you see something that I don’t. I literally have no idea why it’s behaving this way.

    From my calculator.php // Basically puts the calc on the home page.

    *
     * Responsible for placing the calculator form in the_content section of the home page
     *
     */
    function add_calculator_to_page(){
    
        // Check page destination before printing form to page
        if (is_front_page()) {
            echo '<form action="/myCompany/calculator-display/" method="post" class="calc-homepage">';
            echo 'Number of Adults<br />(120 lbs or more)<br /><input type="text" name="adultsField" required /><br />';
            echo '<br />Number of Kids<br />(120 lbs or less)<br /><input type="text" name="kidsField" /><br />';
            echo '<br />Number of Months<br /><input type="text" name="monthsField" required/><br />';
            echo '<br /><input type="submit" name="submit" value="View Recommended Buckets"/>';
            echo '</form>';
        }
        else{
            // Nothing currently
        }
    }
    
    add_action('wp', 'custom_form_handler');
    
    function custom_form_handler(){
    
        if (isset($_POST['submit-essential'])){
    
            echo '<script>alert("hit this hit");</script>';
    
            // Hardcoded product IDs
            $adultsProductID = 96; // Hardcoded IDs, these are the products to add
            $kidsProductID = 90;
    
            $temp = $_POST['adultsField'];
    
            echo 'LOOK HERE '. $temp; // THIS HERE - NO VALUE
    
            // Populate shopping cart
            WC()->cart->add_to_cart($adultsProductID,2);
            WC()->cart->add_to_cart($kidsProductID,2);
    
            //Header("Location: /ReliefFoods_Playground/checkout");
        }
    }

    And this is from my calculator-display.php // I re-display the calculator on this page as well as the results of the user entry.

    <aside class="inner-calc-form">
        <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" class="calc-homepage">
            Number of Adults<br />(120 lbs or more)<br /><input type="text" name="adultsField" value="<?php echo $_POST['adultsField']; ?>" required /><br />
            <br />Number of Kids<br />(120 lbs or less)<br /><input type="text" name="kidsField" value="<?php echo $_POST['kidsField']; ?>"/><br />
            <br />Number of Months<br /><input type="text" name="monthsField" value="<?php echo $_POST['monthsField']; ?>" required/><br />
            <br /><input type="submit" name="submit" value="View Recommended Buckets"/>
        </form>
    </aside>
    
    <div class="form-style-container">
        <!---- #1 Essential Form ---->
        <form action="/myCompany/checkout" method="post" class="calc-homepage">
            <div class="quantity_display">
                <h1>Essential</h1><br />
                <span>Quantity: <?php echo $adults ?> - 70521-RF</span>
                <?php if ($kids > 0){ ?>
                    <br /><span>Quantity: <?php echo $kids?> - 70401-RF (added per kid)</span>
                <?php } ?>
            </div>
            <br /><input type="submit" name="submit-essential" value="Buy Now" />
        </form> <!-- End essential form -->

    Agreed that it needs to be.

    The only thing is, that hook into ‘wp’ as well as the custom form handler don’t run when they are on the same page as the forms are declared (the calculator display page). They only run when after I’ve declared them on the first calculator page. It feels odd to me.

    This would be fine if the data was able to persist by means of calling the function in the first .php from the second display .php. But I’m finding that the data housed within my $_POST drops. My brain is tired, not sure if I’m making complete sense.

    Ok I got it working for the most part. Which is awesome. Many thanks Mike.

    Let me bounce this off you though. So what I had to do to get it to work was to place the ‘custom_form_handler’ function and associated hook in my calculator.php. Sort of using this as the base file of my plugin. So contained within said ‘custom_form_handler’ is the logic:

    if (isset($_POST['submit-form-1'])){
        // WC() calls
    }

    But the form with the name ‘submit-form-1’ lives on my calculator-display.php. The page that displays the results and such.

    Here’s the issue: I can’t seem to call values from the calculator display page back to my calculator page (1st page). Here’s some code:

    function custom_form_handler(){
        if(isset($_POST['submit-form-1'])){
            $productID = 90
    
            WC()->cart-add_to_cart($productID,<IneedThis>); // Issue
        }
    }

    I need to be able to get the quantity specified from the next page. I have tried creating a class, instantiating it and using the properties where I’ve set the quantity values to, and nothing. I’ve tried accessing the values posted within $_POST, to no avail. Would you happen to have any suggestions on how to resolve this?

    So WP newbie question time:

    add_action('wp', 'custom_form_handler');
    
    function custom_form_handler() {
        if (isset($_POST['submit-form-1')){
            // WC() calls
        }
    }

    How do I execute that function on form submit by going this route? What would be the tie in from the form itself?

    Could you clarify a bit what you mean by that? Maybe a little code snippet?

    The hooks you mentioned earlier I placed in the functions.php file of my current theme. This is what is placed there:

    function custom_set_session(){
        WC->session->set_customer_session_cookie(true);
    }

    And then the hook:

    add_action('wp', 'custom_set_session');

    I know from the code that the form is being submitted because once I actually click the submit button, my conditional statement is hit. That alert() window pops. My plan is to implement a redirect to the cart page once I can get this piece running.

    Alright. I removed the action and re-ran.

    You mean the location of the conditional statement? It’s in a code block at the bottom of the above mentioned display page.

    Check this out:

    What I’ve built is a calculator plugin for a client that makes suggestions on the amount of product a customer should buy. Pretty simple. Calculator displays on the home page and is a basic form that posts entries upon submit.

    On submit it redirects to my calculator.php where it displays the results of the home page calculation. There are 4 different forms on this page and each displays different products that the user can select from. The form that the user selects from this page will determine what product gets placed in the cart.

    Here’s some code from the calculator.php (display page mentioned above):

    <form  action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
        <div class="quantity_display">
            <h1>Product Group 1</h1><br />
            <span>Quantity: <?php echo $adults ?> - ProductNameHere</span>
            <?php if ($kids > 0){ ?>
                <br /><span>Quantity: <?php echo $kids?> - OtherProductNameHere(added per kid)</span>
            <?php } ?>
        </div>
        <br /><input type="submit" name="submit-form-1" value="Buy Now" />
    </form>

    On submit, the form submits to PHP_SELF so I can catch the actual submit. And since I’m working with multiple forms this page, I created the below conditional statement:

    if (isset($_POST['submit-form-1'])){
    
        echo '<script>alert("submit button hit. woot!"); </script>';
    
        // Hardcoded product IDs
        $adultsProductID = 96;
        $kidsProductID = 90;
    
        // Populate shopping cart - NOT WORKING - HALP
        WC()->cart->add_to_cart($adultsProductID,$adults);
        WC()->cart->add_to_cart($kidsProductID,$kids);
    }

    Here’s what’s boggling my mind with this issue.. If I pull the WC() calls out of this condition and allow the page to intercept them on load, literally commenting out the condition and placing the calls outside of the brackets, it adds these two products to the cart. But when I place the WC() calls in the condition and try to pick them up on form submit, I’m dead in the water.

    Thoughts?

    Any other thoughts you may have Mike?

Viewing 15 replies - 1 through 15 (of 29 total)