Free should only be used if costs set. If left blank, offer no rate

Closes #8380
This commit is contained in:
Mike Jolley 2015-06-16 14:07:28 +01:00
parent 2347418c5b
commit 71fc6c207e
2 changed files with 22 additions and 13 deletions

View File

@ -120,10 +120,16 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
);
// Calculate the costs
$rate['cost'] = $this->evalulate_cost( $this->get_option( 'cost' ), array(
'qty' => $this->get_package_item_qty( $package ),
'cost' => $package['contents_cost']
) );
$has_costs = false; // True when a cost is set. False if all costs are blank strings.
$cost = $this->get_option( 'cost' );
if ( $cost !== '' ) {
$has_costs = true;
$rate['cost'] = $this->evalulate_cost( $cost, array(
'qty' => $this->get_package_item_qty( $package ),
'cost' => $package['contents_cost']
) );
}
// Add shipping class costs
$found_shipping_classes = $this->find_shipping_classes( $package );
@ -132,10 +138,11 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
foreach ( $found_shipping_classes as $shipping_class => $products ) {
$class_cost_string = $shipping_class ? $this->get_option( 'class_cost_' . $shipping_class, '' ) : $this->get_option( 'no_class_cost', '' );
if ( ! $class_cost_string ) {
if ( $class_cost_string === '' ) {
continue;
}
$has_costs = true;
$class_cost = $this->evalulate_cost( $class_cost_string, array(
'qty' => array_sum( wp_list_pluck( $products, 'quantity' ) ),
'cost' => array_sum( wp_list_pluck( $products, 'line_total' ) )
@ -153,7 +160,9 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
}
// Add the rate
$this->add_rate( $rate );
if ( $has_costs ) {
$this->add_rate( $rate );
}
/**
* Developers can add additional flat rates based on this one via this action since @version 2.4

View File

@ -4,7 +4,7 @@ if ( ! defined( 'ABSPATH' ) ) {
exit;
}
$cost_placeholder = __( 'Enter a cost (excl. tax) or sum, e.g. <code>10 * [qty]</code>.', 'woocommerce' ) . '<br/>' . __( 'Supports the following placeholders: <code>[qty]</code> = number of items, <code>[cost]</code> = cost of items, <code>[fee percent="10" min="20"]</code> = Percentage based fee.', 'woocommerce' );
$cost_desc = __( 'Enter a cost (excl. tax) or sum, e.g. <code>10 * [qty]</code>.', 'woocommerce' ) . '<br/>' . __( 'Supports the following placeholders: <code>[qty]</code> = number of items, <code>[cost]</code> = cost of items, <code>[fee percent="10" min="20"]</code> = Percentage based fee.', 'woocommerce' );
/**
* Settings for flat rate shipping
@ -57,8 +57,8 @@ $settings = array(
'cost' => array(
'title' => __( 'Cost', 'woocommerce' ),
'type' => 'text',
'placeholder' => wc_format_localized_price( 0 ),
'description' => $cost_placeholder,
'placeholder' => '',
'description' => $cost_desc,
'default' => '',
'desc_tip' => true
)
@ -74,8 +74,8 @@ if ( WC()->shipping->get_shipping_classes() ) {
$settings[ 'class_cost_' . $shipping_class->slug ] = array(
'title' => sprintf( __( '"%s" Shipping Class Cost', 'woocommerce' ), esc_html( $shipping_class->name ) ),
'type' => 'text',
'placeholder' => wc_format_localized_price( 0 ),
'description' => $cost_placeholder,
'placeholder' => __( 'N/A', 'woocommerce' ),
'description' => $cost_desc,
'default' => '',
'desc_tip' => true
);
@ -83,8 +83,8 @@ if ( WC()->shipping->get_shipping_classes() ) {
$settings[ 'no_class_cost' ] = array(
'title' => __( 'No Shipping Class Cost', 'woocommerce' ),
'type' => 'text',
'placeholder' => wc_format_localized_price( 0 ),
'description' => $cost_placeholder,
'placeholder' => __( 'N/A', 'woocommerce' ),
'description' => $cost_desc,
'default' => '',
'desc_tip' => true
);