Best Jquery ,javascript, HTML5 CSS3 tutorial with jquery examples, php script , demos for beginners, web designers and web developers.

Jquery

Jquery, javascript Training

Jquery training blog

Add custom field in WooCommerce product vendor

Add custom field in WooCommerce product  vendor. WooCommerce product  vendor plugin registration form has predefined fields, but sometimes we need custom field to add in  WooCommerce product vendor registration form we need to write the following code in function.php file in child theme.

We can write the following code to add custom field in WooCommerce product vendor


add_action( 'init', 'register_vendor_custom_fields' );
function register_vendor_custom_fields() {
add_action( WC_PRODUCT_VENDORS_TAXONOMY . '_add_form_fields', 'add_vendor_custom_fields' );
add_action( WC_PRODUCT_VENDORS_TAXONOMY . '_edit_form_fields', 'edit_vendor_custom_fields', 10 );
add_action( 'edited_' . WC_PRODUCT_VENDORS_TAXONOMY, 'save_vendor_custom_fields' );
add_action( 'created_' . WC_PRODUCT_VENDORS_TAXONOMY, 'save_vendor_custom_fields' );
}

Add Field
function add_vendor_registration_fields() {

wp_nonce_field( basename( __FILE__ ), ‘vendor_custom_fields_nonce’ );
?>

<div class=”form-field”>
<label for=”fieldname”><?php _e( ‘Field Name’); ?></label>
<input type=”text” name=”fieldname” id=”fieldname” value=”” />
</div>
<?php
}

Edit Field

function edit_vendor_custom_fields( $term ) {
wp_nonce_field( basename( __FILE__ ), ‘vendor_custom_fields_nonce’ );
?>
<tr class=”form-field”>
<th scope=”row” valign=”top”><label for=”fieldname”><?php _e( ‘Field Name’); ?></label></th>
<td>

<input type=”text” name=”fieldname” id=”fieldname” value=”<?php echo esc_url( get_term_meta( $term->term_id, ‘fieldname’, true ) ); ?>” />
</td>
</tr>

<?php
}
Save term fields

function save_vendor_custom_fields( $term_id ) {
if ( ! wp_verify_nonce( $_POST[‘vendor_custom_fields_nonce’], basename( __FILE__ ) ) ) {
return;
}
$old_fb = get_term_meta( $term_id, ‘fieldname’, true );

$new_fb = esc_url( $_POST[‘fieldname’] );

if ( ! empty( $old_fb ) && $new_fb === ” ) {
delete_term_meta( $term_id, ‘facebook’ );
} else if ( $old_fb !== $new_fb ) {
update_term_meta( $term_id, ‘facebook’, $new_fb, $old_fb );
}
}

add_action( ‘wcpv_registration_form’, ‘vendors_reg_custom_fields’ );
function vendors_reg_custom_fields() {
?>
<p class=”form-row form-row-first”>
<label for=”wcpv-fieldname”><?php esc_html_e( ‘fieldname’ ); ?></label>
<input type=”text” class=”input-text” name=”fieldname” id=”wcpv-fieldname” value=”<?php if ( ! empty( $_POST[‘fieldname’] ) ) echo esc_attr( trim( $_POST[‘fieldname’] ) ); ?>” />
</p>

<?php
}
add_action( ‘wcpv_shortcode_registration_form_process’, ‘vendors_reg_custom_fields_save’, 10, 2 );
function vendors_reg_custom_fields_save( $args, $items ) {
$term = get_term_by( ‘name’, $items[‘vendor_name’], WC_PRODUCT_VENDORS_TAXONOMY );
if ( isset( $items[‘fieldname’] ) && ! empty( $items[‘fieldname’] ) ) {
$fb = esc_url( $items[‘fieldname’] );
update_term_meta( $term->term_id, ‘fieldname’, $fb );
}

}

Please follow and like us:





Got Something To Say:

Your email address will not be published. Required fields are marked *

*


About the author

Joydeb Choudhury holds a Master's degree in Computer Application from I.G.N.O.U. After working as a web developer in various companies he has started bloging in 2016.

Recent Posts of blog

Downalod Free Php Scripts