wpd_ai_custom_product_cost_options
This filter is used to filter the custom product options that are defined by the settings in General Settings.
Parameter 1 is the array of current settings, these are passed in by the saved options in general settings and are not product specific.
Parameter 2 is the product_id being passed in as an integer, this can be used if you would like to only register or modify custom product costs for particular products.
/**
*
* Generate a list of custom product cost options for use by Alpha Insights
* Expects an associative array in format array( unique_slug => array(key => 'Value') ) in key value pairs.
* The available keys are: label, description, placeholder, static_fee, percent_of_sell_price
*
* @param array Empty array to begin with
* @param int $product_id The product ID of a particular product
*
* @return array The array of custom product costs that are registered by this filter
*
**/
$custom_product_costs = apply_filters( 'wpd_ai_custom_product_cost_options', $custom_product_costs, $product_id );
Example:
The example below registers three new custom product cost fields for all products, a good example of the expected array structure.
/**
*
* Add custom product cost inputs to the product page
*
* @param array The Custom product costs
* @param int The product ID to check against
*
**/
add_filter( 'wpd_ai_custom_product_cost_options', 'wpd_additional_product_cost_inputs', 10, 2 );
function wpd_additional_product_cost_inputs( $custom_product_costs, $product_id ) {
// Shipping
$custom_product_costs['shipping_costs'] = array(
'label' => 'Shipping Costs',
'description' => 'Shipping costs for each unit of this product sold.',
'placeholder' => '',
'static_fee' => 1,
'percent_of_sell_price' => 10 // Expects integer, we convert to a decimal later
);
// Modify a particular product
if ( $product_id == 36 ) $custom_product_costs['shipping_costs']['percent_of_sell_price'] = 5;
// Return results
return $custom_product_costs;
}