Skip to content

Instantly share code, notes, and snippets.

@hlashbrooke
Last active April 6, 2022 16:49
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save hlashbrooke/11205023 to your computer and use it in GitHub Desktop.
Save hlashbrooke/11205023 to your computer and use it in GitHub Desktop.
WooCommerce Product Vendors: Add extra custom fields to vendor profiles
<?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 );
}
?>
@BertPC
Copy link

BertPC commented Apr 23, 2014

Thank you!

@BertPC
Copy link

BertPC commented Jun 24, 2014

I finally got to putting this in, and the main Product Vendors screen lets me enter a new vendor, but when I go to edit it, I see this error and the custom field is not present on the edit form:

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'custom_edit_vendor_fields' not found or invalid function name in /home/nhmadehq/public_html/wp-includes/plugin.php on line 470

Does this mean the "custom_edit_vendor_fields" hook isn't present? I have WooCommerce 2.1.11, WordPress 3.9.1, and Product Vendors 1.0.8. (Though the system status report says there's a Product Vendors 1.0.9, I'm not seeing that it's available in WooThemes Helper, nor on the plugin update page.)

I can send a system status dump and the code I'm using if you want; I've only updated the name of the field and the labels, nothing else.

@jbricha
Copy link

jbricha commented Jul 16, 2014

