0
add_action('woocommerce_after_checkout_validation', 'deny_pobox_postcode');

function deny_pobox_postcode( $posted ) {
 global $woocommerce;

 $address  = ( isset( $posted['shipping_address_1'] ) ) ?          $posted['shipping_address_1'] : $posted['billing_address_1'];
 $postcode = ( isset( $posted['shipping_postcode'] ) ) ?    $posted['shipping_postcode'] : $posted['billing_postcode'];

$replace  = array(" ", ".", ",");
$address  = strtolower( str_replace( $replace, '', $address ) );
$postcode = strtolower( str_replace( $replace, '', $postcode ) );

if ( strstr( $address, 'pobox' ) || strstr( $postcode, 'pobox' ) ) {
  $woocommerce->add_error( "Sorry, we don't ship to PO BOX addresses." );
 }
}

i am getting:

fatal error :call to undefined add_error

when i am pasting on my function .php

3 Answers 3

1

add_error() has been renamed to wc_add_notice():

$woocommerce->wc_add_notice( "Sorry, we don't ship to PO BOX addresses." );
1
  • still i am getting Fatal error: Call to undefined method WooCommerce::wc_add_notice() May 20, 2015 at 5:19
0
 add_action('woocommerce_after_checkout_validation', 'deny_pobox_postcode');

function deny_pobox_postcode( $posted ) {
 global $woocommerce;

  $address  = ( isset( $posted['shipping_address_1'] ) ) ?     
 $posted['shipping_address_1'] : $posted['billing_address_1'];

 $postcode = ( isset( $posted['shipping_postcode'] ) ) ?  
 $posted['shipping_postcode'] : $posted['billing_postcode'];

 $replace  = array(" ", ".", ",");
 $address  = strtolower( str_replace( $replace, '', $address ) );
 $postcode = strtolower( str_replace( $replace, '', $postcode ) );

 if ( strstr( $address, 'pobox' ) || strstr( $postcode, 'pobox' ) ) {
   $notice = sprintf( __( '%1$sSorry, we dont ship to PO BOX addresses..' , 'error' ) , '<strong>' , '</strong>' );

        if ( version_compare( WC_VERSION, '2.3', '<' ) ) {
            $woocommerce->add_error( $notice );
        } else {
            wc_add_notice( $notice, 'error' );
        }
  }
}
0
<?php
add_filter('woocommerce_package_rates', 
'shomaris_hide_fedex_for_po_box_shipment', 
10, 2);    
function shomaris_hide_fedex_for_po_box_shipment($available_shipping_methods, 
$package){
$shipping_method_to_hide = 'flat_rate:4';
//  $shipping_method_to_hides = 'flat_rate:5';
global $woocommerce;
$address  = ( !empty( $woocommerce->customer->get_shipping_address_1() ) ) ? 
$woocommerce->customer->get_shipping_address_1() : $woocommerce->customer- 
>get_billing_address_1();
$postcode = ( !empty( $woocommerce->customer->get_shipping_postcode() ) ) ? 
$woocommerce->customer->get_shipping_postcode() : $woocommerce->customer- 
>get_billing_postcode();
$replace  = array(" ", ".", ",");
$address2  = strtolower( str_replace( $replace, '', $address ) );
if ( strstr( $address2, 'pobox' ) ) {
    foreach ($available_shipping_methods as $shipping_method => $value) {
        if( strpos( $shipping_method, $shipping_method_to_hide, 
$shipping_method_to_hides ) !== false ) {
            unset($available_shipping_methods[$shipping_method]);
        }
    }
}
return $available_shipping_methods;
}
?>
1
  • 1
    This will validate the address, within the input field, BEFORE hitting submit and dynamically hide the flat-rate options (or any shipping method of your choice) of your choice. MUCH better than validating AFTER hitting submit and waiting for an error! Mar 25, 2021 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.