woocommerce / projects

38 stars 24 forks source link

Custom fields for product data #51

Closed helgatheviking closed 10 years ago

helgatheviking commented 10 years ago

Allow custom fields to be built out by users. A sample select type could then be created via:

function gb_custom_fields( $fields ){
    $fields['select'] = array(
            'name'          => __( 'Select', 'projects' ),
            'description'   => __( 'Test a dropdown', 'projects' ),
            'type'          => 'select',
            'default'       => 'b',
            'section'       => 'info',
            'options'       => array( 'a' => 'Apple', 'b' => 'Bacon' )
        );
    return $fields;
}
add_filter( 'projects_custom_fields', 'gb_custom_fields' );`

function gb_select_field( $html, $k, $data, $v ){
    $html = '<select name="' . esc_attr( $k ) . '" id="' . esc_attr( $k ) . '" >'. "\n";

    if( isset( $v['options'] ) && is_array( $v['options'] ) ){
        foreach ( $v['options'] as $val => $option ){
            $html .= '<option value="' . esc_attr( $val ) . '" ' . selected( $val, $data, false ) . '>'. $option .'</option>' . "\n";
        }
    }
    $html .= '</select>'. "\n";
    return $html;
}
add_filter( 'projects_data_field_type_select', 'gb_select_field', 10, 4 );
jameskoster commented 10 years ago

Thanks for this. I think I'd rather include the select case in core though. Makes it a bit easier for folks extending. Wanna add that?

FYI your snippet is missing the opening <select> :p

helgatheviking commented 10 years ago

Sure. I didn't know how many types you wanted to support in core.

jameskoster commented 10 years ago

I think we should include common cases, so select, checkbox, radio etc.

mattyza commented 10 years ago

@jameskoster - There is a bunch of that in our settings API class and in the new WooFramework development version (in the WF_Fields class) which you could grab and modify. :)

Matt Cohen Chief Product Officer at WooThemes

http://woothemes.com/ http://matty.co.za/

From: James Koster notifications@github.com Reply: woothemes/projects reply@reply.github.com Date: 25 March 2014 at 2:17:01 PM To: woothemes/projects projects@noreply.github.com Subject:  Re: [projects] Custom fields for product data (#51)

I think we should include common cases, so select, checkbox, radio etc.

— Reply to this email directly or view it on GitHub.

jameskoster commented 10 years ago

@mattyza that's only useful if you're using a wootheme though..?

mattyza commented 10 years ago

I meant that you’d grab the code out of there to construct the fields you need. :)

Matt Cohen Chief Product Officer at WooThemes

http://woothemes.com/ http://matty.co.za/

From: James Koster notifications@github.com Reply: woothemes/projects reply@reply.github.com Date: 25 March 2014 at 2:24:04 PM To: woothemes/projects projects@noreply.github.com Cc: Matty Cohen matt@woothemes.com Subject:  Re: [projects] Custom fields for product data (#51)

@mattyza that's only useful if you're using a wootheme though..?

— Reply to this email directly or view it on GitHub.

helgatheviking commented 10 years ago

New commit has select and radio. I'll let you guys do checkboxes as I think that might require some tweaking to the save routine.

jameskoster commented 10 years ago

ty!