WordPress custom user_meta not saving - wordpress

I have built a custom plugin and I need to be able to add custom meta to the user profile screen.
I have the meta showing but they don't seem to update. This is the first instance of this meta so it doesn't exist in the DB already.
The files are located in my plugin dir and not in my theme.
<?php
add_action( 'show_user_profile', 'add_extra_social_links' );
add_action( 'edit_user_profile', 'add_extra_social_links' );
function add_extra_social_links( $user )
{
?>
<h3>Web App Information</h3>
<table class="form-table">
<tr>
<th><label for="companyname">Company Name</label></th>
<td>
<?php
$args = array(
'post_type' => array('operators','clients'),
'posts_per_page' => -1,
'orderby' => 'title',
);
$myposts = get_posts( $args );
echo '<select id="companyname"name="companyname">';
echo '<option value="' . esc_attr(get_the_author_meta( 'companyname', $user->ID )) . '">';
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<option value="<?php echo $post->post_title; ?>">
<?php echo $post->post_title; ?>
</option>
<?php endforeach;
echo '</select>';
wp_reset_postdata();?>
</td>
</tr>
<tr>
<th><label for="companyadmin">Is this user a company admin.</label></th>
<td>
<?php
echo '<select id="companyadmin" name="companyadmin">';
echo '<option value="' . esc_attr(get_the_author_meta( 'companyadmin', $user->ID )) . '">';
echo '<option value="Y">Yes, is admin.</option>';
echo '<option value="N">No, is not admin.</option>';
echo '</select>';
?>
</td>
</tr>
</table>
<?php
}
add_action( 'personal_options_update', 'save_extra_social_links' );
add_action( 'edit_user_profile_update', 'save_extra_social_links' );
function save_extra_social_links( $user_id )
{
update_user_meta( $user_id,'companyname', $_POST['companyname'] );
update_user_meta( $user_id,'companyadmin', $_POST['companyadmin'] );
}
?>

try use function get_current_user_id in $user_id
i think you use parameter but not send in hook

Related

Can't show the product name on table using get_name();

I can't echo the product name on my order page. I'm using ultimate member plugin with WooCommerce. "get_id();" is working just fine. I used "$order->get_name()" to echo the ordered product name. but it shows me error when tried to load the page.
<tbody>
<?php
foreach( $customer_orders as $customer_order ) {
$order = wc_get_order( $customer_order->ID );
$order_id = $customer_order->ID;
$order_data = $order->get_data();
$order_date = strtotime( $order->get_date_created() );
?>
<tr class="order" data-order_id="<?php echo esc_attr( $order_id ); ?>">
<?php do_action( 'um_woocommerce_orders_tab_before_table_row', $order, $customer_orders ); ?>
<td class="order-total" data-title="<?php _e( 'Item', 'um-woocommerce' ); ?>">
<?php echo $order->get_name() ?></td>
<td class="order-date" data-title="<?php _e( 'Date', 'um-woocommerce' ); ?>">
<time
datetime="<?php echo wp_date( 'Y-m-d', $order_date ); ?>"><?php echo wp_date( $date_time_format, $order_date ); ?></time>
</td>
<td class="order-status" data-title="<?php _e( 'Status', 'um-woocommerce' ); ?>">
<span
class="um-woo-status <?php echo $order->get_status(); ?>"><?php echo wc_get_order_status_name( $order->get_status() ); ?></span>
</td>
<td class="order-total" data-title="<?php _e( 'Total', 'um-woocommerce' ); ?>">
<?php echo $order->get_formatted_order_total() ?></td>
<td class="order-detail">
<a href="<?php echo esc_url( "$url#$order_id" ); ?>" class="um-woo-view-order um-tip-n"
title="<?php esc_attr_e( 'View order', 'um-woocommerce' ); ?>"><i class="um-icon-eye"></i></a>
<?php do_action( 'um_woocommerce_orders_tab_actions', $order, $customer_orders ); ?>
</td>
<?php
if ( UM()->options()->get('woo_account_order_ations') ) {
$actions = wc_get_account_orders_actions( $order );
echo '<td class="order-actions">';
if ( !empty( $actions ) ) {
foreach ( $actions as $key => $action ) {
echo '' . esc_html( $action['name'] ) . '';
}
}
echo '</td>';
}
?>
<?php do_action( 'um_woocommerce_orders_tab_after_table_row', $order, $customer_orders ); ?>
</tr>
<?php } ?>
</tbody>
There is no get_name() function for order object. You need loop through the order items to get the product object and use the $product->get_name() function
<?php foreach ($order->get_items() as $item_key => $item) {
$product = $item->get_product();
?>
<td class="order-total" data-title="<?php _e( 'Item', 'um-woocommerce' ); ?>">
<?php echo $product->get_name() ?>
</td>
<?php } ?>

