wpd_ai_custom_product_cost_default_value

Alpha Insights Documentation

wpd_ai_custom_product_cost_default_value

This filter is used to set a default value for a custom product cost against a line item within a WooCommerce order. This is the per unit cost.

This value will override the default values you’ve set at a product / settings level, but will be overriden if you enter a meta value into the input on the order admin page.

This relies on custom product costs having been setup, if you would like to setup a custom product cost for WooCommerce read this how to article.

				
										/**
					 * 
					 * 	Sets the default custom cost value for a custom product cost.
					 * 	This will be overriden by the meta field input
					 * 
					 * 	@param float $default_custom_product_cost_value The default custom product cost if set by your product configuration
					 *  @param string $custom_cost_slug The custom product cost slug that we are currently looking at
					 * 	@param WC_Order $order The WC Order for this line item
					 * 	@param WC_Order_Item_Product $item The line item for this product
					 * 
					 * 	@return float $default_custom_product_cost_value The filtered custom product cost value (must return a floating integer)
					 * 
					 **/
					$default_custom_product_cost_value = (float) apply_filters( 'wpd_ai_custom_product_cost_default_value', $default_custom_product_cost_value, $custom_cost_slug, $order, $item );
				
			
Example: The example below sets a default value for the packaging costs (which is a custom product cost registered by the wpd_ai_custom_product_costs filter).
				
					/**
 *
 * 	Set the default packaging costs to be 2% of the order revenue + 1.25
 *
 * 	@param float $default_custom_product_cost_value The default custom product cost if set by your product configuration
 *  @param string $custom_cost_slug The custom product cost slug that we are currently looking at
 * 	@param WC_Order $order The WC Order for this line item
 * 	@param WC_Order_Item_Product $item The line item for this product
 *
 * 	@return float $default_custom_product_cost_value The filtered custom product cost value (must return a floating integer)
 *
 **/
add_filter( 'wpd_ai_custom_product_cost_default_value', 'wpd_set_default_packaging_cost_per_line_item', 10, 4 );
function wpd_set_default_packaging_cost_per_line_item( $default_custom_product_cost_value, $custom_cost_slug, $order, $item ) {
 
	// Only edit packaging costs
	if ( $custom_cost_slug === 'shipping_costs' ) {

		// Make sure we've got an order
		if ( ! is_a($order, 'WC_Order') ) return $default_custom_product_cost_value;

		// Order Value
		$order_value = (float) $order->get_total();

		// Update to 2% of order value
		$default_custom_product_cost_value = $order_value * .02 + 1.25;

	}

	// Per unit cost
	return $default_custom_product_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