I'm trying to display a custom field on an email.
Here's the script that I thought should work:
<?php if ($day) : ?>
<p><strong><?php _e('day:', 'woocommerce-pip'); ?></strong> <?php echo $order->day; ?></p>
<?php endif; ?>
But it doesn't work. The name of the field is 'day'.
Should this work? What am I missing?
Thanks.
I assume that you have already set correctly this custom field on checkout page with some code that displays the custom field and save the value of this custom field within the order in database (if not, you will get nothing).
You need first to retrieve the order ID before trying to get the value of this custom field and there can be 2 cases:
You have already the $order variable or object. You simply retrieve it with:
$order_id = $order->id;
// or
$order_id = str_replace( '#', '', $order->get_order_number() );
You don't have the $order object, you will need to get it first:
global $woocommerce, $post;
$order = new WC_Order( $post->id );
// and after you can get the order ID
$order_id = $order->id;
// or
$order_id = str_replace( '#', '', $order->get_order_number() );
Now you can get the value and display it because you have $order_id:
<?php $day = get_post_meta( $order_id, 'day', true );
if ( !empty( $day ) ) : ?>
<p><strong><?php _e('day:', 'woocommerce-pip'); ?></strong> <?php echo $day; ?></p>
<?php endif; ?>
But you should check in your database within the order_id as post_id in wp_postmeta table, that a meta_value => 'day' exist with some value in the corresponding meta_value row/column. If not, there is certainly a problem with your code when you create this custom field and save the value on checkout page.
For display custom field like this
<?php echo get_post_meta( get_the_ID(), ‘tes_custom_field’, true ); ?>
for more information check this vary good tutorial http://www.themelocation.com/how-to-display-custom-field-value-on-product-page-in-woocommerce/
Related
I'm trying to add a new admin column to my custom post type using the content of a custom field (ACF). The field I want to add is a 'post object' field, but it just displays the post title instead of the linked post from the ACF. I added a screenshot.
This is what I have so far:
function add_new_realisaties_column($columns) {
$columns['realisatie_line'] = 'Line';
return $columns;
}
add_filter('manage_realisaties_posts_columns', 'add_new_realisaties_column');
function add_new_realisaties_admin_column_show_value( $column, $post_id ) {
if ($column == 'realisatie_line') {
$post_object = get_field('realisatie_line');
if( $post_object ):
// override $post
$post = $post_object;
setup_postdata( $post );
$evdate = the_title();
wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif;
echo $evdate;
}
}
add_filter('manage_realisaties_posts_custom_column', 'add_new_realisaties_admin_column_show_value', 10, 2);
/* Make the column sortable */
function set_custom_realisaties_sortable_columns( $columns ) {
$columns['realisatie_line'] = 'realisatie_line';
return $columns;
}
add_filter( 'manage_edit-realisaties_sortable_columns', 'set_custom_realisaties_sortable_columns' );
function realisaties_custom_orderby( $query ) {
if ( ! is_admin() )
return;
$orderby = $query->get('orderby');
if ( 'realisatie_line' == $orderby ) {
$query->set( 'meta_key', 'realisatie_line' );
$query->set( 'orderby', 'meta_value' );
}
}
add_action( 'pre_get_posts', 'realisaties_custom_orderby' );
There's a couple things I notice. One is that you shouldn't actually need to use the setup_postdata() function, because the ACF Post Object field uses get_post() which returns a full object already. You'd only do this if you're intended to override the global $post object, say on a single-{post_type}.php template, for instance.
Another thing is that it's generally more common to use a switch statement instead of an if/else for post columns. A bit pedantic, but something to note.
Lastly, the_title() will echo the title by default, so assigning it, and then echoing it later, can cause issues (namely leaving variables littered around the document). Consider using get_the_title() if you plan on assigning it to a variable. Also, I won't go into excruciating detail, but just using setup_postdata may not be enough to get the post helper functions to pull the data from where you want.
Now, with all that said, you should be able to just echo the post_title field of the $post_object from get_field(), since it returns a full formed WP_Post object. I put this on a test site of mine and it worked just as intended:
add_filter('manage_realisaties_posts_custom_column', 'add_new_realisaties_admin_column_show_value', 10, 2);
function add_new_realisaties_admin_column_show_value( $column, $post_id ){
switch( $column ){
case 'realisatie_line':
if( $post_object = get_field('realisatie_line') ){
echo $post_object->post_title;
}
break;
}
}
And here's what it looks like in the admin, note I just grabbed a random post for the Post Object relational field:
Try changing your add_new_realisaties_admin_column_show_value function to the code below. If the ACF field name is realisatie_line you are going to need to also pass a $post_id to get the specific meta data back for each particular post.
function add_new_realisaties_admin_column_show_value( $column, $post_id ) {
//Try using a switch/case for the column name
switch ( $column ) {
case 'realisatie_line': //Name of new column from add_new_realisaties_column function
echo get_the_title( get_post_meta( $post_id, 'realisatie_line', true ) ); //Getting the ACF post meta using the $post_id, passing it through the get_the_title function to get title of attached post
break;
default:
break;
}
}
I'm trying to set a custom field featured_image with ACF and can't access it within my php code. First are screenshots of confirming adding the field to the latest post, then after is the php code.
I'm very new to WordPress so I'm expecting this to be a trivial misunderstanding.
I've run var_dump( get_post_meta(get_the_ID()) ); which doesn't show the existence of the 'featured_image' field. I've also created a text custom field, which also doesn't show up.
<?php $my_query = new WP_Query( 'cat=2&posts_per_page=3' );
while ( $my_query->have_posts() ) : $my_query->the_post();
// some variable code
?>
<div class="section-info background3">
<div class="section-details">
// some irrelevant html
<?php
$image = get_field('featured_image');
var_dump( $image );
echo $image;
$featured_image = the_field('featured_image');
var_dump( $featured_image );
$size = 'full'; // (thumbnail, medium, large, full or custom size)
if( $image ) {
echo wp_get_attachment_image( $image, $size );
}
?>
</div>
</div>
<?php endwhile; ?>
Which outputs
bool(false) for var_dump( $image );
NULL for var_dump( $featured_image );
Any help is greatly appreciated. Thanks in advance.
First strategy I'd go for in this case is to confirm that the data is present at the DB level.
Run a query against your DB:
SELECT * FROM `wp_postmeta` WHERE `post_id` = YOUR_POST_ID AND `meta_key` LIKE '%featured_image%'
If you can confirm that the data's there, then the next thing to do is ensure that you're passing the right post_id to get_field.
Because you're creating a custom WP_Query, and get_field usually infers ID from the global $post var, there's always potential for confusion. get_field optionally takes a second $post_id parameter.
If you can dump the contents of get_the_ID() inside your custom loop, and confirm that it matches the post whose featured_image field has the data, then send it along in the form of
<?php
$my_query = new WP_Query('cat=2&posts_per_page=3');
while ($my_query->have_posts() ) :
$my_query->the_post();
var_dump(get_the_ID()); // <-- this should match the ID of the post w/ the featured_image
$image = get_field('featured_image', get_the_ID());
var_dump($image);
endwhile;
wp_reset_postdata(); // always good practice to reset the globals after a custom q!
I'm using Woocommerce Bookings with only a base cost filled. No other rules. How do I display product price in a loop like this?
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => -1 );
$loop = new WP_Query( $args );
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
ID is: <?php the_ID(); ?>
Title is: <?php the_title(); ?>
Price: <?php echo esc_html( get_post_meta( get_the_ID(), '_regular_price', true ) ); ?>
<?php endwhile; wp_reset_query(); ?>
The price field didn't show anything
UPDATE
another code I tried:
<?php
global $woocommerce;
$product = new WC_Product_Booking($productID);
$base_price = $product->get_price();
$product_data = $product->get_data();
$product_pricing = get_post_meta( $product_id, '_wc_booking_pricing', true);
?>
<?php echo $product_pricing; ?>
<?php echo $base_price;?>
both also return the value of zero
ANOTHER TRY
<?php
global $woocommerce;
$product = new WC_Product_Booking($productID);
$product = wc_get_product( $product_id );
$base_price = $product->get_price();
$product_data = $product->get_data();
$product_pricing = $product_data['pricing'];
foreach($product_pricing as $key => $princing ){
$pricing_type = $princing['type'];
$pricing_base_cost = $princing['base_cost'];
$pricing_base_modifier = $princing['base_modifier'];
$pricing_cost = $princing['cost'];
$pricing_modifier = $princing['modifier'];
$pricing_from = $princing['from'];
$pricing_to = $princing['to'];
}
$pricing_data = get_post_meta( $product_id, '_wc_booking_pricing', false); ?>
<?php echo $pricing_data; ?>
nothing works :( they're all either showing blanks or showing zero.
Can anyone help point in the right direction?
many thanks
Try (from the docs):
$product = new WC_Product_Booking($productID);
//don't know if you need 'echo'
echo $product->get_price_html()
If not, I think the logic of 2nd snippet it's ok, but probably productID != postID,
so another solution would be:
find the postID for that productID
call get_post_meta( postID, '_wc_booking_pricing', true);
print the price
EDIT
My answer's too long for being posted in the comments.
Are you sure that you are set correctly the product from wp-admin?
In order to do the second suggestion, you need access to the database.
Get the ID of the post(product) in wp-admin when you create/modify it, you should have in the url something like {URL}/wp-admin/id=123.
Once you have the ID go in wp_post_meta table in the DB and look for all the fields with that post_id, and check if you can find the correct price.
Once you find it, you can call get_post_meta(correctID,correctKey)
It seems like you're pulling the right meta key, could be related with you using esc_url(), when you should be using just esc_html(). I'd try that.
I have a custom page template with certain custom fields. I want to display these custom fields outside the loop, but within the same page.
This one works:
<?php echo get_post_meta( '244', 'custom_field_name', true ) ?>
But I want to to work dynamically, without me entering the actual ID of the page.
How can I call the page ID in the echo?
Try this :
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'Your-Custom-Field', true);
wp_reset_query();
?>
If this call is in the loop, replace the id with function get_the_ID, this function retrieves the ID of the current item in the WordPress Loop.
<?php echo get_post_meta( get_the_ID(), 'custom_field_name', true ) ?>
See: https://developer.wordpress.org/reference/functions/get_the_ID/
If this call is in the single page, replace the id with object item $post->ID.
$post = get_post();
<?php echo get_post_meta( $post->ID, 'custom_field_name', true ) ?>
See: https://codex.wordpress.org/Class_Reference/WP_Post
Also, you can get the access to the post via global variable $post.
$post = get_post();
<?php echo get_post_meta( $post->ID, 'custom_field_name', true ) ?>
See: https://codex.wordpress.org/Global_Variables
ok, here's what I'm trying to do:
I've got a custom post type called drinks-menu, a taxonomy called drinks-menu-categories, and a page template called template-drinks-menu.php.
The taxonomy has a bunch of terms that are heirarchical - Wine, with children White and Red; Beer, with children Pilsener, Stout, etc...
I want to use one loop to display all the posts from these terms in the same order that they're ordered by in the admin. Here's the code I've got so far:
<?php
$post_type = 'drinks-menu';
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( (object) array('post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) :
echo '<h1>'.$term->name.'</h1>';
if ( $term->description !== '' ) {
echo '<div class="page_summary">'. $term->description .'</div>';
}
echo '<br>';
$posts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=-1&orderby=id&order=DESC" );
if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post();
?>
<?php the_title(); ?>
<?php
if( get_field( "price" ) ): ?>
<?php echo '<span">'; the_field( "price" ); echo ' | '; the_field( "abv" ); echo '</span>';?>
<?php endif;
echo '<em>'; the_excerpt(); echo '</em><br><br>';
endwhile; endif;
endforeach;
endforeach;
?>
This is working well to bring all the posts from the terms onto the page, but not in order. You can see I tried taxonomy=$taxonomy&term=$term-slug&posts_per_page=-1&orderby=id&order=DESC but it's not working, everything shows in alphabetical order.
Any Wordpress gurus who can point me in the right direction? I hope I've been clear about the issue. Thanks :-)
Posts ordered in admin page by "menu_order";
If u want to order posts using query_posts (WP_Query constructor) u should use value of variable "orderby" in upper case -> "ID".