Display shipping for each products on invoice - Woocommerce

I want to show a shipping row on invoice because I use the plugin "Multiple Packages for WooCommerce". Indeed for each product the customer can choose "livraison express (25€)" or "Livraison Franco de port" (0€).
Currently my invoice looks like this
So I wish it looked like this :
The shipping method above each products and only the amount of delivery in the row of total.
In the file "invoice.php", I Have this :
<tbody>
<?php $items = $this->get_order_items(); if( sizeof( $items ) > 0 ) : foreach( $items as $item_id => $item ) : ?>
<tr class="<?php echo apply_filters( 'wpo_wcpdf_item_row_class', $item_id, $this->type, $this->order, $item_id ); ?>">
<td class="product">
<?php $description_label = __( 'Description', 'woocommerce-pdf-invoices-packing-slips' ); // registering alternate label translation ?>
<span class="item-name"><?php echo $item['name']; ?></span>
<?php do_action( 'wpo_wcpdf_before_item_meta', $this->type, $item, $this->order ); ?>
<span class="item-meta"><?php echo $item['meta']; ?></span>
<dl class="meta">
<?php $description_label = __( 'SKU', 'woocommerce-pdf-invoices-packing-slips' ); // registering alternate label translation ?>
<?php if( !empty( $item['sku'] ) ) : ?><dt class="sku"><?php _e( 'Référence :', 'woocommerce-pdf-invoices-packing-slips' ); ?></dt><dd class="sku"><?php echo $item['sku']; ?></dd><?php endif; ?></dl>
<?php do_action( 'wpo_wcpdf_after_item_meta', $this->type, $item, $this->order ); ?>
</td>
<td class="quantity"><?php echo $item['quantity']; ?></td>
<td class="price"><?php echo $item['order_price']; ?></td>
</tr>
<?php endforeach; endif; ?>
</tbody>
So I try to add this code
<?php echo $order->get_shipping_to_display(); ?>
Like this
<dl class="meta">
<?php $description_label = __( 'SKU', 'woocommerce-pdf-invoices-packing-slips' ); // registering alternate label translation ?>
<?php if( !empty( $item['sku'] ) ) : ?><dt class="sku"><?php _e( 'Référence :', 'woocommerce-pdf-invoices-packing-slips' ); ?></dt><dd class="sku"><?php echo $item['sku']; ?></dd><?php endif; ?>
<?php /*if( !empty( $item['weight'] ) ) : ?><dt class="weight"><?php _e( 'Weight:', 'woocommerce-pdf-invoices-packing-slips' ); ?></dt><dd class="weight"><?php echo $item['weight']; ?><?php echo get_option('woocommerce_weight_unit'); ?></dd><?php endif;*/ ?>
<dt class="sku"><?php_e( 'Livraison :', 'woocommerce-pdf-invoices-packing-slips' ); ?></dt><dd class="sku">
<?php echo $order->get_shipping_to_display(); ?></dd></dl>
but i get this :
So I understand that this code call the shipping for the order and not for the item but if I try <?php echo $item->get_shipping_to_display(); ?> I have an error.
Is someone can help me?

