-
-
Save rituhritika90/0483d089bd7781f1ec90 to your computer and use it in GitHub Desktop.
WooCommerce Product Vendors: Add extra custom fields to vendor profiles
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 | |
// Add fields to new vendor form | |
add_action( 'shop_vendor_add_form_fields', 'custom_add_vendor_fields', 2, 1 ); | |
function custom_add_vendor_fields( $taxonomy ) { | |
?> | |
<div class="form-field"> | |
<label for="vendor_website"><?php _e( 'Vendor website' ); ?></label> | |
<input type="text" name="vendor_data[website]" id="vendor_website" class="vendor_fields" /><br/> | |
<span class="description"><?php _e( 'The vendor\'s website.' ); ?></span> | |
</div> | |
<?php | |
} | |
// Add fields to vendor edit form for admins to edit | |
add_action( 'shop_vendor_edit_form_fields', 'custom_edit_vendor_fields', 2, 1 ); | |
function custom_edit_vendor_fields( $vendor ) { | |
$vendor_id = $vendor->term_id; | |
$vendor_data = get_option( 'shop_vendor_' . $vendor_id ); | |
$vendor_website = ''; | |
if( isset( $vendor_data['website'] ) && ( strlen( $vendor_data['website'] ) > 0 || $vendor_data['website'] != '' ) ) { | |
$vendor_website = $vendor_data['website']; | |
} | |
?> | |
<tr class="form-field"> | |
<th scope="row" valign="top"><label for="vendor_website"><?php _e( 'Vendor website' ); ?></label></th> | |
<td> | |
<input type="text" name="vendor_data[website]" id="vendor_website" class="vendor_fields" /><br/> | |
<span class="description"><?php _e( 'The vendor\'s website' ); ?></span> | |
</td> | |
</tr> | |
<?php | |
} | |
// Add fields to vendor details form for vendors to edit | |
add_action( 'product_vendors_details_fields', 'custom_vendor_details_fields', 10, 1 ); | |
function custom_vendor_details_fields( $vendor_id ) { | |
$vendor = get_user_vendor(); | |
$vendor_data = get_option( 'shop_vendor_' . $vendor->ID ); | |
$vendor_info = get_vendor( $vendor->ID ); | |
$vendor_website = ''; | |
if( isset( $vendor_data['website'] ) && ( strlen( $vendor_data['website'] ) > 0 || $vendor_data['website'] != '' ) ) { | |
$vendor_website = $vendor_data['website']; | |
} | |
$html = '<p class="form-field"> | |
<label for="vendor_website">' . __( 'Website' ) . ':</label> | |
<input type="text" name="wc_product_vendors_website_' . $vendor->ID . '" id="vendor_website" class="vendor_fields" /> | |
</p>'; | |
echo $html; | |
} | |
// Save fields from vendor details form | |
add_action( 'product_vendors_details_fields_save', 'custom_vendor_details_fields_save', 10, 2 ); | |
function custom_vendor_details_fields_save( $vendor_id, $posted ) { | |
$vendor_data = get_option( 'shop_vendor_' . $vendor_id ); | |
if( isset( $posted[ 'wc_product_vendors_website_' . $vendor_id ] ) ) { | |
$vendor_data['website'] = $posted[ 'wc_product_vendors_website_' . $vendor_id ]; | |
} | |
update_option( 'shop_vendor_' . $vendor_id, $vendor_data ); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment