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.
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.