how to save and get selected item id list with add_meta_box

hi all i need create to metabox in page for the footer and header posttypes i created, in this code i tried for this but i cant save selected item and retrive id selected item in the frontend.
function wpdocs_register_meta_boxes() {
add_meta_box( 'meta-box-id', __( 'header', 'textdomain' ),'wpdocs_my_display_callback', 'page', 'normal' , 'high');
}
add_action( 'add_meta_boxes', 'wpdocs_register_meta_boxes' );
function wpdocs_my_display_callback() {
?>
<select name="count" id="selectId" >
<?php
$posts = new WP_Query(array('posts_per_page' => -1, 'post_type' => 'header'));
while($posts->have_posts()) : $posts->the_post(); ?>
<option id="selection" value="<?php echo get_the_ID(); ?>"><?php echo get_the_title(); ?></option>
<?php endwhile;
die()
?>
</select>
<?php
}
function wpdocs_save_meta_box( $post_id ) {
if (isset($_POST['selectId'])) {
update_post_meta( $post_id, 'selectId', $_POST['selectId']);
}
}
add_action( 'save_post', 'wpdocs_save_meta_box' );
you can directly use by postId like below
echo esc_attr( get_post_meta( get_the_ID(), 'selectId', true ) );
Change your wpdocs_my_display_callback function like below.
function wpdocs_my_display_callback( $post ) {
$selectId = get_post_meta( $post->ID, 'selectId', true );
?>
<select name="selectId" id="selectId" >
<?php
$posts = new WP_Query(array('posts_per_page' => -1, 'post_type' => 'header'));
while($posts->have_posts()) : $posts->the_post(); ?>
<option id="selection" value="<?php echo get_the_ID(); ?>" <?php selected( $selectId, get_the_ID() ); ?>><?php echo get_the_title(); ?></option>
<?php endwhile;
wp_reset_query();
?>
</select>
<?php
}
thanks for answer and that work, But in need two metabox one for header select and one for select footer. I use this code for but not work, when comment and disable section this code for header, footer is work and save. when comment footer section code, header is work and save options seleceted!!!
function header_register_meta_boxes() {
add_meta_box( 'meta-box-header', __( 'header', 'poedit' ),'header_my_display_callback', array('page','post'), 'side' , 'high');
}
add_action( 'add_meta_boxes', 'header_register_meta_boxes' );
function header_my_display_callback()
{
global $post;
$header_value = get_post_meta($post->ID, 'selected_header', true);
?>
<select name="selected_header" id="selected_header">
<?php
$posts = new WP_Query(array('posts_per_page' => -1,'post_status' => 'publish', 'post_type' => 'header'));
echo'<option value="0">select header</option>';
while ($posts->have_posts()) : $posts->the_post(); ?>
<option <?php selected($header_value, get_the_ID()); ?> id="selection_header" value="<?php echo get_the_ID(); ?>"><?php echo get_the_title(); ?></option>
<?php endwhile;
wp_reset_query();
?>
</select>
<?php
}
function header_save_meta_box( $post_id ) {
if (isset($_POST['selected_header'])) {
update_post_meta( $post_id, 'selected_header', $_POST['selected_header']);
}
}
add_action( 'save_post', 'header_save_meta_box' );
function footer_register_meta_boxes() {
add_meta_box( 'meta-box-footer', __( 'footer', 'poedit' ),'footer_my_display_callback', array('page','post'), 'side' , 'high');
}
add_action( 'add_meta_boxes', 'footer_register_meta_boxes' );
function footer_my_display_callback()
{
global $post;
$footer_value = get_post_meta($post->ID, 'selected_footer', true);
?>
<select name="selected_footer" id="selected_footer">
<?php
$posts = new WP_Query(array('posts_per_page' => -1,'post_status' => 'publish', 'post_type' => 'footer'));
echo'<option value="0">select footer</option>';
while ($posts->have_posts()) : $posts->the_post(); ?>
<option <?php selected($footer_value, get_the_ID()); ?> id="selection_footer" value="<?php echo get_the_ID(); ?>"><?php echo get_the_title(); ?></option>
<?php endwhile;
wp_reset_query();
?>
</select>
<?php
}
function footer_save_meta_box( $post_id ) {
if (isset($_POST['selected_footer'])) {
update_post_meta( $post_id, 'selected_footer', $_POST['selected_footer']);
}
}
add_action( 'save_post', 'footer_save_meta_box' );

