Added get_rate_id() method for consistent rate nams

This commit is contained in:
Mike Jolley 2016-04-25 17:06:35 +01:00
parent c067a1675d
commit 2b1442c3f0
4 changed files with 23 additions and 4 deletions

View File

@ -207,13 +207,34 @@ abstract class WC_Shipping_Method extends WC_Settings_API {
return $this->rates;
}
/**
* Returns a rate ID based on this methods ID and instance, with an optional
* suffix if distinguishing between multiple rates.
* @since 2.6.0
* @param string $suffix
* @return string
*/
public function get_rate_id( $suffix = '' ) {
$rate_id = array( $this->id );
if ( $this->instance_id ) {
$rate_id[] = $this->instance_id;
}
if ( $suffix ) {
$rate_id[] = $suffix;
}
return implode( ':', $rate_id );
}
/**
* Add a shipping rate. If taxes are not set they will be calculated based on cost.
* @param array $args (default: array())
*/
public function add_rate( $args = array() ) {
$args = wp_parse_args( $args, array(
'id' => '', // ID for the rate
'id' => $this->get_rate_id(), // ID for the rate. If not passed, this id:instance default will be used.
'label' => '', // Label for the rate
'cost' => '0', // Amount or array of costs (per item shipping)
'taxes' => '', // Pass taxes, or leave empty to have it calculated for you, or 'false' to disable calculations

View File

@ -123,7 +123,7 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
*/
public function calculate_shipping( $package = array() ) {
$rate = array(
'id' => $this->id . $this->instance_id,
'id' => $this->get_rate_id(),
'label' => $this->title,
'cost' => 0,
'package' => $package,

View File

@ -146,7 +146,6 @@ class WC_Shipping_Free_Shipping extends WC_Shipping_Method {
*/
public function calculate_shipping( $package = array() ) {
$this->add_rate( array(
'id' => $this->id . $this->instance_id,
'label' => $this->title,
'cost' => 0,
'taxes' => false,

View File

@ -56,7 +56,6 @@ class WC_Shipping_Local_Pickup extends WC_Shipping_Method {
*/
public function calculate_shipping( $package = array() ) {
$this->add_rate( array(
'id' => $this->id . $this->instance_id,
'label' => $this->title,
'package' => $package,
) );