Get the product price in Woocommerce 3 - php

I'm trying to get price without currency in a function I made.
function add_price_widget()
{
global $woocommerce;
$product = new WC_Product(get_the_ID());
$thePrice = $product->get_price_html();
echo thePrice;
}
Displays: 100kr
How do I get it to just give me the price 100

What #Syntax_Error had said is correct you have to use
get_price(), WooCOmmerce also provide a wrapper function
wc_get_product() for WC_Product class.
So your function would look something like this:
function add_price_widget()
{
$product = wc_get_product(get_the_ID());
$thePrice = $product->get_price(); //will give raw price
echo $thePrice;
}
Hope this helps!

You can just use the function get_price that returns only the number (without dots or symbol)
function add_price_widget() {
global $woocommerce;
$product = new WC_Product(get_the_ID());
$thePrice = $product->get_price();
echo thePrice;
}
I just tested it in my site and it work. So it should work for you too.

Related

How to get current order info on the thank you page using shortcode

I'm trying to introduce a shortcode on the thankyou.php page to show the details of the order just placed by a customer.
If I write the code in php like this it works and shows the total:
<?php echo $order->get_total(); ?>
Now I'm trying to get the same result through a shortcode, but I don't know some parameters and therefore can't get it to work.
<?php
add_shortcode( 'custom-woocommerce-total' , 'custom_total' );
function custom_total(){
$customer_id = get_current_user_id();
$order = new WC_Order($order_id); // I suppose that's not right.
return $order->get_total();
} ?>
can anyone help me understand what I'm doing wrong?
You would need to get the order id first by using global $wp variable. Try this:
add_shortcode('custom-woocommerce-total', 'custom_total');
function custom_total($attr)
{
if (is_wc_endpoint_url('order-received'))
{
global $wp;
$order_id = absint($wp->query_vars['order-received']);
if ($order_id)
{
$order = new WC_Order($order_id);
if($order)
{
return $order->get_total();
}
}
}
}
And in the thankyou page template use it like this:
echo do_shortcode('[custom-woocommerce-total]');
Don't forget to override the template.

Custom function that get product reviews count in WooCommerce

I'm trying to make a function that get the count of product reviews in WooCommerce product page.
I need to use it in another function in a logical operation ... Can't figure out whats wrong.
function reviews_count() {
$id = $product->get_id();
$product = wc_get_product($id);
$count = $product->get_review_count();
return $count;
}
Try the following instead (for single product_pages):
function reviews_count() {
global $product;
if ( ! is_a( $product, 'WC_Product' ) ) {
$product = wc_get_product( get_the_ID() );
}
return $product->get_review_count();
}
Or you can also add the product Id as function argument (to use it in another function) like:
function reviews_count( $product_id ) {
$product = wc_get_product( $product_id );
return $product->get_review_count();
}
So in your other function, you will be able to pass the product Id a bit like:
function my_other_function() {
global $product;
$count = reviews_count( $product->get_id() );
}

Get the customer ID from an order ID in WooCommerce

I want to get the "mycred" balance of a customer through the order while using WP ALL Export to export the customer balance based on orders to a spreadsheet. It's actually probably quite simple. I'm able to get the Order ID, but not the Customer ID
Here is what I'm doing to test if I can get the customer ID:
function get_customeruserid($value)
{
global $woocommerce, $post;
$order = new WC_Order($post->ID);
$order_id = $order->get_order_number();
$customer = new WC_Customer($post->ID);
$user_id = $customer->get_ID();
$value = $user_id;
return $value;
}
This returns a 0.
However, I can get the order number easily enough by doing this:
function get_customerorderid($value)
{
global $woocommerce, $post;
$order = new WC_Order($post->ID);
$order_id = $order->get_order_number();
$value = $order_id;
return $value;
}
This returns the customer's order number which is great, but only half the battle. I now want the Customer ID so I call call the mycred balance function to show their balance.
Any ideas? I'm a newbie at php and probably very bad.
To get the User ID from the Order ID, you can use many ways, Here are 2 of them:
In WooCommerce 3.0+ you can use WC_Order Class methods this way:
function get_customerorderid(){
global $order, $post;
if( ! is_a($order, 'WC_Order') ) {
$order_id = $post->ID;
// Get an instance of the WC_Order object
$order = wc_get_order($order_id);
} else {
$order_id = $order->id;
}
// Get the user ID from WC_Order methods
$user_id = $order->get_user_id(); // or $order->get_customer_id();
return $user_id;
}
Before WooCommerce 3.0 version, you can use get_post_meta() function this way:
function get_customerorderid(){
global $order, $post;
if( ! is_a($order, 'WC_Order') ) {
$order_id = $post->ID;
} else {
$order_id = $order->id;
}
// Get the user ID
$user_id = get_post_meta($order_id, '_customer_user', true);
return $user_id;
}
For those who want to specifically add the customer mycred balance from an ORDER into the CSV sheet within WP All Export here is the bit of code I used.
Thank you for your help getting it solved.
While editing an ORDER export in WP ALL EXPORT, add a new data object and click on it and "Export the value returned by a PHP function" then add the following function in the code editor:
function all_export_mycred($balance)
{
global $woocommerce, $post;
$order = new WC_Order($post->ID);
$user_id = $order->get_user_id( );
$balance = mycred_get_users_balance( $user_id );
return $balance;
}
Then make sure to add the "all_export_mycred" to the php return field.

