wpd_ai_order_shipping_cost_default_value
This filter is used to alter the shipping cost of an order.
Parameter 1 is the total shipping cost, which is a floating (decimal) number, your filter must return a decimal number. The WC_Order object is also passed as an additional argument.
/**
*
* Filters the default shipping cost of an order (this is an expense, not the amount charged to the customer)
*
* @param float The current shipping cost assigned to this order
* @param WC_Order The order object
*
* @return float The updated shipping cost
*
**/
$shipping_cost = (float) apply_filters( 'wpd_ai_order_shipping_cost_default_value', $shipping_cost, $this->order );
Example:
The example below would filter all orders going to France to set the shipping cost (expense) to $100 in your store’s currency.
Note: Filtering this value will require a cache refresh after you have made your change.
/**
*
* Set the shipping cost to $100 for orders going to France
*
**/
add_filter( 'wpd_ai_order_shipping_cost_default_value', 'wpd_override_shipping_costs', 10, 2 );
function wpd_override_shipping_costs( $shipping_cost, $order ) {
// Safety Check
if ( ! is_a($order, 'WC_Order') ) return $shipping_cost;
// Set a shipping expense for orders shipped to frnace
if ( $order->get_shipping_country() == 'FR' ) $shipping_cost = 100;
// Return the value
return $shipping_cost;
}
This filter will override the default settings found in Alpha Insights > General Settings.
However, if you set a value from the order edit page for a particular order, this saved value will take precedence over this filtered value.
Review the shipping cost calculation method below for how the logic is applied.
/**
*
* Calculate shipping costs for order
*
**/
private function calculate_shipping_costs() {
// Default Options
$shipping_cost_multiplier_order_revenue = (float) wpd_divide( $this->cost_defaults['default_shipping_cost_percent'], 100 );
$shipping_cost_multiplier_shipping_charged = (float) wpd_divide( $this->cost_defaults['default_shipping_cost_percent_shipping_charged'], 100 );
$shipping_cost_fee = (float) $this->cost_defaults['default_shipping_cost_fee'];
// Calculate Default Value
$order_revenue_multiplier = $this->results['total_order_revenue'] * $shipping_cost_multiplier_order_revenue;
$shipping_charge_multiplier = $this->results['total_shipping_charged'] * $shipping_cost_multiplier_shipping_charged;
$shipping_cost = $order_revenue_multiplier + $shipping_charge_multiplier + $shipping_cost_fee;
/**
*
* Filters the default shipping cost of an order (this is an expense, not the amount charged to the customer)
*
* @param float The current shipping cost assigned to this order
* @param WC_Order The order object
*
* @return float The updated shipping cost
*
**/
$shipping_cost = (float) apply_filters( 'wpd_ai_order_shipping_cost_default_value', $shipping_cost, $this->order );
// Use saved value if set
$meta_shipping_cost = $this->order->get_meta( '_wpd_ai_total_shipping_cost' );
if ( is_numeric($meta_shipping_cost) ) $shipping_cost = $meta_shipping_cost;
// Store in main var
$this->results['total_shipping_cost'] = (float) $shipping_cost;
}