• Hello Friends, I need help to restrict payment based on state. I set two states ‘Yangon’ and ‘Other City’ with following code. And I want to restrict payment based on that states. If the user will choose ‘Yangon’, the payment should available both Direct Bank Transfer and Cash on delivery. But if the user choose ‘Other City’, the payment should available only one Direct Bank Transfer.

    Here is the code

    add_filter( ‘woocommerce_states’, ‘bbloomer_custom_woocommerce_states’ );
    function bbloomer_custom_woocommerce_states( $states ) {
    $states[‘MM’] = array(
    ‘Yangon’ => ‘Yangon’, ‘Other City’
    );
    return $states;
    }

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter minsuwai

    (@minsuwai)

    I restricted with this code but there cod is enabled for both state.

    // Restrict payment
    add_filter('woocommerce_states', 'bbloomer_custom_woocommerce_states');
    
    function bbloomer_custom_woocommerce_states($states) {
        $states['MM'] = array(
            'Yangon' => 'Yangon',
            'Other City' => 'Other City'
        );
        return $states;
    }
    
    add_filter('woocommerce_available_payment_gateways', 'custom_available_payment_gateways');
    
    function custom_available_payment_gateways($gateways) {
        // Get the selected state from the user input
        $selected_state = isset($_POST['billing_state']) ? sanitize_text_field($_POST['billing_state']) : '';
    	
    	error_log('Selected State: ' . $selected_state);
    	
        // Disable Cash on Delivery for all states initially
        if (isset($gateways['cod'])) {
            $gateways['cod']->enabled = false; // Disable COD for all states
        }
    
        // Enable Cash on Delivery only for 'Yangon'
        if ($selected_state === 'Yangon') {
            if (isset($gateways['cod'])) {
                $gateways['cod']->enabled = true; // Enable COD for 'Yangon'
            }
        }
    
        return $gateways;
    }
    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    I recommend asking at https://www.ads-software.com/support/plugin/woocommerce/#new-post so the plugin’s / theme’s developers and support community can help you with this.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Restrict Payment’ is closed to new replies.