WordPress.org

Forums

WooCommerce - excelling eCommerce
Allow Cash on Delivery only in certain postcodes (6 posts)

  1. Chirag Vora
    Member
    Posted 10 months ago #

    How to allow Cash on Delivery (cod) only in specific postcodes.

    I know there are plugins out there, but most of them are making checkout slow for me.

    Is there a way to do it programmatically?

    https://wordpress.org/plugins/woocommerce/

  2. terrytsang
    Member
    Posted 10 months ago #

    Hi @Chirag, you can put below code into your them functions.php:

    function postcode_cash_on_delivery( $available_gateways ) {
      global $woocommerce;
    
      //example: allow COD in United States (US) and certain postcodes
      $cod_country = 'US';
      $cod_postcodes = array("02115", "02116", "10001", "10002");
    
      if ( isset( $available_gateways['cod'] ) && $woocommerce->customer->get_country() == $cod_county && !in_array($woocommerce->customer->get_postcode(), $cod_postcodes)) {
        unset(  $available_gateways['cod'] );
      }
    
      return $available_gateways;
    }
    
    add_filter( 'woocommerce_available_payment_gateways', 'postcode_cash_on_delivery' );
  3. Chirag Vora
    Member
    Posted 10 months ago #

    Thanks Terry that works.
    However, in my case I have more than 3000 postcodes that allow cash on delivery.
    When put into the array within functions.php, the checkout is becoming a bit slow.

    Is there any alternative to this approach?

  4. Magesh R
    Member
    Posted 6 months ago #

    hi chirag vora,

    kidly guide me, where is specified function.php file. And where do i put these code in function.php

  5. Magesh R
    Member
    Posted 6 months ago #

    Hi Friends,

    kidly guide me, where is specified function.php file. And where do i put these code in function.php

  6. terrytsang
    Member
    Posted 6 months ago #

    Hi, after login to WordPress Admin, you can mouseover Appearance sidebar menu, then click 'Editor' link, then on your right sidebar, open functions.php file and put those at the end of file and before symbol '?>'.

    You can refer my tutorial here also.

Reply

You must log in to post.

About this Plugin

  • WooCommerce - excelling eCommerce
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic