1

I have a WooCommerce site with Gravity forms and a plugin called WooCommerce Gravity Forms Product Add-ons that allows me to add a Gravity Forms form to a product.

I would like to hide a woocommerce payment gateway based on the selection of one of the radio inputs in the Gravity Form that is on the product.

I know that the form is ID 1 and I know the field I'm interested in (a radio input) is field 8.

I have tried a number of things based on Gravity Forms documentation as well as documentation from the payment gateway and from StackOverflow posts.

// remove Partial.ly for hospice selection

add_action('gform_after_submission_1', 'get_hospice_info', 10, 2); 
function get_hospice_info($entry, $form) {
$hospiceinfo = $entry['8'];
}

function hospice_selector_remove_partially($gateway) {
if ($hospiceinfo = "Yes, member under hospice or hospital care") {
$unset = true;
}
if ( $unset == true ) unset( $gateway['partially'] );
return $gateway;
}     add_action('woocommerce_available_payment_gateways','hospice_selector_remove_partially');

I feel i'm close. But it is removing the gateway even when the other radio option is selected.

Any help would be greatly appreciated if possible.

4
  • Can you show us screenshots? Commented Mar 2, 2019 at 3:09
  • Of the Gravity form fields or of the payment step of checkout (or both)? Commented Mar 4, 2019 at 13:17
  • The screen shot of the form field: nimb.ws/ShKRBk The screen shot of the checkout: nimb.ws/DwXpPT You'll see that "No, member not under hospice/hospital care" is selected. Which means the payment plan should show up IF i actually had the function written correctly. Please let me know if you were looking for different screen shots. Commented Mar 5, 2019 at 12:45
  • I am looking for a very similar solution. Actually the same! Did you sort this out? Commented Oct 14, 2020 at 20:01

2 Answers 2

1

Janna--

The get_hospice_info function sets a variable, $hospiceinfo, which is only in the scope of that particular function. Therefore, $hospiceinfo will be undefined in the hospice_selector_remove_partially function. You would have to set $hospiceinfo as a global variable.

Secondly, even if the variable was available in the hospice_selector_remove_partially function, the single = in the first line of said function actually sets the variable equal to the value "Yes, member under hospice or hospital care". You would need at least two == to compare the value. This is an often overlooked typo, which is why WP states yoda conditions are a best practice.

Please see the following code which addresses the above listed concerns, as well as removes unnecessary code:

add_action('gform_after_submission_1', 'get_hospice_info', 10, 2); 
function get_hospice_info($entry, $form) {
    global $hospiceinfo;
    $hospiceinfo = $entry['8'];
}

function hospice_selector_remove_partially($gateway) {
    global $hospiceinfo;
    if ("Yes, member under hospice or hospital care" == $hospiceinfo ) {
        unset( $gateway['partially'] );
    }
    return $gateway;
}   
add_action('woocommerce_available_payment_gateways','hospice_selector_remove_partially');

Now, this answer assumes that both of these actions are called during the same page load and that the gform_after_submission action is called prior to the woocommerce_available_payment_gateways action. This would have to be verified to be sure the above code will actually provide the desired outcome.

Ideally, you would fetch the value of the hospice_info field during the woocommerce_available_payment_gateways action and do away with the gform_after_submission callback entirely. Without thoroughly examining the code however, I do not know if that will be possible.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for the updated functions and the links to more info (the yoda conditions were interesting). However, I'm still seeing the payment gateway regardless of the selection made in the Gravity Form on the product. I trying add_filter since I have two functions that successfully hide the gateway which use this instead of add_action however, this didn't work either. You mentioned the after submission action. I'm wondering if I should look at gravity forms get input value instead: docs.gravityforms.com/gform_get_input_value Going to give it a shot
0

You can loop through all the cart items and get the Gravity Field values. Then use your if logic to unset shipping methods

function disable_local_shipping_on_checkout($available_methods)
{

     // Find each product in the cart and add it to the $cart_ids array
     foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
         $cart_product = $values['_gravity_form_lead'];
         
         $field_value   = $cart_product[109]; //109 is the form field ID
         if ($field_value == 'Whatever field value you want') :

            unset($available_methods['wf_shipping_ups:03']); //You can find this by inspecting your shopping cart or checkout.
        endif;
    }
    return $available_methods;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.