I have been working with Wordpress and Woocommerce to add a custom meta field to my admin products page. I have everything working fine except that when I save the product, it does not show the info I entered in my meta box.
I checked the database and the data does indeed save, I saw the data I entered in the value and the meta field name was in the meta key. So that leads me to believe everything is working except showing the value on the admin product page once the product has been saved.
I think that the problem lies within one of these two functions but not certain, it could be in the save function though where it calls the update_post_meta.
here is where I think my problem is:
function variable_fields( $loop, $variation_data ) {
?>
<tr>
<td>
<div>
<label><?php _e( 'My Custom Field', 'woocommerce' ); ?></label>
<input type="text" size="5" name="my_custom_field[<?php echo $loop; ?>]" value="<?php echo $variation_data['_my_custom_field'][0]; ?>"/>
</div>
</td>
</tr>
<?php
}
function variable_fields_js() {
?>
<tr>
<td>
<div>
<label><?php _e( 'My Custom Field', 'woocommerce' ); ?></label>
<input type="text" size="5" name="my_custom_field[' + loop + ']" />
</div>
</td>
</tr>
<?php
}
This is the function that saves the data, just incase the problem could be in here?
function variable_fields_process( $post_id ) {
if (isset( $_POST['variable_sku'] ) ) :
$variable_sku = $_POST['variable_sku'];
$variable_post_id = $_POST['variable_post_id'];
$variable_custom_field = $_POST['my_custom_field'];
for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
$variation_id = (int) $variable_post_id[$i];
if ( isset( $variable_custom_field[$i] ) ) {
update_post_meta( $variation_id, '_my_custom_field', stripslashes( $variable_custom_field[$i] ) );
}
endfor;
endif;
}
Looking through the code nothing is sticking out to me. Everything is working except when I save the product, the data entered doesnt show (but it is in the database) so Im thinking it could be a conflict with when it is saved, showing the saved values in the fields.
Thanks for your help
Update:
Specifically I think it is this line that is jamming me up:
value="<?php echo $variation_data['_my_custom_field'][0]; ?>"
I know this is a little late but i managed to solve this and wanted to share it with you.
Basically WooCommerce have dropped $variation_data. You will now need to add $variation to your function and get the id. With the ID you can them get the post_meta / custom field. This can be done like so:
function variable_fields( $loop, $variation_data, $variation ) {
$get_variation_id = $variation->ID;
$data_to_get = get_post_meta($get_variation_id, '_my_custom_field', true );
This line:
value="<?php echo $variation_data['_my_custom_field'][0]; ?>"
Then becomes:
value="<?php echo $data_to_get; ?>"
That's it! i hope this helps you and anyone else that comes across this page.
I would recommend trying the Advanced Custom Fields (ACF) plugin, assuming this is for your site and not code to go into a published plugin.
I also started trying to code custom fields into a site, then realised after several pages of code that goes way beyond the business needs of "just adding a field", that the ACF plugin does the same job in ten clicks and two minutes. Sometimes you just have to be pragmatic, and accept that occasionally the best way of winning the debugging game is not to play the coding game in the first place.
Apologies if that is not the answer you or SO is looking for.
Related
I am trying to add 2 custom fields with 15 character input limit to Dokan Vendor Registeration form. I am completely new to wordpress and have little coding knowledge on how to do it. I need help in codes which can help me do it.
I refered other forums and changed the Dokan Seller-registeration-form.php to include these fields but they are not getting saved in the Dokan Vendor Profile Settings as well as wordpress backend.
These fields also need to be updated in the Admin Backend in wordpress.
I have tried the link below in stack as solution.
Add custom fields to existing form on Dokan (wordpress/woocoomerce).
I am missing something which i have no clue to where it needs to be modified.
I appreciate your help. Please let me know if you need any more details.
Regards
Abhilash
Here I am showing how you can add extra field in the registration form. [Not with input limit]
You need a child-theme before doing this process. If you have installed a child theme already then you can override the seller registration form through it.
Please create a folder called dokan into your child-theme and then create another folder into dokan folder called global. Now, copy the seller-registration-form.php file from your wp-content/plugins/dokan-lite/templates/global folder and paste it to your child-theme
Now, from your child-theme open the seller-registration-form.php file and add the extra field on your desire position like below –
You can use this code if you want to just copy and paste (Field for GST Number) –
<p class="form-row form-group form-row-wide">
<label for="shop-phone"><?php esc_html_e( 'GST Number', 'dokan-custom-codes' ); ?>
<span class="required">*</span></label>
<input type="text" class="input-text form-control" name="gst_id" id="gst_id" value="<?php if ( ! empty( $postdata['gst_id'] ) ) echo esc_attr($postdata['gst_id']); ?>" required="required" />
</p>
After adding the input field you will be able to see the registration field on the vendor registration form-
Now, you have to save the field data and need to show on user profile.
Just open your functions.php file and paste the below code –
function dokan_custom_seller_registration_required_fields( $required_fields ) {
$required_fields['gst_id'] = __( 'Please enter your GST number', 'dokan-custom' );
return $required_fields;
};
add_filter( 'dokan_seller_registration_required_fields',
'dokan_custom_seller_registration_required_fields' );
function dokan_custom_new_seller_created( $vendor_id, $dokan_settings ) {
$post_data = wp_unslash( $_POST );
$gst_id = $post_data['gst_id'];
update_user_meta( $vendor_id, 'dokan_custom_gst_id', $gst_id );
}
add_action( 'dokan_new_seller_created', 'dokan_custom_new_seller_created', 10, 2 );
/* Add custom profile fields (call in theme : echo $curauth->fieldname;) */
add_action( 'dokan_seller_meta_fields', 'my_show_extra_profile_fields' );
function my_show_extra_profile_fields( $user ) { ?>
<?php if ( ! current_user_can( 'manage_woocommerce' ) ) {
return;
}
if ( ! user_can( $user, 'dokandar' ) ) {
return;
}
$gst = get_user_meta( $user->ID, 'dokan_custom_gst_id', true );
?>
<tr>
<th><?php esc_html_e( 'Gst Number', 'dokan-lite' ); ?></th>
<td>
<input type="text" name="gst_id" class="regular-text" value="<?php
echo esc_attr($gst); ?>"/>
</td>
</tr>
<?php
}
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
function my_save_extra_profile_fields( $user_id ) {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
return;
}
update_usermeta( $user_id, 'dokan_custom_gst_id', $_POST['gst_id'] );
}
If you want to change the field name or meta key then you have to change the meta key or field name accordingly on every place. On this code, I have used the meta key for the field as dokan_custom_gst_id and use the field id as gst_id
After saving the above code, you will be able to use vendor GST Number on his/her user profile –
I'm attempting to save a simple text input to the WooCommerce session. The session is created when a user adds something to their cart.
My input field exists in custom page template that will be placed in the user flow after the cart but before the checkout: cart > my template > checkout.
So far
Simple form to capture data (custom template file)
<form name="group" method="post" class="checkout woocommerce-checkout" action="http://localhost:/site.dev/my-template">
<div class="group-order">
<p class="form-row form-row woocommerce-validated" id="create_new_group_field">
<label for="create_new_group" class="">Join an existing group</label>
<input type="text" class="input-text " name="create_new_group" id="create_new_group">
</p>
</div>
</form>
Receiving and setting data (I'm having trouble figuring out when/how to run this. in my custom page)
UPDATE
I've added the code below to the top of my page template so the page processes itself and then re-directs to the checkout.
function set_and_save_input_to_session() {
if( !is_admin( ) ) {
// User input
if( ! empty( $_POST['create_new_group'] ) ) {
$group_input_value = $_POST['create_new_group'];
// Set session and save data
WC()->session->set( 'group_order_data', $group_input_value );
wp_redirect( 'http://localhost:28/site.dev/checkout' );
exit();
}
}
get_header();
add_action('woocommerce_checkout_process', 'set_and_save_input_to_session');
Retrieving and saving data
function retrieve_and_save_group_input_value_to_order_meta() {
$retrived_group_input_value = WC()->session->get( 'group_order_data' );
update_post_meta( $order_id, '_create_new_group', $retrived_group_input_value );
}
add_action('woocommerce_checkout_update_order_meta', 'retrieve_and_save_group_input_value_to_order_meta');
I'm currently working my way through what are to me, more complex solutions and therefore I'd appreciate if anyone could point out any major flaws with my process so far.
UPDATE
I can confirm that the form is receiving data and that the WC()->session->set is setting data. (Thanks to #Firefog for suggesting the use the $_SESSION global)
After further investigation and finding the right place to var_dump the session data I found that the data was being set to the session with my original method.
The data is set, but I can't see why the data won't save to the order.
It's more saying Thank you for solving my problem. But here is an answer, too:
The post meta could not been updated because there is no $order_id parameter in your callback function. This should do the trick:
function retrieve_and_save_group_input_value_to_order_meta( $order_id ) {
$retrived_group_input_value = WC()->session->get( 'group_order_data' );
update_post_meta( $order_id, '_create_new_group', $retrived_group_input_value );
}
add_action('woocommerce_checkout_update_order_meta', 'retrieve_and_save_group_input_value_to_order_meta');
Here is another approach.
1st page:
session_start();//place this at the top of all code
$data = $_POST['create_new_group'];
$_SESSION['custom_create_new_group']=$data;
Now in another page write the following to receive the value:
session_start(); //optional
$retrive_price = $_SESSION['custom_create_new_group'];
I don't know if this is the correct place to ask, but I'll try. I have an ongoing project with Wordpress (Genesis framework - Agentpress PRO theme) working with Ninja Forms, and am having a slight problem. The web page is: http://www.supercastor.net/wordpress/listings/castellon-65-m2-ytong/
I have a listings page (for each house model). Each listing has metadata that is accessible via a simple function from the genesis framework:
genesis_get_custom_field( 'CUSTOM_FIELD_NAME' );
I would like to have, like the link above has, a list of checkboxes that the user can select/deselect, and the TOTAL value is finally calculated. Everything is working, except that I cannot assign the value of this "custom field" that is from the listing.
I am using Ninja Forms, and after some research, I have come up with the solution of registering a custom field in NF:
function precio_basico_calc_display( $field_id, $data ){
// Get the default_value
if( isset( $data['default_value'] ) ){
$def_value_new = genesis_get_custom_field( '_listing_precio_sort' );
$data['default_value'] = $def_value_new;
$default_value = $data['default_value'];
debug_to_console("Default_value is set to:$default_value");
}else{
$default_value = '';
debug_to_console("Default_value NOT SET");
}
$products_select = genesis_get_custom_field( '_listing_precio_sort' );
}
// Now that $products_select is populated with the listing_price, output a checkbox with the value of the price. ?>
<input type="checkbox" name="ninja_forms_field_<?php echo $field_id;?>" value="<?php echo $products_select;?>" checked="checked" class="ninja-forms-field ninja-forms-field-calc-listen" disabled="disabled">
<?php
}
I also have a small "debug_to_console" function, but this is just as a quick "debugging". I can see that the value is there, but in the calculation it is not set. I have set the default to 40.000, but it should be set to the value for each price (in the link example, should be 19.000). As:
$products_select = genesis_get_custom_field( '_listing_precio_sort' );
And I have also tried the following (field_id 33 is the checkbox in the form):
add_filter( 'ninja_forms_field', 'my_filter_function', 10, 2 );
function my_filter_function( $data, $field_id ){
if( $field_id == 33 ){
$listing_price = genesis_get_custom_field( '_listing_precio_sort' );
$data['default_value'] = $listing_price;
}
return $data;
}
But it isn't working either. I mean, it does display the value (in the console), but not making the calculation correctly.
Help! I just want the calculation (TOTAL) to update correctly for each listing. I thought changing the default_value would do, but it doesn't. I'm lost :(
Thanks!
I have a strange problem. I have added custom field to User Profile and used the_author_meta() function for display fields content. Yeasterday works everything great. But today it doesn't work.
Custom field is in User Profile. When I click update profile - the content of fields remains in place (so information should be saved). But on authors page where I have <?php the_author_meta( 'skills' ); ?> happens nothing.
I followed this tutorial when I was creating my own custom field:
http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields
And this one when I tried to find the error in my code:
http://bavotasan.com/2009/adding-extra-fields-to-the-wordpress-user-profile/
They both are similar. I worked with page templates since yesterday, so it maybe caused the problem. So I deleted everything what I've added since yesterday. But nothing changed.
Here is my code:
// CUSTOM FIELD IN USER ADMIN - SAVE
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
function my_show_extra_profile_fields( $user ) { ?>
<h3>Skills - Färdigheter</h3>
<table class="form-table">
<tr>
<th><label for="skills">Skills</label></th>
<td>
<textarea type="text" name="skills" rows="4" cols="50" id="skills" class="regular-text"><?php echo esc_attr( get_the_author_meta( 'skills', $user->ID ) ); ?></textarea><br>
<span class="description">Please enter your skills.</span>
</td>
</tr>
</table>
<?php }
// CUSTOM FIELD IN USER ADMIN
add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
function my_save_extra_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
{return false;}
/* Copy and paste this line for additional fields. Make sure to change 'twitter' to the field ID. */
update_user_meta( $user_id, 'skills', $_POST['skills'] );
}
Now I think that you will save the user meta successfully. Only you need to display from them.
Now you can use the function "get_user_meta($user_id, $key, $single);".
Visit it here: https://codex.wordpress.org/Function_Reference/get_user_meta
Thanks
Problem solved! The solution was to use <?php echo $curauth->skills; ?> instead of <?php the_author_meta( 'skills' ); ?> or <?php get_user_meta($user_id, $key, $single); ?>
I don't know why, but it doesn't matter :D
Thought about asking on wordpress answers, but they would tell me it's a "plugin thing".. If you disagree then feel free to pass it over to them.
I'm using this code in my functions.php which offers a dropdown menu of available categories when a user clicks the new post button. This forces them to choose a category BEFORE the new blank post is created.
The problem is I'm getting "invalid post type" error and have it traced it back to a plugin called "adminimizer".
The new post never gets created, the address bar gets stuck at mysite.com/wp-admin/post-new.php?category_id%5B%5D=4&continue=Continue&post_type= (it should say "post" after the type=.. and I get an error box saying "invalid post type".
I'd love to hear any thoughts on why my script might not be completing. Here's the code I'm using:
//ADD DROPDOWN MENU TO CHOOSE NEW POST CATEGORY (CHOOSE THE CAT ID’S YOU WANT TO APPEAR)
add_filter( 'load-post-new.php', 'wpse14403_load_post_new' );
function wpse14403_load_post_new()
{
$post_type = 'post';
if ( isset( $_REQUEST['post_type'] ) ) {
$post_type = $_REQUEST['post_type'];
}
// Only do this for posts
if ( 'post' != $post_type ) {
return;
}
if ( array_key_exists( 'category_id', $_REQUEST ) ) {
add_action( 'wp_insert_post', 'wpse14403_wp_insert_post' );
return;
}
// Show intermediate screen
extract( $GLOBALS );
$post_type_object = get_post_type_object( $post_type );
$title = $post_type_object->labels->add_new_item;
include( ABSPATH . 'wp-admin/admin-header.php' );
$dropdown = wp_dropdown_categories( array(
'orderby' => 'name',
'include' => '3, 4',
'name' => 'category_id[]',
'hide_empty' => false,
'echo' => false,
) );
$category_label = __( 'Create New' );
$continue_label = __( 'Continue' );
echo <<<HTML
<div class="wrap">
<h2>{$title}</h2>
<form method="get">
<table class="orange">
<tbody>
<tr valign="top">
<th scope="row">{$category_label}</th>
<td>{$dropdown}</td>
</tr>
<tr>
<td></td>
<th><input name="continue" type="submit" class="button-primary" value=" {$continue_label}" /></th>
</tbody>
</table>
<input type="hidden" name="post_type" value="{$post_type}" />
</form>
</div>
HTML;
include( ABSPATH . 'wp-admin/admin-footer.php' );
exit();
}
// This function will only be called when creating an empty post,
// via `get_default_post_to_edit()`, called in post-new.php
function wpse14403_wp_insert_post( $post_id )
{
wp_set_post_categories( $post_id, $_REQUEST['category_id'] );
}
UPDATE / MORE INFO. On further investigation, I changed a bit of my code towards the end from hidden to text. This reveals an input field that is usually automatically filled with the post type "post". The adminimize plugin appears to prevent this from happening, even though I have not set it to hide anything?
<input type="text" name="post_type" value="{$post_type}" />
I guess the easy solution would be just don't use the adminimizer plugin, but it's very useful to keep things simple for new site owners. I've asked the plugin author about this, but don't expect an answer any time soon - thanks
Well it turns out that the plugin author (Frank) offers great support. He spotted there was a problem with this line & simply commented it out. Now everything works as expected :)
//extract( $GLOBALS );