Support » Plugin: WooCommerce » Add information notice to customer when Cash on Delivery is not Available

  • Resolved iamlaurie

    (@abanista)


    I want to display this notice on checkout page if cash on delivery payment gateway is not available for the customer and if cash on delivery is available, I don’t want it to display. I can’t seem to get it work the right way

    [php]
    add_action( ‘woocommerce_checkout_before_terms_and_conditions’, ‘abanista_cod_not_available_info_notice’ );

    function abanista_cod_not_available_info_notice() {

    $available_payment_methods = WC()->payment_gateways->get_available_payment_gateways();

    $html = "<br><div class=’woocommerce-delivery-message’ style=’clear:both’>Why Pay on Delivery is Not Available: One or more of the items you’re buying or your shipping region does not support this option. Your selected item is on Backorder (to be shipped from U.K) or the option is not allowed on your account based on your past delivery experience</div>";

    echo $html;
    }
    [/php]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Joey – a11n

    (@jricketts4)

    @abanista – Try using WC()->payment_gateways->payment_gateways() instead:

    
    add_action( 'woocommerce_checkout_before_terms_and_conditions', 'abanista_cod_not_available_info_notice' );
    
    function abanista_cod_not_available_info_notice() {
    
        $available_payment_methods = WC()->payment_gateways->payment_gateways();
    
        $html = "<div class='woocommerce-delivery-message' style='clear:both'>Why Pay on Delivery is Not Available: One or more of the items you’re buying or your shipping region does not support this option. Your selected item is on Backorder (to be shipped from U.K) or the option is not allowed on your account based on your past delivery experience</div>";
    
        echo $html;
    }
    

    I just tested this locally and found that it displayed as expected.

    – Joey

    Thread Starter iamlaurie

    (@abanista)

    @jricketts4 Thanks, I have 3 payment gateways including cash on delivery but cash on delivery is restricted to some users at checkout due to the contents of their cart. I need a small tweak to display the above notice only to customers who don’t find the cash on delivery as one of the available payment methods at checkout. And have the notice hidden if cash on delivery is available to the customer.

    Plugin Support Joey – a11n

    (@jricketts4)

    @abanista – Sorry about that, I forgot to check if the array returned ‘COD’. Unfortunately, that array returns all payment gateways on your site, regardless if it’s available in checkout. Once I found a workaround, however, the notice displayed twice, since something must be hooking onto woocommerce_checkout_before_terms_and_conditions more than once.

    We started from scratch and came up with this:

    add_action( 'wp_footer', 'cod_not_available_info_notice' );
    
    function cod_not_available_info_notice() {
        $available_gateways =  WC()->payment_gateways->payment_gateways() ;
        if(is_checkout()) {
            if (! $available_gateways[ 'cod' ]->is_available() ) {
              wc_add_notice( 'COD is not enabled', 'notice');
            }
        }
    }

    This will run on page load with wp_footer, but only on the checkout page (is_checkout()). This created some issues with where you were trying to display it, so it has to be added with wc_add_note(), which displays a notice at the top of the page.

    Just be sure to change ‘COD is not enabled’ to what you’d like to display in that notice box.

    – Joey

    Thread Starter iamlaurie

    (@abanista)

    @jricketts4 Thanks. I’ve applied this and it has done the trick.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Add information notice to customer when Cash on Delivery is not Available’ is closed to new replies.