WC_Shipping_Legacy_Flat_Rate::get_extra_cost
Deprecated since 2.4.0. It is no longer supported and may be removed in future releases. It is recommended to replace this function with the same one.
Get extra cost.
Method of the class: WC_Shipping_Legacy_Flat_Rate{}
No Hooks.
Returns
float.
Usage
$WC_Shipping_Legacy_Flat_Rate = new WC_Shipping_Legacy_Flat_Rate(); $WC_Shipping_Legacy_Flat_Rate->get_extra_cost( $cost_string, $type, $package );
- $cost_string(string) (required)
- Cost string.
- $type(string) (required)
- Type.
- $package(array) (required)
- Package information.
Changelog
| Deprecated since | 2.4.0 |
WC_Shipping_Legacy_Flat_Rate::get_extra_cost() WC Shipping Legacy Flat Rate::get extra cost code WC 10.7.0
public function get_extra_cost( $cost_string, $type, $package ) {
$cost = $cost_string;
$cost_percent = false;
// @codingStandardsIgnoreStart
$pattern =
'/' . // Start regex.
'(\d+\.?\d*)' . // Capture digits, optionally capture a `.` and more digits.
'\s*' . // Match whitespace.
'(\+|-)' . // Capture the operand.
'\s*' . // Match whitespace.
'(\d+\.?\d*)' . // Capture digits, optionally capture a `.` and more digits.
'\%/'; // Match the percent sign & end regex.
// @codingStandardsIgnoreEnd
if ( preg_match( $pattern, $cost_string, $this_cost_matches ) ) {
$cost_operator = $this_cost_matches[2];
$cost_percent = $this_cost_matches[3] / 100;
$cost = $this_cost_matches[1];
}
switch ( $type ) {
case 'class':
$cost = $cost * count( $this->find_shipping_classes( $package ) );
break;
case 'item':
$cost = $cost * $this->get_package_item_qty( $package );
break;
}
if ( $cost_percent ) {
switch ( $type ) {
case 'class':
$shipping_classes = $this->find_shipping_classes( $package );
foreach ( $shipping_classes as $shipping_class => $items ) {
foreach ( $items as $item_id => $values ) {
$cost = $this->calc_percentage_adjustment( $cost, $cost_percent, $cost_operator, $values['line_total'] );
}
}
break;
case 'item':
foreach ( $package['contents'] as $item_id => $values ) {
if ( $values['data']->needs_shipping() ) {
$cost = $this->calc_percentage_adjustment( $cost, $cost_percent, $cost_operator, $values['line_total'] );
}
}
break;
case 'order':
$cost = $this->calc_percentage_adjustment( $cost, $cost_percent, $cost_operator, $package['contents_cost'] );
break;
}
}
return $cost;
}