How to get pages as a checkbox option in user profile

i want to list the pages in checkbox in user profile for the users role to check specified page for them
function my_user_field( $user ) {
$user_meta = get_the_author_meta( 'user_interests', $user->ID, true );?>
<table class="form-table">
<tr>
<th>
<label for="Dealing Type">Multi pages select for blurbs
</label></th><td>
<?php
$mypages = get_pages( array(
'sort_column' => 'post_date',
'sort_order' => 'desc'
) );
if(count($mypages)){
foreach( $mypages as $page )
{
$title = $page->post_title;
$slug = $page->post_name;
?>
<label for='user_interests_<?php echo $page->post_name; ?>' class='blurb_user_label_checkbox'>
<input id='user_interests_<?php echo $page->post_name; ?>' name='user_interests[<?php echo $page->post_title; ?>]' type='checkbox' value='$page->post_title'
<?php if ( in_array( $page->post_title, $user_meta ) ) echo ' checked="checked"'; ?> class='blurb_user_checkbox'>
<?Php echo $title; ?>
</label>
<?php
}
}
var_dump($user_meta);
?>
</td>
</tr>
</table>
function my_save_custom_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return FALSE;
update_usermeta( $user_id, 'user_interests', $_POST['user_interests']);
}
add_action( 'user_new_form', 'my_user_field' );
add_action( 'show_user_profile', 'my_user_field' );
add_action( 'edit_user_profile', 'my_user_field' );
add_action( 'personal_options_update', 'my_save_custom_user_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_custom_user_profile_fields' );
add_action('user_register', 'my_save_custom_user_profile_fields');
The checkbox is not working when options saved so please help me somebody. i hope guys you can help me.if there any other methods also please give me

get_post_meta in woocommerce email notifications

