Show price in Woocommerce product custom loop - php

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.

Related

Woocommerce php array select dropdown only shows 10 results

I have this product dropdown for Woocommerce.
Problem is: it does only display 10 product..
I would like it to show all of my products!
Can anyone guide me to the problem :-) ?
Dropdown:
<option value=""> - Select poster - </option>';
$args = array( 'post_type' => 'product' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) :
$loop->the_post();
$post_id = $loop->post->ID;
$product = wc_get_product($post_id);
$image_id = $product->get_image_id();
$image_url = wp_get_attachment_image_url( $image_id, 'full' );
$title = $product->get_name();
$permalink = $product->get_permalink();
echo '<option value="'.$image_url.'" producturl="'.$permalink.'".>'.$title.'</option>';
endwhile;
// Reset post data
wp_reset_postdata();
echo'</select>
Note: A jQuery scripts sorts the dropdown alphabetically, and another script makes next and previous buttons for the dropdown - but i guess thats not the problem..
solved: just WP settings -> max post pr page was set to 10

Echo a custom page's custom field outside the loop

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

Calling and displaying a WooCommerce custom checkout field value

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/

Sum of all custom fields for category

I've got a custom field on posts called number.
On the front-end in category.php, I need to total up the value of all posts in the current category that have number.
So for example, if the current category had 3 posts, and in each post the number values were 1, 5 and 10 respectively, then on the front-end it needs to display the total 16.
I'm not entirely sure where to start with this.
The loop I have at the moment:
<?php if ( have_posts() ) : ?>
<h1><?php printf( __( 'Category Archives: %s', 'twentythirteen' ), single_cat_title( '', false ) ); ?></h1>
<p></p>
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php else : ?>
<?php // ?>
<?php endif; ?>
Any help is appreciated.
Thanks everyone. There was a lot there that helped me arrive at this solution (and a little help from Google):
<?php
$args = array(
'numberposts' => -1,
'offset' => 0,
'category' => $category
);
$numbers = get_posts( $args );
$total = 0;
foreach( $numbers as $numbersID ) {
$single = get_post_meta( $numbersID->ID, 'numbers', true );
$total += $single;
}
echo $total;
?>
The key was knowing to increment a variable += as #rnevius suggested, which I didn't know about.
A shorter version of what you're trying to achieve (which also reduces calls to the database) would look something like the following:
<p>
<?php
$total = 0;
foreach( $wp_query->posts as $number ) {
$total += get_post_meta( $number->ID, 'numbers', true );
}
echo $total;
?>
</p>
As mentioned in another comment, you don't need another loop, as your posts already exist in $wp_query.
Supposing you're using MySQL use SUM() Method to achive your goals. Query might look something like SELECT SUM(number) FROM category WHERE [drill down to a specific set of categories if neccesary];

WooCommerce return product object by id

I am creating a custom theme for woocommerce and I need to be able to create a mini product display. I am having problems finding documentation on the woocommerce api. I have a comma delimited list of product IDs that I need to iterate through and display a custom mini product display for each in sequence.
$key_values = get_post_custom_values('rel_products_ids');
//get comma delimited list from product
$rel_product_ids = explode(",", trim($key_values, ","));
// create array of just the product ids
foreach ( $rel_product_ids as $pid ) {
//sequentially get each id and do something with it
$loop = new WP_Query( array( 'post__in' => $pid ) );
// also tried ...
//$loop = new WP_Query( array( 'ID' => $pid ) );
while ( $loop->have_posts() ) : $loop->the_post(); $_product = &new WC_Product( $loop->post->ID );
//do stuff here I have stripped the html in favor of getting to the meat of the issue
woocommerce_show_product_sale_flash( $post, $_product );
if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_single');
get_permalink( $loop->post->ID );
the_title();
$_product->get_price_html();
endwhile;
}
Any help would be appreciated.
Thank you,
Tim
Use this method:
$_product = wc_get_product( $id );
Official API-docs: wc_get_product
Another easy way is to use the WC_Product_Factory class and then call function get_product(ID)
http://docs.woothemes.com/wc-apidocs/source-class-WC_Product_Factory.html#16-63
sample:
// assuming the list of product IDs is are stored in an array called IDs;
$_pf = new WC_Product_Factory();
foreach ($IDs as $id) {
$_product = $_pf->get_product($id);
// from here $_product will be a fully functional WC Product object,
// you can use all functions as listed in their api
}
You can then use all the function calls as listed in their api:
http://docs.woothemes.com/wc-apidocs/class-WC_Product.html
Alright, I deserve to be throttled. definitely an RTM but not for WooCommerce, for Wordpress.
Solution found due to a JOLT cola (all hail JOLT cola).
TASK:
Field named 'related_product_ids' added to a custom post type. So when that post is displayed mini product displays can be displayed with it.
PROBLEM:
Was having a problem getting the multiple ids returned via WP_Query.
SOLUTION:
$related_id_list = get_post_custom_values('related_product_ids');
// Get comma delimited list from current post
$related_product_ids = explode(",", trim($related_id_list[0],','));
// Return an array of the IDs ensure no empty array elements from extra commas
$related_product_post_ids = array( 'post_type' => 'product',
'post__in' => $related_product_ids,
'meta_query'=> array(
array( 'key' => '_visibility',
'value' => array('catalog', 'visible'),'compare' => 'IN'
)
)
);
// Query to get all product posts matching given IDs provided it is a published post
$loop = new WP_Query( $related_posts );
// Execute query
while ( $loop->have_posts() ) : $loop->the_post(); $_product = get_product( $loop->post->ID );
// Do stuff here to display your products
endwhile;
Thank you for anyone who may have spent some time on this.
Tim
global $woocommerce;
var_dump($woocommerce->customer->get_country());
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = new WC_product($cart_item['product_id']);
var_dump($product);
}

Categories