Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save imran-khan1/adf7d1f9b0ad0d0c9fd791197b853d23 to your computer and use it in GitHub Desktop.
Save imran-khan1/adf7d1f9b0ad0d0c9fd791197b853d23 to your computer and use it in GitHub Desktop.
<?php
// Admin side Variable product fields
add_action( 'woocommerce_product_after_variable_attributes', 'ci_variation_settings_fields', 10, 3 );
// 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'
)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment