1. Home
  2. Knowledge Base
  3. WooCommerce Product Add-Ons Ultimate
  4. Advanced
  5. Add custom parameters to add-on fields
  1. Home
  2. Knowledge Base
  3. WooCommerce Product Add-Ons Ultimate
  4. Add custom parameters to add-on fields

Add custom parameters to add-on fields

You can add a custom parameter to your add-on fields, e.g. for SKU, by using the following snippet:

<?php
/**
* Add a custom parameter to add-on field settings
*/
function prefix_add_param( $group_key, $item_key, $item, $post_id ) {
echo '<div class="pewc-fields-wrapper pewc-my-new-fields">';
echo '<div>';
$your_param_name = isset( $item['your_param_name'] ) ? $item['your_param_name'] : '';
echo '<label>Param Name</label>';
echo '<input type="text" class="pewc-field-your_param_name" name="_product_extra_groups_' .esc_attr( $group_key ) . '_' . esc_attr( $item_key ) . '[your_param_name]" value="'. esc_html( $your_param_name ).'">';
echo '</div>';
echo '</div>';
}
add_action( 'pewc_end_product_extra_field', 'prefix_add_param', 10, 4 );
/**
* Add the custom param to field data
*/
function prefix_pewc_item_params( $params, $field_id ) {
$params[] = 'your_param_name';
return $params;
}
add_filter( 'pewc_item_params', 'prefix_pewc_item_params', 10, 2 );
/**
* Add the new params to the cart item data
*/
function prefix_end_add_cart_item_data( $cart_item_data, $item, $group_id, $field_id, $value ) {
if( ! empty( $item['sku'] ) ) {
$cart_item_data['product_extras']['groups'][$group_id][$field_id]['sku'] = $item['sku'];
}
return $cart_item_data;
}
add_filter( 'pewc_filter_end_add_cart_item_data', 'prefix_end_add_cart_item_data', 10, 5 );
function prefix_itemmeta_admin_item( $list_item, $field, $price ) {
if( ! empty( $field['sku'] ) ) {
$list_item .= '<br>' . $field['sku'];
}
return $list_item;
}
add_filter( 'pewc_itemmeta_admin_item', 'prefix_itemmeta_admin_item', 10, 3 );

Note that you’ll need to update the code, changing your_param_name to the name of your custom parameter.

You can see how to add a custom snippet to your code here.

Was this article helpful?

Related Articles