I just want to ask how to display custom post meta that I created on customer order detail page on frontend.
Like for example I want to get the value of this meta receive_newsletter on success order detail page on Woocommerce.
Thanks,
JM
You can try this:
$user_id = get_current_user_id();
$key = 'receive_newsletter';
$single = true;
$meta = get_user_meta( $user_id, $key, $single );
echo $meta;
It is depend on where you want to show on order detail page.
For example this hook: do_action( 'woocommerce_order_details_after_order_table', $order );
You can use:
add_action('woocommerce_order_details_after_order_table','wn_display_receive_newsletter');
function wn_display_receive_newsletter($order){
$oder_id = $order->get_id();
$receive_newsletter = get_post_meta('receive_newsletter',$oder_id,true);
echo $receive_newsletter;
}
If you saved "receive_newsletter" meta to user, you can use this
add_action('woocommerce_order_details_after_order_table','wn_display_receive_newsletter');
function wn_display_receive_newsletter($order){
$user_id= $order->get_user_id();
$receive_newsletter = get_user_meta('receive_newsletter',$user_id,true);
echo $receive_newsletter;
}
Related
I am trying to create a shortcodes of payment_method.
I have a custom page thank you page, and i'm looking for the working code
Found here such code:
add_shortcode( 'custom-woocommerce-name' , 'custom_first_name' );
function custom_first_name(){
$customer_id = get_current_user_id();
$order = wc_get_customer_last_order( $customer_id );
return $order->get_billing_first_name();
}
I'm trying to display the author id on the author page by using a shortcode I created but it's currently not working because the shortcode is only outputting the administrator id.
This is what I'm returning in the shortcode:
return get_the_author_meta('ID');
Working code:
function author_id_shortcode(){
ob_start();
// We get the author ID outside loop.
global $post;
$author_id = $post->post_author;
// Now get the author ID inside loop.
$author_id = get_the_author_meta( 'ID' );
$output = get_the_author_meta( 'ID', $author_id );
ob_end_clean();
return $output;
}
add_shortcode( 'author_id', 'author_id_shortcode' );
call the shortcode with [author_id]
Please try the below code on author.php
if (is_author()){
$author = get_queried_object();
$author_id = $author->ID;
echo $author_id;
}
I am creating a Playlist using LearnDash Plugin,
Below is my code, This code works good but I can't get a video of a particular Lesson
Is there anyone who knows how to fix this?
add_shortcode( 'playlist', 'playlist_for_LMS' );
function playlist_for_LMS() {
global $post;
$user_id = get_current_user_id();
$courses = learndash_user_get_enrolled_courses( $user_id, array(), true );
$lessons = learndash_get_course_lessons_list( $courses[0], $user_id, array(), true );
foreach ($lessons as $lesson){
$postID = $lesson['post']->lesson_video_url;
$postImage = get_the_post_thumbnail($lesson['post']->ID);
$postTitle = $lesson['post']->post_title;
$postLink = get_post_permalink($lesson['post']->ID);
echo ''.$postTitle.' '.$postID.'<br>';
}
}
You use the below code and whats are the your output and go through your out put of fetched data. And also you can get errors if you have.
echo '<pre>';
print_r($lessons);
I have created a super simple theme. What i want to have is the ability to define an array of 3 fields for each post and page.
For example having the fields: Name, Link and a Dropdown for Type
Additionally i want to add multiple items of these "field sets" per post/page. I couldn't find any documentation on this but always results in Google which led me to WP Plugins. In general this is ood, cause in this case this seems to be possible programmatically, but bad cause i couldn't find any kind of information on this.
Hopefully someone can help me.
You are looking for custom meta boxes, with the add_meta_box() function:
Adds a meta box to one or more screens.
Source # https://developer.wordpress.org/reference/functions/add_meta_box/
And the add_meta_boxes action hook.
Fires after all built-in meta boxes have been added:
Source # https://developer.wordpress.org/reference/hooks/add_meta_boxes/
A simple example would be if we wanted to add a "Listening to..." custom meta box, to share our mood while writing a post.
<?php
add_action( 'add_meta_boxes', 'add_meta_listening_to' );
function add_meta_listening_to() {
add_meta_box(
'meta_listening_to', //id
'Listening to ...', //title
'listeningto', //callback
'post', //screen &/or post type
'normal', //context
'high', //priority
null //callback_args
);
};
function listeningto( $post ) { //function handle same as callback
$ListeningToInput = get_post_meta( $post->ID, 'ListeningToInput', true );
echo '<input name="listening_to_input" type="text" placeholder="Listening to ..." value="'. $ListeningToInput .'" style="width:100%;">';
};
add_action( 'save_post', 'save_meta_listening_to' );
function save_meta_listening_to( $post_ID ) {
if ( isset( $_POST[ 'listening_to_input' ] ) ) {
update_post_meta( $post_ID, 'ListeningToInput', esc_html( $_POST[ 'listening_to_input' ] ) );
};
}; ?>
Then to display on the front end, we would use the following:
<?php echo get_post_meta( $post->ID, 'ListeningToInput', true ); ?>
Learn more
Learn more about the Custom Meta Boxes # https://developer.wordpress.org/plugins/metadata/custom-meta-boxes/
I am trying to get some product prices to show up in one custom template. Is there, perhaps, an integrated woocommerce function that given ID will give me products price?
Something along the lines of: woocommerce_productprice(423)
Basically, I have a custom page template as a catalog and I want to hook product prices up to that template, so whenever something changes, it automatically changes the price without me having to change the custom template.
Help please!
Updated for WooCommerce 2.3:
$product = we_get_product( $post_id );
echo $product->get_price();
$product = wc_get_product( $product_id );
$price = $product->get_price_html();
woocommerce 3.x, it working!
Here try this
$product = new WC_Product( $post_id );
$price = $product->price;
echo $price;
Actually, there's a typo on the above code, see below:
$product = wc_get_product( $post_id );
echo $product->get_price();