In line 17 :
it's function custom_edit_vendor_fields( $vendor ) {
not function edit_vendor_fields( $vendor ) {

@fegul
Copy link

fegul commented Feb 18, 2015

Is there a way to add an image upload feature to the vendor page?

@LlewopG
Copy link

LlewopG commented Apr 13, 2015

I've implemented this code and the field appears with no errors but for some reason its not saving? any ideas?

@sanjaynakate
Copy link

I have added this code in my custom.php and i a m using thesis it showing field in backed vendor forms but fields are not saving any body have any idea why this is happening with me please help me to sort out this issue. i have searched through web but i ddint find any solution except this one.i am using WooCommerce Product Vendors Version 1.1.11

@rituhritika90
Copy link

It is saving the field but you just need to add value="" in input type in custom_edit_vendor_fields () function (line no 30 ). Then the code will work well.

@waynhall
Copy link

Thank you so much. I used this to add city, state, and zip code to vendors. I was about to post about the value= part, but I see @rituhritika90 has already posted about that.

@cncwebsolutions
Copy link

Dumb question but the new field i just created, how do I pull that in on my page?

Do I do something like this:

@rituhritika90
Copy link

Hey @ProgrammingGenius,
You need to do nothing, just put the above code in to your theme functions.php. Automatically the one filed named as "Vendor website" will appear in your plugin and rest if you want more then similarly copy paste the things with new names.

@tmth
Copy link

tmth commented Nov 26, 2015

Hi, i added the code, and added the value in the script but It shows only on the vendor edit page, not showing the output on the website. Any ideas?.. Would greatly appreciate any input..

@javi766
Copy link

javi766 commented Nov 27, 2015

Is there a way to add an image upload feature to the vendor page?

@abhiy707
Copy link

abhiy707 commented Dec 2, 2015

hi i am setup to give facility to add custom fields to like city to admin as well as vendors on add product page i copied the function in my function.php file but there is no change in my add product page can any one please tell step by step to me how i can use this function

thanks

@rituhritika90
Copy link

Hey @abhiy707,
It will not effect in product page these fields are for vendor custom post type only. Click on vendors-> while adding a new vendor -> here you are can see your new input fields. For product page you have to edit the vendor plugin a bit, this is not as heavy work. Find out from where the vendor's list coming from plugin in product page , then copy paste similarly for you input filed.

@rituhritika90
Copy link

Hey @javi766,
You can use Advance Custom filed wp plugin for this. I have used and it works.

@rituhritika90
Copy link

Hey @tmth,
Use the below code
$meta_values = get_post_meta( $productID, 'vendor_input_values',true );
$price = unserialize($meta_values);
print_r($price);

Enjoy 👍

@evolve301
Copy link

Can some one post the full code to add an Image uploader field in the vendor area. So the admin can go in and upload a image to appear on each vendors product pages.

Copy link

ghost commented Jun 12, 2016

I am using this for a form but I am trying to use it with a drop down but its not saving the values any ideas what we are doing wrong

                 <select name="vendor_size" id="vendor_size" value="<?php if ( ! empty( $_POST['vendor_size'] ) ) echo esc_attr( trim( $_POST['vendor_size'] ) ); ?>" tabindex="16">
          <option value="S" <?php if (!empty($vendor_size) && $vendor_size == 'S')  echo 'selected = "selected"'; ?>>S</option>
          <option value="M" <?php if (!empty($vendor_size) && $vendor_size == 'M')  echo 'selected = "selected"'; ?>>M</option>
          <option value="L" <?php if (!empty($vendor_size) && $vendor_size == 'L')  echo 'selected = "selected"'; ?>>L</option>
          <option value="XL" <?php if (!empty($vendor_size) && $vendor_size == 'XL')  echo 'selected = "selected"'; ?>>XL</option>

        </select>
function save_vendor_custom_fields( $term_id ) {
    if ( ! wp_verify_nonce( $_POST['vendor_custom_fields_nonce'], basename( __FILE__ ) ) ) {
        return;
    }

    $old_vendor_job      = get_term_meta( $term_id, 'vendor_job', true );
    $old_vendor_address = get_term_meta( $term_id, 'vendor_address', true );
    $old_vendor_biography = get_term_meta( $term_id, 'vendor_biography', true );
    $old_vendor_photo      = get_term_meta( $term_id, 'vendor_photo', true );
    $old_vendor_cv = get_term_meta( $term_id, 'vendor_cv', true );
    $old_vendor_type = get_term_meta( $term_id, 'vendor_type', true );
    $old_vendor_size      = get_term_meta( $term_id, 'vendor_size', true );
    $old_vendor_priceperhour = get_term_meta( $term_id, 'vendor_priceperhour', true );
    $old_vendor_sex = get_term_meta( $term_id, 'vendor_sex', true );
    $new_vendor_job      = ( $_POST['vendor_job'] );
    $new_vendor_address = ( $_POST['vendor_address'] );
    $new_vendor_biography = ( $_POST['vendor_biography'] );
    $new_vendor_photo      = ( $_POST['vendor_photo'] );
    $new_vendor_cv = ( $_POST['vendor_cv'] );
    $new_vendor_type = ( $_POST['vendor_type'] );
    $new_vendor_size      = ( $_POST['vendor_size'] );
    $new_vendor_priceperhour = ( $_POST['vendor_priceperhour'] );
    $new_vendor_sex = ( $_POST['vendor_sex'] );

    if ( ! empty( $old_vendor_size ) && $new_vendor_size === '' ) {
        delete_term_meta( $term_id, 'vendor_size' );

@ugoxuqu
Copy link

ugoxuqu commented Mar 3, 2017

Please I need help with creating a google location field for vendors. That is, I would like to add either the Lat/Long coordinates of the vendor's location or grab it from google map/location services; the latter being the preferred option. Any one, please?

@rituhritika90
Copy link

@ugoxuqu You can add a google map location field using Advanced Custom Fields (WP ACF plugin ), by using this plugin you can add map and able to get lat and Long for particular vendor.

@bondseid
Copy link

Hello,

I have copied the exact code above into my funtions.php but not seeing the new field anywhere. Not in the admin not during signgup Has anything changed in the code?

@Sammuel09
Copy link

Can I use this code with Woocommerce product vendors 2.0. If so, what do I need to change or edit to get it to work.?

@ka1888868
Copy link

Please can anyone tell me how to add password field to choose a password for registration instead of generate password automatically?

@rozsara
Copy link

rozsara commented Apr 6, 2022

Hi Hugh - do you have any idea how I can change the wording of 'Commission' to 'Revenue' on the vendors completed order email template which reads:
Your commission for this order is €amt
I am using woo product vendors AND Eventon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment