1

We use a plugin for the payment gateways. There is a setting for recurring subscriptions. Now we have a subscription where we need that checkbox selected and we have one sub where we don't need that recurring checkbox.

This is the original plugin code:

<input type="checkbox" checked name="payrexx-allow-recurring" id="payrexx-allow-recurring" value="1" />

Now, if the special subscription (check with ID) is in the cart, then uncheck the checkbox and hide the checkbox. If another sub is in the cart, do nothing.

Can we do this with PHP or is here js needed? I'm a beginner, so would be nice if you guys can tell me how I can do this and how. :)

Cheers and Thanks

mujuonly
  • 11,370
  • 5
  • 45
  • 75
Nik7
  • 346
  • 2
  • 16

1 Answers1

1
function conditional_checkout_fields_js( $fields ) {
    $cart = WC()->cart->get_cart();

    foreach ( $cart as $item_key => $values ) {
        $product = $values['data'];

        if ( $product->get_id() == 1631817356 ) { // Change the product ID
               $html .= '<script type="text/javascript">
                  jQuery(function() {
                    jQuery("#payrexx-allow-recurring").prop("checked", false); // Unchecks it
                    jQuery("#payrexx-allow-recurring").hide();
                  });
                </script>';
               echo $html;
        }
    }

    return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'conditional_checkout_fields_js' );

Add this to your active child theme functions.php file

mujuonly
  • 11,370
  • 5
  • 45
  • 75
  • Thanks, but it does not work :( nothing happend. Whats do I miss here? Product ID is changed to the ID of a variation. – Nik7 Feb 22 '22 at 12:13
  • View the page source if the JS is rendered when the specific product is in the checkout. – mujuonly Feb 22 '22 at 12:37
  • This works now. But the checkbox disappears but then is added back. I think this is because of the load event. Can we somehow fix this? – Nik7 Feb 22 '22 at 16:14
  • Please replace this "jQuery(function()" using like ready/load events – mujuonly Feb 22 '22 at 16:18
  • Thanks. but I never use this. Can you make a example that I can understand what I have to do? – Nik7 Feb 22 '22 at 16:45