Conditionally making WooCommerce product non purchasable

I've been struggling with a snippet for WooCommerce that I'd like to add to my child's theme functions but I am reaching the limits of my PHP knowledge.
I have set a custom attribute called pdt_volume, that I will fill everytime I add a product. I would like to remove the Add_to_cart button when the sum of this attribute for all the items already in the cart have reach a max_volume limit.
For example, the Max_volume is 100, I have 4 products of pdt_volume = 20, therefore, the add_to_cart button for a product with a pdt_volume of 25 should be removed so that I cannot add it to the cart.
So I have come up with this snippet, with bits of code found here and there.
But the functions.php won't save it, and others variations of this code has succeeded to register in the functions.php but the website would then give a 500 error...
Please, does someone has any idea how to achieve this?
What am I doing wrong?
Thanks.
EDIT 1 : Alright, so I got this code to actually be registered in the functions.php by the editor without breaking, but on the site, I still get the internal server error. Like something is not computing or something.
EDIT 2 (29/01/2017)
I used the ACF plugin I had purchased a long time ago but didn't find any suitable purpose... so far.
Here is the working code I was able to come up with. This is not a code masterpiece and I won't get any award for this, but it seems to be working so far. At least, it allows me to get a TRUE/FALSE statement that I can use in a if condition to change the add_to_cart button to a Your_box_is_full button.
Indeed, I didn't need any global $woocommerce or $product !
function get_cart_volume() {
$cart_volume = 0;
foreach( WC()->cart->get_cart() as $cart_item ) {
$item_id = $cart_item['product_id'];
$item_volume = get_field('product_volume', $item_id);
$item_qty = $cart_item['quantity'];
$vol_by_qty = $item_volume * $item_qty;
$cart_volume += $vol_by_qty;
}
return $cart_volume;
}
function check_if_full($candidate) {
$max_volume = 100;
$candidate = $product->id;
$a_volume = get_field('product_volume', $candidate);
$b_volume = get_cart_volume();
$check_volume = $a_volume + $b_volume;
if ($check_volume > $max_volume)
return true;
else
return false;
}
//Just a function to see if it's working on the cart page fur debugging purpose
add_action( 'woocommerce_check_cart_items', 'current_volume');
function current_volume() {
if (is_cart() || is_checkout()) {
global $woocommerce;
$current_volume = get_cart_volume();
wc_add_notice( sprintf( '<strong>Volume is %s.</strong>', $current_volume ),
'error' );
}
}
As Helgatheviking says, you get already, with woocommerce_is_purchasable hook in your hooked function, the $product object as 2nd argument. So you don't need and you have to remove global $woocommerce, $product; to make it work without throwing an error.
Your code is going to be:
add_filter('woocommerce_is_purchasable', 'if_room_is_purchasable', 10, 2);
function if_room_is_purchasable ($is_purchasable, $product){
$cart_volume = 0;
$max_volume = 100;
foreach( WC()->cart->get_cart() as $cart_item ){
$item_id = $cart_item['product_id'];
$terms = get_the_terms( $item_id, 'pa_pdt_volume');
foreach($terms as $key => $term){
$cart_volume += $term->name; // May be better with $term->slug;
}
}
$candidate_volume = $cart_volume + $product->get_attribute( 'pa_pdt_volume' );
if ( $candidate_volume > $max_volume )
return false;
else
return true;
}
This should work now without error.

WooCommerce - get_order() is not working

I'm trying to create a function that will retrieve an order by its ID. For some reason I can't get the WooCommerce global function get_order to work. I'm passing a valid order id to the function and trying to print it out to verify that it's working. The function has been placed in my functions.php file.
function getWC_order_details($id){
global $woocommerce;
$order = get_order( $id );
print "<pre>";
print_r($order);
print "</pre>";
}
I have tested echoing other data out of the function without a problem.
First of all make function like this :
function getWC_order_details($order_id) {
$order = new WC_Order( $order_id );
var_dump($order);
}
After that, use it with some woo_commerce action or filter.
function use_after_cart_table(){
getWC_order_details(40);
}
add_action( 'woocommerce_after_cart_table', 'use_after_cart_table' );
So after adding any product to the cart, you will see after cart table that there is one array containing all the details.
NOTE : You can use any other action or filter and you can find them here.
EDITED:
function getWC_order_details($order_id) {
$order = new WC_Order( $order_id );
//var_dump($order);
$order_shipping_total = $order->get_shipping();
$order_shipping_method = $order->get_shipping_methods();
var_dump($order_shipping_total);//Use it for debugging purpose or to see details in that array
var_dump($order_shipping_method);//Use it for debugging purpose or to see details in that array
$_order = $order->get_items(); //to get info about product
foreach($_order as $order_product_detail){
//var_dump($order_product_detail);
echo "<b>Product ID:</b> ".$order_product_detail['product_id']."<br>";
echo "<b>Product Name:</b> ".$order_product_detail['name']."<br><br>";
}
//var_dump($_order);
}
Try this. It might be useful to you.
function getWC_order_details($id)
{
$array = WC_API_Orders::get_order( $id, $fields );
print "<pre>";
print_r($order);
print "</pre>";
}
Source:
File name: woocommerce/includes/api/class-wc-api-orders.php

Categories