skip to Main Content

I have a unique situation where we only allow 1 form of shipping and 1 form of payment for a particular brand of products. If this brand of products is in the cart, the customer can only pick up and can only pay via bank transfer.

When both of these are the only options and are selected by default, users tend to ignore it and place their orders, leading to more work due to needing to call the customer to follow up on the order.

Is there a way to make the only shipping and payment methods remain as radio buttons to be selected?

I’ve tried implementing the below code to not select one by default, but due to the shipping method not being a radio button, this doesn’t work, as well as the payment method code not working either.

add_filter( 'pre_option_woocommerce_default_gateway' . '__return_false', 99 );
add_filter( 'woocommerce_shipping_chosen_method', '__return_false', 99);

2

Answers


  1. It looks like you’re trying to force the WooCommerce shipping payment method to be displayed as a radio button. This can be done using custom code or a plugin. However, if you’re also looking for a payment gateway solution that seamlessly integrates with WooCommerce and offers a high degree of customization, you might want to consider using MoneyCollect.

    MoneyCollect provides a comprehensive payment gateway that can address your needs effectively.

    Login or Signup to reply.
  2. To ensure that customers manually select the only available shipping and payment methods for a particular brand of products, you can use a combination of JavaScript and PHP. This will prevent the methods from being preselected and prompt the user to make a selection.

    Step 1: Add JavaScript to Handle the UI Behavior
    Add the following JavaScript to your theme’s functions.php file or a custom plugin to unselect the default options when the checkout page loads and ensure the user manually selects them before proceeding:

    function custom_checkout_scripts() {
        if (is_checkout()) {
            ?>
            <script type="text/javascript">
                jQuery(document).ready(function($) {
                    // Unselect the default shipping method
                    $('input[name="shipping_method[0]"]').prop('checked', false);
    
                    // Unselect the default payment method
                    $('input[name="payment_method"]').prop('checked', false);
    
                    // Ensure user selects shipping method before proceeding
                    $('form.checkout').on('submit', function(e) {
                        if (!$('input[name="shipping_method[0]"]:checked').length) {
                            alert('Please select a shipping method.');
                            e.preventDefault();
                            return false;
                        }
                    });
    
                    // Ensure user selects payment method before proceeding
                    $('form.checkout').on('submit', function(e) {
                        if (!$('input[name="payment_method"]:checked').length) {
                            alert('Please select a payment method.');
                            e.preventDefault();
                            return false;
                        }
                    });
                });
            </script>
            <?php
        }
    }
    add_action('wp_footer', 'custom_checkout_scripts');
    

    Step 2: Modify WooCommerce Filters
    In addition to the JavaScript, use the following PHP filters to ensure WooCommerce does not preselect any shipping or payment methods on the server side:

    // Disable the default payment gateway selection
    add_filter('pre_option_woocommerce_default_gateway', '__return_false');
    
    // Disable the default shipping method selection
    add_filter('woocommerce_shipping_chosen_method', '__return_false', 99);
    

    This should help….

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search