Simple local pickup cost

Closes #11045
This commit is contained in:
Mike Jolley 2016-06-06 14:19:58 +01:00
parent 2d2a5eb816
commit fbb73324d2
2 changed files with 22 additions and 97 deletions

View File

@ -40,8 +40,6 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
public function init() { public function init() {
$this->instance_form_fields = include( 'includes/settings-flat-rate.php' ); $this->instance_form_fields = include( 'includes/settings-flat-rate.php' );
$this->title = $this->get_option( 'title' ); $this->title = $this->get_option( 'title' );
$this->availability = $this->get_option( 'availability' );
$this->countries = $this->get_option( 'countries' );
$this->tax_status = $this->get_option( 'tax_status' ); $this->tax_status = $this->get_option( 'tax_status' );
$this->cost = $this->get_option( 'cost' ); $this->cost = $this->get_option( 'cost' );
$this->type = $this->get_option( 'type', 'class' ); $this->type = $this->get_option( 'type', 'class' );

View File

@ -41,10 +41,9 @@ class WC_Shipping_Local_Pickup extends WC_Shipping_Method {
$this->init_settings(); $this->init_settings();
// Define user set variables // Define user set variables
$this->title = $this->get_option( 'title' ); $this->title = $this->get_option( 'title' );
$this->codes = $this->get_option( 'codes' ); $this->tax_status = $this->get_option( 'tax_status' );
$this->availability = $this->get_option( 'availability' ); $this->cost = $this->get_option( 'cost' );
$this->countries = $this->get_option( 'countries' );
// Actions // Actions
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) ); add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
@ -58,6 +57,7 @@ class WC_Shipping_Local_Pickup extends WC_Shipping_Method {
$this->add_rate( array( $this->add_rate( array(
'label' => $this->title, 'label' => $this->title,
'package' => $package, 'package' => $package,
'cost' => $this->cost,
) ); ) );
} }
@ -72,98 +72,25 @@ class WC_Shipping_Local_Pickup extends WC_Shipping_Method {
'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ), 'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
'default' => __( 'Local Pickup', 'woocommerce' ), 'default' => __( 'Local Pickup', 'woocommerce' ),
'desc_tip' => true, 'desc_tip' => true,
),
'tax_status' => array(
'title' => __( 'Tax Status', 'woocommerce' ),
'type' => 'select',
'class' => 'wc-enhanced-select',
'default' => 'taxable',
'options' => array(
'taxable' => __( 'Taxable', 'woocommerce' ),
'none' => _x( 'None', 'Tax status', 'woocommerce' )
)
),
'cost' => array(
'title' => __( 'Cost', 'woocommerce' ),
'type' => 'text',
'placeholder' => '0',
'description' => __( 'Optional cost for local pickup.', 'woocommerce' ),
'default' => '',
'desc_tip' => true
) )
); );
} }
/**
* Get postcodes for this method.
* @return array
*/
public function get_valid_postcodes() {
$codes = array();
if ( $this->codes != '' ) {
foreach( explode( ',', $this->codes ) as $code ) {
$codes[] = strtoupper( trim( $code ) );
}
}
return $codes;
}
/**
* See if a given postcode matches valid postcodes.
* @param string postcode
* @param string country code
* @return boolean
*/
public function is_valid_postcode( $postcode, $country ) {
$codes = $this->get_valid_postcodes();
$postcode = $this->clean( $postcode );
$formatted_postcode = wc_format_postcode( $postcode, $country );
if ( in_array( $postcode, $codes ) || in_array( $formatted_postcode, $codes ) ) {
return true;
}
// Pattern matching
foreach ( $codes as $c ) {
$pattern = '/^' . str_replace( '_', '[0-9a-zA-Z]', preg_quote( $c ) ) . '$/i';
if ( preg_match( $pattern, $postcode ) ) {
return true;
}
}
// Wildcard search
$wildcard_postcode = $formatted_postcode . '*';
$postcode_length = strlen( $formatted_postcode );
for ( $i = 0; $i < $postcode_length; $i++ ) {
if ( in_array( $wildcard_postcode, $codes ) ) {
return true;
}
$wildcard_postcode = substr( $wildcard_postcode, 0, -2 ) . '*';
}
return false;
}
/**
* See if the method is available.
*
* @param array $package
* @return bool
*/
public function is_available( $package ) {
$is_available = true;
if ( $is_available && $this->get_valid_postcodes() ) {
$is_available = $this->is_valid_postcode( $package['destination']['postcode'], $package['destination']['country'] );
}
if ( $is_available ) {
if ( $this->availability === 'specific' ) {
$ship_to_countries = $this->countries;
} else {
$ship_to_countries = array_keys( WC()->countries->get_shipping_countries() );
}
if ( is_array( $ship_to_countries ) && ! in_array( $package['destination']['country'], $ship_to_countries ) ) {
$is_available = false;
}
}
return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package );
}
/**
* Clean code string.
*
* @access public
* @param mixed $code
* @return string
*/
public function clean( $code ) {
return str_replace( '-', '', sanitize_title( $code ) ) . ( strstr( $code, '*' ) ? '*' : '' );
}
} }