I'm trying to get some data from an order in a woocommerce email template, but get_post_meta just returns false. This code works on the thankyou page. I have spent too much time on this. Any help would be appreciated. Thanks!
global $post;
echo "test!!!<br />";
$x = get_post_meta( $order->id, 'attendee_data', true );
$y = get_post_meta( $order->id, 'attendee_test', true );
echo $order->id . '<br />';
echo $x;
echo $y;
I've attached a picture of the sql as well as an email.
SQL: http://i.stack.imgur.com/zUFBa.png
Email: http://i.stack.imgur.com/Uqtih.png
The whole email template:
<?php do_action('woocommerce_email_header', $email_heading); ?>
<p><?php _e( "Your order has been received and is now being processed. Your order details are shown below for your reference:", 'woocommerce' ); ?></p>
<?php do_action( 'woocommerce_email_before_order_table', $order, $sent_to_admin, $plain_text ); ?>
<h2><?php echo __( 'Order:', 'woocommerce' ) . ' ' . $order->get_order_number(); ?></h2>
<table cellspacing="0" cellpadding="6" style="width: 100%; border: 1px solid #eee;" border="1" bordercolor="#eee">
<thead>
<tr>
<th scope="col" style="text-align:left; border: 1px solid #eee;"><?php _e( 'Product', 'woocommerce' ); ?></th>
<th scope="col" style="text-align:left; border: 1px solid #eee;"><?php _e( 'Quantity', 'woocommerce' ); ?></th>
<th scope="col" style="text-align:left; border: 1px solid #eee;"><?php _e( 'Price', 'woocommerce' ); ?></th>
</tr>
</thead>
<tbody>
<?php echo $order->email_order_items_table( $order->is_download_permitted(), true, ( $order->status=='processing' ) ? true : false ); ?>
</tbody>
<tfoot>
<?php
if ( $totals = $order->get_order_item_totals() ) {
$i = 0;
foreach ( $totals as $total ) {
$i++;
?><tr>
<th scope="row" colspan="2" style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['label']; ?></th>
<td style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['value']; ?></td>
</tr><?php
}
}
?>
</tfoot>
</table>
<?php
global $post;
echo "test!!!<br />";
$x = get_post_meta( $order->id, 'attendee_data', true );
$y = get_post_meta( $order->id, 'attendee_test', true );
echo $order->id . '<br />';
echo $x;
echo $y;
foreach ( $x as $k => $p ) {
echo $k ." ... ". $p;
} ?>
<?php // attendee_order_details($order->get_order_number()) ?>
<?php do_action( 'woocommerce_email_after_order_table', $order, $sent_to_admin, $plain_text ); ?>
<?php do_action( 'woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text ); ?>
<h2><?php _e( 'Customer details', 'woocommerce' ); ?></h2>
<?php if ($order->billing_email) : ?>
<p><strong><?php _e( 'Email:', 'woocommerce' ); ?></strong> <?php echo $order->billing_email; ?></p>
<?php endif; ?>
<?php if ($order->billing_phone) : ?>
<p><strong><?php _e( 'Tel:', 'woocommerce' ); ?></strong> <?php echo $order->billing_phone; ?></p>
<?php endif; ?>
<?php wc_get_template( 'emails/email-addresses.php', array( 'order' => $order ) ); ?>
<?php do_action( 'woocommerce_email_footer' ); ?>
I did some testing based on your info and tried to reproduce your problems.
First I created a function which inserts the post meta (attendee_test and attendee_data) when placing an order (using the woocommerce_checkout_update_order_meta hook like you do).
I added this in my themes functions.php (Note that the values are based on your input (image from db) and are not dynamic, just for testing):
add_action('woocommerce_checkout_update_order_meta', 'add_meta_values', 2);
function add_meta_values($order_id){
// array with attendee data
$attendee_data = array(
array(
'edit' => 'false',
'company' => 'get',
)
);
add_post_meta( $order_id, 'attendee_data', $attendee_data );
add_post_meta( $order_id, 'attendee_test', 'test' );
}
Then I added the following code to the template customer-processing-order.php:
<?php
$attendee_data = get_post_meta( $order->id, 'attendee_data', true );
$attendee_test = get_post_meta( $order->id, 'attendee_test', true );
echo 'Order ID: ' . $order->id . '<br />';
echo 'Attendee Test: ' . $attendee_test . '<br />';
echo 'Attendee Data:<br />';
foreach ( $attendee_data as $k => $data ) {
foreach ($data as $key => $value){
echo $key . ' .. ' . $value . '<br />';
}
} ?>
As you can see I modified it a bit to make it more clear. Also changed the $attendee_data loop because I think that wasn't completely right (needed an extra foreach). Of course this isn't related to the real problem.
Making a testorder will show the following data in the email:
This result demonstrate that get_post_meta is working in the email template based on the code above. (I used Wordpress 4.0.1 and WooCommerce 2.2.10).
If the test above is working fine also in your case then I think that the values are inserted in your database after the email is sent (they are inserted right, but to late).
Another thing you might want to check is resending the confirmation email. You can do this from the Wordpress admin. When you edit an order you have a dropdown 'Actions' on the right side. You can select 'Processing order' under 'Resend order emails'. Then click the refresh icon next to the dropdown and you will receive the order confirmation email again. See screenshot below:
This time you know for sure the values are already in the database before you're sending the email.
Hope this information is helping you to solve the problem.
Well, 'attendee_data' is a serialized object so the third argument passed on the function get_post_meta must be set to false (or omitted).
$x = get_post_meta( $order->id, 'attendee_data', false );

Resources