不允许PO BOX运输woocommerce无法正常工作

时间:2015-05-19 12:15:18

标签: wordpress woocommerce

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." );
 }
}

我得到了:

  

致命错误:调用undefined add_error

当我粘贴我的函数.php

3 个答案:

答案 0 :(得分:1)

add_error()已重命名为wc_add_notice()

$woocommerce->wc_add_notice( "Sorry, we don't ship to PO BOX addresses." );

答案 1 :(得分: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' );
        }
  }
}

答案 2 :(得分: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;
}
?>