Created
April 17, 2020 08:18
-
-
Save imran-khan1/ac6f0e4ef92b17d636479ebb443121c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Admin side Variable product fields | |
add_action( 'woocommerce_product_after_variable_attributes', 'ci_variation_settings_fields', 10, 3 ); | |
// Save Variation Settings | |
add_action( 'woocommerce_save_product_variation', 'ci_save_variation_settings_fields', 10, 2 ); | |
// Create new fields for variations | |
function ci_variation_settings_fields( $loop, $variation_data, $variation ) { | |
// Text Field | |
woocommerce_wp_text_input( | |
array( | |
'id' => 'ci_text_field[' . $variation->ID . ']', | |
'label' => __( 'My Text Field', 'woocommerce' ), | |
'placeholder' => 'Enter text field value', | |
'desc_tip' => 'true', | |
'description' => __( 'Enter the text field value here.', 'woocommerce' ), | |
'value' => get_post_meta( $variation->ID, 'ci_text_field', true ) | |
) | |
); | |
// Number Field | |
woocommerce_wp_text_input( | |
array( | |
'id' => 'ci_number_field[' . $variation->ID . ']', | |
'label' => __( 'My Number Field', 'woocommerce' ), | |
'desc_tip' => 'true', | |
'description' => __( 'Enter the custom number here.', 'woocommerce' ), | |
'value' => get_post_meta( $variation->ID, 'ci_number_field', true ), | |
'custom_attributes' => array( | |
'step' => 'any', | |
'min' => '0' | |
) | |
) | |
); | |
// Textarea | |
woocommerce_wp_textarea_input( | |
array( | |
'id' => 'ci_textarea[' . $variation->ID . ']', | |
'label' => __( 'My Textarea', 'woocommerce' ), | |
'placeholder' => '', | |
'description' => __( 'Enter the custom value here.', 'woocommerce' ), | |
'value' => get_post_meta( $variation->ID, 'ci_textarea', true ), | |
) | |
); | |
// Select | |
woocommerce_wp_select( | |
array( | |
'id' => 'ci_select[' . $variation->ID . ']', | |
'label' => __( 'My Select Field', 'woocommerce' ), | |
'description' => __( 'Choose a value.', 'woocommerce' ), | |
'value' => get_post_meta( $variation->ID, 'ci_select', true ), | |
'options' => array( | |
'one' => __( 'CI Option 1', 'woocommerce' ), | |
'two' => __( 'CI Option 2', 'woocommerce' ), | |
'three' => __( 'CI Option 3', 'woocommerce' ) | |
) | |
) | |
); | |
// Checkbox | |
woocommerce_wp_checkbox( | |
array( | |
'id' => 'ci_checkbox[' . $variation->ID . ']', | |
'label' => __('My Checkbox Field', 'woocommerce' ), | |
'description' => __( 'Check me!', 'woocommerce' ), | |
'value' => get_post_meta( $variation->ID, 'ci_checkbox', true ), | |
) | |
); | |
// Hidden field | |
woocommerce_wp_hidden_input( | |
array( | |
'id' => 'ci_hidden_field[' . $variation->ID . ']', | |
'value' => 'hidden_value' | |
) | |
); | |
} | |
// Save new fields for variations | |
function ci_save_variation_settings_fields( $post_id ) { | |
// Text Field | |
$text_field = $_POST['ci_text_field'][ $post_id ]; | |
if( ! empty( $text_field ) ) { | |
update_post_meta( $post_id, 'ci_text_field', esc_attr( $text_field ) ); | |
} | |
// Number Field | |
$number_field = $_POST['ci_number_field'][ $post_id ]; | |
if( ! empty( $number_field ) ) { | |
update_post_meta( $post_id, 'ci_number_field', esc_attr( $number_field ) ); | |
} | |
// Textarea | |
$textarea = $_POST['ci_textarea'][ $post_id ]; | |
if( ! empty( $textarea ) ) { | |
update_post_meta( $post_id, 'ci_textarea', esc_attr( $textarea ) ); | |
} | |
// Select | |
$select = $_POST['ci_select'][ $post_id ]; | |
if( ! empty( $select ) ) { | |
update_post_meta( $post_id, 'ci_select', esc_attr( $select ) ); | |
} | |
// Checkbox | |
$checkbox = isset( $_POST['ci_checkbox'][ $post_id ] ) ? 'yes' : 'no'; | |
update_post_meta( $post_id, 'ci_checkbox', $checkbox ); | |
// Hidden field | |
$hidden = $_POST['ci_hidden_field'][ $post_id ]; | |
if( ! empty( $hidden ) ) { | |
update_post_meta( $post_id, 'ci_hidden_field', esc_attr( $hidden ) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment