Skip to content

Instantly share code, notes, and snippets.

@plugin-republic
Forked from igorbenic/checkbox_input.php
Last active August 23, 2018 14:26
Show Gist options
  • Save plugin-republic/b164b50060ac78e231346e1dc941c47b to your computer and use it in GitHub Desktop.
Save plugin-republic/b164b50060ac78e231346e1dc941c47b to your computer and use it in GitHub Desktop.
How To Add Woocommerce Custom Product Fields | http://www.ibenic.com/how-to-add-woocommerce-custom-product-fields
<?php
$args = array(
'label' => '', // Text in Label
'class' => '',
'style' => '',
'wrapper_class' => '',
'value' => '', // if empty, retrieved from post meta where id is the meta_key
'id' => '', // required
'name' => '', //name will set from id if empty
'cbvalue' => '',
'desc_tip' => '',
'custom_attributes' => '', // array of attributes
'description' => ''
);
woocommerce_wp_checkbox( $args );
<?php
add_action( 'woocommerce_product_options_pricing', 'my_woo_custom_price_field' );
/**
* Add a Christmas Price field
**/
function my_woo_custom_price_field() {
$field = array(
'id' => 'christmas_price',
'label' => __( 'Christmas Price', 'textdomain' ),
'data_type' => 'price' //Let WooCommerce formats our field as price field
);
woocommerce_wp_text_input( $field );
}
add_action( 'woocommerce_product_options_general_product_data', 'my_woo_custom_fields' );
/**
* Add a select Field at the bottom
*/
function my_woo_custom_fields() {
$select_field = array(
'id' => 'my_select',
'label' => __( 'Custom Select', 'textdomain' ),
'options' => array(
'value1' => __( 'Text 1', 'textdomain' ),
'value2' => __( 'Text 2', 'textdomain' )
)
);
woocommerce_wp_select( $select_field );
}
<?php
// When you know the value of $product_id
function get_custom_field_value( $product_id ) {
$product = wc_get_product( $product_id );
$value = $product->get_meta( 'my_custom_input' );
return $value;
}
<?php
$args = array(
'value' => '',
'class' => '',
'id' => ''
);
woocommerce_wp_hidden_input( $args );
<?php
$args = array(
'label' => '', // Text in Label
'class' => '',
'style' => '',
'wrapper_class' => '',
'value' => '', // if empty, retrieved from post meta where id is the meta_key
'id' => '', // required
'name' => '', //name will set from id if empty
'options' => '', // Options for radio inputs, array
'desc_tip' => '',
'description' => ''
);
woocommerce_wp_radio( $args );
<?php
add_action( 'woocommerce_process_product_meta', 'save_custom_field' );
function save_custom_field( $post_id ) {
$custom_field_value = isset( $_POST['my_custom_input'] ) ? $_POST['my_custom_input'] : '';
$product = wc_get_product( $post_id );
$product->update_meta_data( 'my_custom_input', $custom_field_value );
$product->save();
}
<?php
$args = array(
'label' => '', // Text in Label
'class' => '',
'style' => '',
'wrapper_class' => '',
'value' => '', // if empty, retrieved from post meta where id is the meta_key
'id' => '', // required
'name' => '', //name will set from id if empty
'options' => '', // Options for select, array
'desc_tip' => '',
'custom_attributes' => '', // array of attributes
'description' => ''
);
woocommerce_wp_select( $args );
<?php
add_action( 'woocommerce_product_write_panel_tabs', 'my_custom_tab_action' );
/**
* Adding a custom tab
*/
function my_custom_tab_action() {
?>
<li class="custom_tab">
<a href="#the_custom_panel">
<span><?php _e( 'My Custom Tab', 'textdomain' ); ?></span>
</a>
</li>
<?php
}
<?php
add_filter( 'woocommerce_product_data_tabs', 'my_custom_tab' );
/**
* Adding a custom tab
*/
function my_custom_tab( $tabs ) {
$tabs['custom_tab'] = array(
'label' => __( 'My Custom Tab', 'textdomain' ),
'target' => 'the_custom_panel',
'class' => array(),
);
return $tabs;
}
<?php
add_action( 'woocommerce_product_data_panels', 'custom_tab_panel' );
function custom_tab_panel() {
?>
<div id="the_custom_panel" class="panel woocommerce_options_panel">
<div class="options_group">
<?php
$field = array(
'id' => 'my_custom_input',
'label' => __( 'Custom Input', 'textdomain' ),
);
woocommerce_wp_text_input( $field );
?>
</div>
</div>
<?php
}
<?php
$args = array(
'label' => '', // Text in Label
'placeholder' => '',
'class' => '',
'style' => '',
'wrapper_class' => '',
'value' => '', // if empty, retrieved from post meta where id is the meta_key
'id' => '', // required
'name' => '', //name will set from id if empty
'type' => '',
'desc_tip' => '',
'data_type' => '',
'custom_attributes' => '', // array of attributes
'description' => ''
);
woocommerce_wp_text_input( $args );
<?php
$args = array(
'label' => '', // Text in Label
'placeholder' => '',
'class' => '',
'style' => '',
'wrapper_class' => '',
'value' => '', // if empty, retrieved from post meta where id is the meta_key
'id' => '', // required
'name' => '', //name will set from id if empty
'rows' => '',
'cols' => '',
'desc_tip' => '',
'custom_attributes' => '', // array of attributes
'description' => ''
);
woocommerce_wp_textarea_input( $args );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment