Skip to content

Instantly share code, notes, and snippets.

@imran-khan1
Last active January 29, 2020 07:13
Show Gist options
  • Save imran-khan1/c0b33156ca2c1af0a3a077c3c8ab219a to your computer and use it in GitHub Desktop.
Save imran-khan1/c0b33156ca2c1af0a3a077c3c8ab219a to your computer and use it in GitHub Desktop.
<?php
// Display product Fields
add_action( 'woocommerce_product_options_general_product_data', 'ci_custom_general_tab_fields' );
function ci_custom_general_tab_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_text_field',
'label' => __( 'Text Field', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Enter text here.', 'woocommerce' )
)
);
// Number Field
woocommerce_wp_text_input(
array(
'id' => '_number_field',
'label' => __( 'Number Field', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Enter number here.', 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
// Textarea
woocommerce_wp_textarea_input(
array(
'id' => '_textarea',
'label' => __( 'Textarea Field', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Enter custom detail here.', 'woocommerce' )
)
);
// Select
woocommerce_wp_select(
array(
'id' => '_select',
'label' => __( 'Select Field', 'woocommerce' ),
'options' => array(
'one' => __( 'Option 1', 'woocommerce' ),
'two' => __( 'Option 2', 'woocommerce' ),
'three' => __( 'Option 3', 'woocommerce' )
)
)
);
// Checkbox
woocommerce_wp_checkbox(
array(
'id' => '_checkbox',
'wrapper_class' => 'show_if_simple',
'label' => __('Checkbox Field', 'woocommerce' ),
'description' => __( 'Check me!', 'woocommerce' )
)
);
// Hidden field
woocommerce_wp_hidden_input(
array(
'id' => '_hidden_field',
'value' => 'hidden_value'
)
);
// Custom field Type
?>
<p class="form-field custom_field_type">
<label for="custom_field_type"><?php echo __( 'Custom Field 1', 'woocommerce' ); ?></label>
<span class="wrap">
<?php $custom_field_one = get_post_meta( $post->ID, '_custom_field_one', true ); ?>
<input placeholder="<?php _e( 'Field One', 'woocommerce' ); ?>" class="" type="number" name="_field_one" value="<?php echo $custom_field_one; ?>" step="any" min="0" />
</span>
<span class="description"><?php _e( 'Place description here!', 'woocommerce' ); ?></span>
</p>
<p class="form-field custom_field_type">
<label for="custom_field_type"><?php echo __( 'Custom Field 2', 'woocommerce' ); ?></label>
<span class="wrap">
<?php $custom_field_two = get_post_meta( $post->ID, '_custom_field_two', true ); ?>
<input placeholder="<?php _e( 'Field Two', 'woocommerce' ); ?>" type="date" name="_field_two" value="<?php echo $custom_field_two; ?>" step="any" />
</span>
</p>
<!-- product select -->
<p class="form-field product_field_type">
<label for="product_field_type"><?php _e( 'Woo Product Select', 'woocommerce' ); ?></label>
<select id="product_field_type" name="product_field_type[]" class="wc-enhanced-select" multiple="multiple" data-placeholder="<?php _e( 'Search for a product&hellip;', 'woocommerce' ); ?>">
<?php
$query = new WC_Product_Query( array(
'limit' => 7,
'orderby' => 'date',
'order' => 'DESC',
'return' => 'ids',
) );
$product_ids = $query->get_products();
if ( $product_ids ) {
$i=0;
foreach ( $product_ids as $product_id ) {
$product = get_product( $product_id );
$product_name = woocommerce_get_formatted_product_name( $product );
$product_field_type_ids = get_post_meta( $post->ID, '_product_field_type_ids', true );
$prod_ids = ! empty( $product_field_type_ids ) ? array_map( 'absint', $product_field_type_ids ) : null;
if($product_id == $prod_ids[$i])
{
echo '<option value="' . esc_attr( $product_id ) . '" selected="selected">' . esc_html( $product_name ) . '</option>';
}
else
{
echo '<option value="' . esc_attr( $product_id ) . ' ">' . esc_html( $product_name ) . '</option>';
}
$i++;
}
}
?>
</select> <img class="help_tip" data-tip='<?php _e( 'Your description here', 'woocommerce' ) ?>' src="<?php echo $woocommerce->plugin_url(); ?>/assets/images/help.png" height="16" width="16" />
</p>
<?php
echo '</div>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment