I want to add one input field in view order details - wordpress

In my woocommerce project I want one input field in view order page,If customer click the view order it will show one text box to add notes,I guess,this values should be store in (wp_woocommerce_order_itemmeta) table.Can you help me..I tried something..
function add_name_on_field() {
echo '<form class="cart" method="post" enctype="multipart/form-data"><table class="variations"
cellspacing="0"> <tbody><tr>
<td class="label"><label for="name">Add notes</label></td>
<td class="value">
<input type="text" name="name-on-tshirt" value="" /> <button type="submit"
class="">Submit</button</td></tr></tbody></table></form>'; }
add_filter( 'woocommerce_order_item_quantity_html',
'add_name_on_field' );
For storing I tried this..
function tshirt_order_meta_handler( $item_id ) {
if( WC()->session->__isset( $cart_item_key.'_name_on_tshirt' ) ) {
wc_add_order_item_meta( $item_id, "name_on_tshirt", WC()->session->get( $cart_item_key.'_name_on_tshirt') );}}
add_action( 'woocommerce_add_order_item_meta', 'tshirt_order_meta_handler', 1, 3 );

You need to check out: http://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/
There it states how to proceed doing what you want.

Related

How to create Unique User ID in WordPress

I need to add a users from WordPress backend. So, I want to generate unique User IDs for them. But I can achieve it. Can somebody help me out on this.
Here is what I cam so far. It is better if I could append the WordPress default generated unique user id to end of my field.
// Customize the user fields
function custom_user_profile_fields($user) {
$rand = randomGen(1000,5000,1);
$auth_Token = '2021-ABCD-'.$rand[0];
?>
<h3>Profile Information</h3>
<table class="form-table">
<tr>
<th><label for="srn">Student Registration Number</label></th>
<td>
<input type="text" class="regular-text" name="srn" value="<?php echo (get_option('srn') ? esc_attr( get_the_author_meta( 'srn', $user->ID ) ) : $auth_Token); ?>" id="srn" readonly /><br />
</td>
</tr>
</table>
<?php
}
add_action( 'show_user_profile', 'custom_user_profile_fields' );
add_action( 'edit_user_profile', 'custom_user_profile_fields' );
add_action( "user_new_form", "custom_user_profile_fields" );
function save_custom_user_profile_fields($user_id) {
# again do this only if you can
if(!current_user_can('manage_options'))
return false;
# save my custom field
update_usermeta($user_id, 'srn', $_POST['srn']);
}
add_action('user_register', 'save_custom_user_profile_fields');
add_action('profile_update', 'save_custom_user_profile_fields');
When a user added I can't get the previously saved Index Number to the field. It's generating another and showing again. As I am not an expert for this, could anybody help me out with this?
Thanks in advance
You were on the right path to solve the problem, but you made some mistakes.
You can use the working code below:
Creation of fields
add_action( 'user_new_form', 'bks_add_student_reg_number_field' );
add_action( 'edit_user_profile', 'bks_add_student_reg_number_field' );
add_action( 'show_user_profile', 'bks_add_student_reg_number_field' );
function bks_add_student_reg_number_field( $user ) {
?>
<h3><?php _e("Extra profile information", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="student_reg_number"><?php _e("Student Registration Number"); ?></label></th>
<td>
<?php if (is_object($user)) { ?>
<input type="text" name="student_reg_number" id="student_reg_number" value="<?php echo esc_attr( get_the_author_meta( 'student_reg_number', $user->ID )); ?>" class="regular-text" readonly disabled="disabled"/><br />
<?php } else { ?>
<input type="text" name="student_reg_number" id="student_reg_number" class="regular-text" /><br />
<span class="description"><?php _e("Please enter Student registration number."); ?></span>
<?php } ?>
</td>
</tr>
</table>
<?php }
This creates a field option at new user generation and edit form.
The field is only enabled when a new user is created. If the registered user has already have a value the value would be shown with no access to change it.
Save field value
add_action( 'user_register', 'bks_student_reg_number_field' );
function bks_student_reg_number_field($user_id) {
// You can maybe add checks here whch would determine if the users role is customer
// or not or maybe validate the number.
if(!current_user_can('manage_options'))
return false;
$reg_value = get_user_meta($user_id, 'student_reg_number', true);
if (isset( $_POST['student_reg_number']) && $_POST['student_reg_number'] !== $reg_value) {
update_user_meta($user_id, 'student_reg_number', $_POST['student_reg_number'].'-'.$user_id);
}
}
Only user_register hook is used to prevent updation on profile edit.
You can see the user_id appended with a - infront of it in the field and the field is readonly and disabled.
If you want user to be able to edit the id after registering as well then. do the following.
Add profile_update hook. add_action( 'profile_update', 'bks_student_reg_number_field' ); as well so that the code runs when you update the user as well.
Remove readonly and disabled attribute from the form.
The code is TESTED and WORKS.

Add custom fields upon user creation

I was trying to add custom fields using the hooks:
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
and it perfectly fine but it only shows upon editing of users and not upon creating new users. How can I add custom fields upon creation? Can I also specify to only show this field depending on the selected user role?
Edit
Also tried the suggestion:
do_action( 'user_new_form', 'extra_user_profile_fields' );
But it doesn't seem to work
Add your custom fields using follows- hook to show it in add user section.
do_action( 'user_new_form', 'add-new-user' );
And the code snippet are as follows -
// Add extra custom data
function extra_user_profile_fields(){
?>
<h3>Extra User Profile data</h3>
<table class="form-table">
<tr>
<th><label for="company">Company Name</label></th>
<td>
<input type="text" class="regular-text" name="company_name" value="" id="company_name" /><br />
<span class="description">Add your company name.</span>
</td>
</tr>
</table>
<?php
}
add_action( "user_new_form", "extra_user_profile_fields" );
// Used to save extra custom data
function save_extra_user_profile_fields($user_id){
if(isset($_POST['company_name']))
update_user_meta($user_id, '_user_company', $_POST['company_name']);
}
add_action('user_register', 'save_extra_user_profile_fields');
And you can show or hide this fields for selected user role using scripts.

WP Options API Custom Action After Form Submit

I'm sorry if this is an old question but I really can't find answer for this. I am new to wordpress plugin development. How can I do a somewhat like a callback before a form is submitted using WP Options API? In a native PHP, we can just say as
if (isset($_POST['submit'])) {
$options = $_POST['zt_ftp_settings'];
var_dump($options);
}
However, I am using the WP Options API. It saves the data correctly, I just want to do some more after saving the data.
Here is what I did,
<form method="post" action="options.php">
<?php
settings_fields('zt_ftp_settings_options' );
do_settings_sections( 'zt_ftp_settings_options' );
$option = get_option('zt_ftp_settings');
?>
<h2>FTP Settings</h2>
<p>Please provide your ftp credentials.</p>
<table class="form-table">
<tbody>
<tr>
<th scope="row">FTP Hostname</th>
<td>
<input name="zt_ftp_settings[zt_ftp_host_name]" type="text" id="zt_ftp_host_name" value="<?php echo $option['zt_ftp_host_name']?>" class="regular-text">
</td>
</tr>
<tr>
<th scope="row">FTP Username</th>
<td>
<input name="zt_ftp_settings[zt_ftp_user_name]" type="text" id="zt_ftp_user_name" value="<?php echo $option['zt_ftp_user_name']?>" class="regular-text">
</td>
</tr>
<tr>
<th scope="row">FTP Password</th>
<td>
<input name="zt_ftp_settings[zt_ftp_password]" type="password" id="zt_ftp_password" value="<?php echo $option['zt_ftp_password']?>" class="regular-text">
</td>
</tr>
</tbody>
</table>
<?php submit_button( 'Save Settings' ); ?>
Thanks.
Use WP AJAX functionality.
Use below JS,
$("#submit").click(function(){
var url = window.location.href;
jQuery.post(
ajaxurl,
{
'action': 'delete_search',
'tid': tid,
},
function(response){
location.reload();
});
});
Replace your submit button ID and use your form values.
Use below code in function.php
function prefix_ajax_delete_search() {
global $wpdb;
$user_id = get_current_user_id();
$wpdb->delete('sf_save_search', array('id' => $_POST['tid']) );
wp_die();
}
add_action( 'wp_ajax_delete_search', 'prefix_ajax_delete_search' );
As per your requirement change the function name and variables

How to display Woocommerce product price by ID number on a custom page?

I'm trying to display a price of a product in Woocommerce, on a custom page.
There is a short code for that, but it gives product price and also adds an "Add to cart button", I don't want the button, i just want to get the price of a specific product by ID.
Is this possible?
Thanks.
CODE:
<table class="unlockTableBorder">
<tbody>
<tr>
<td>
<h2>פתיחת מכשירי Alcatel כלל עולמי</h2>
<h4>אנא קראו והבינו את תנאי השירות הבאים לפני הזמנת שירות זה:</h4>
<ul>
<li>שירות זה תומך בפתיחת מכשירים סלולריים מסוג Alcatel מארה"ב, קנדה ומקסיקו.</li>
<li>מכשירי CDMA וספקיות שירות CDMA לא נתמכים בידי שירות זה, אנא אל תשתמשו בשירות זה בשביל מכשירים אלו - במידה ותשמשו בשירות זה למכשירי CDMA, אתם תקבלו קוד שלא תוכלו להשתמש בו, ולא תוכלו לקבל החזר כספי! - אנא <a title="פתיחת מכשירי CDMA לכל הרשתות" href="http://www.unlocker.co.il/sim-unlock-cdma-mobile-device/">ראו פתיחת מכשירי CDMA לכל הרשתות בישראל.</a></li>
</ul>
<h5><strong>זמן הספקה: 1-24 שעות</strong></h5>
<form id="unlock1" class="cart" enctype="multipart/form-data" method="post" name="unlock"><input class="the_imei" style="width: 80%; border-radius: 15px;" name="the_imei" type="text" value="" placeholder="מספר סידורי IMEI של המכשיר (חייג #06#*)" /> <input class="add-to-cart" name="add-to-cart" type="hidden" value="76" /> <button class="unlockButton" type="submit" value="submit">פתח לכל הרשתות בישראל </button></form>*בלחיצה על הפתור, אתם מסכימים ל<a title="תנאי השירות" href="http://www.unlocker.co.il/terms-and-conditions/">תנאי השירות</a>.</td>
</tr>
</tbody>
</table>
<script src="http://www.unlocker.co.il/checkimei1.js" type="text/javascript"></script>
If you have the product's ID you can use that to create a product object:
$_product = wc_get_product( $product_id );
Then from the object you can run any of WooCommerce's product methods.
$_product->get_regular_price();
$_product->get_sale_price();
$_product->get_price();
Update
Please review the Codex article on how to write your own shortcode.
Integrating the WooCommerce product data might look something like this:
function so_30165014_price_shortcode_callback( $atts ) {
$atts = shortcode_atts( array(
'id' => null,
), $atts, 'bartag' );
$html = '';
if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
$_product = wc_get_product( $atts['id'] );
$html = "price = " . $_product->get_price();
}
return $html;
}
add_shortcode( 'woocommerce_price', 'so_30165014_price_shortcode_callback' );
Your shortcode would then look like [woocommerce_price id="99"]
In woocommerce,
Get regular price :
$price = get_post_meta( get_the_ID(), '_regular_price', true);
// $price will return regular price
Get sale price:
$sale = get_post_meta( get_the_ID(), '_sale_price', true);
// $sale will return sale price
Other answers work, but
To get the full/default price:
$product->get_price_html();

wordpress template specific metaboxes?

i have built several diffrent templates for my WP site each calling diffrent metas.
ok, im looking for a way to add a meta-box that will only display if the correct page template is selected.
say i have a homepage.php template, this template calls some specific meta info that only the homepage uses, a call to action of example, so i need the meta-box to appear once the proper page template is selected and disappear if a different page template is selected.
any help is appreciated.
get_page_template will return the template you are looking at. You can switch on this to display the content you want.
ok i finally found a way around it guys and here it is:
<?php
function my_admin() {
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
$template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
// check for a template type
if ($template_file == 'home.php'){
add_meta_box( 'product_info',
'Informacion de Producto',
'display_product_info_meta_box',
'ex_producto', 'normal', 'high'
);
}
}
function display_product_info_meta_box( $product_info ) {
$product_price = floatval( get_post_meta( $product_info->ID, 'product_price', true ) );
$product_code = esc_html( get_post_meta( $product_info->ID, 'product_code', true ) );
?>
<table>
<tr style="width: 100%">
<td >Precio Producto</td>
<td><input type="text" size="80" name="product_info_product_price" value="<?php echo $product_price; ?>" /></td>
</tr>
<tr style="width: 100%">
<td >Codigo de Producto</td>
<td><input type="text" size="80" name="product_info_product_code" value="<?php echo $product_code; ?>" /></td>
</tr>
</table>
<?php
}
add_action( 'admin_init', 'my_admin' );
?>
so... yeap i hope that dose someone out there some good :)

Resources