Skip to content

Instantly share code, notes, and snippets.

@plugin-republic
Last active February 11, 2020 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plugin-republic/42d6d5b6e644ff3b1c162289bf75689e to your computer and use it in GitHub Desktop.
Save plugin-republic/42d6d5b6e644ff3b1c162289bf75689e to your computer and use it in GitHub Desktop.
<?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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment