wpd_ai_custom_order_cost_default_value

Alpha Insights Documentation

wpd_ai_custom_order_cost_default_value

This filter is used to set a default cost for your custom order costs.

This filter works with the custom order costs that you can enable within Alpha Insights.

If you do not have a custom order cost, or do not know what it is, you can read the documentation here to setup custom order costs.

This filter will allow you to set a default cost for that custom order cost without having to manually enter it into each order.

If you are not sure what the unique slug is for your custom order cost, you can find this in Alpha Insights > General Settings > Custom Order & Product Costs labelled as the “unique key” underneath your cost name.

This filter will override the default settings if you require more complex calculations, however it will still be overriden by the meta value saved in cost overrides on each order, if you choose to manually override for an order.

Note: Filtering this value will require a cache refresh after you have made your change.

				
					            /**
             * 
             * 	Allow filtering of the custom order cost value, takes preference over the default settings but is overriden by the stored meta value.
             * 
             * 	@param float The current custom order cost value assigned to this order
             * 	@param string The unique slug for this custom order cost
             *  @param WC_Order The order object
             * 
             * 	@return float The updated payment gateway cost
             * 
             **/
            $custom_cost_value = (float) apply_filters( 'wpd_ai_custom_order_cost_default_value', $custom_cost_value, $cost_slug, $this->order );
				
			

Example:

This example snippet is used (which is most common amongst our customers) to assign a default commission cost, using the custom order cost “commission”.

*This custom order cost is not enabled in Alpha Insights by default, you will need to setup custom order cost(s) to interact with this filter.

				
					/**
 * 
 * 	Setup our custom cost (comission) to be 2% of order revenue by default
 * 
 **/
add_filter( 'wpd_ai_custom_order_cost_default_value', 'wpd_assign_custom_order_cost_automatically', 10, 3 );
function wpd_assign_custom_order_cost_automatically( $custom_cost_value, $cost_slug, $order ) {

	// Safety Check
	if ( ! is_a($order, 'WC_Order') ) return $custom_cost_value;

	// Check which custom cost we are looking at
	if ( $cost_slug == 'commission' ) {

		// Get order total
		$order_revenue = (float) $order->get_total();

		// Commission is 2% of order revenue
		$custom_cost_value = $order_revenue * 0.02;

	}

	// Return results
	return $custom_cost_value;

}
				
			

Got A Question?

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
blank
Alpha Insights

Alpha Insights

The World's Most Advanced WooCommerce Reporting Plugin.

WooCommerce Cost Of Goods Order Profit Reports