Woocommerce custom shortcode returns zero value - php

I have created a custom Thank you page in wordpress which has this paragraph:
Thank you so much for your order!
After paying off [cart_total] via XYZ website, please fill out
this form and inform us about your payment.
And tried this custom shortcode:
// Total Price Shortcode
function cart_wctotal(){
global $woocommerce;
$wctotal = $woocommerce->cart->get_cart_total();
return "<span class='cart-total'> ".$wctotal."</span>";
}
add_shortcode( 'cart_total', 'cart_wctotal' );
But when I check out orders the output returns zero value for Total Price :
Thank you so much for your order!
After paying €0.00 via XYZ website, please fill out this form and
inform us about your payment.

See comment from #xhynk why your code is not going to work.
Get last order (and order total) by user id
function cart_wctotal(){
// Get user id
$user_id = get_current_user_id();
// Get last order by user id
$last_order = wc_get_customer_last_order( $user_id );
// Order total
$wctotal = $last_order->get_total();
return "<span class='cart-total'> " . $wctotal . "</span>";
}
add_shortcode( 'cart_total', 'cart_wctotal' );

Related

How to remove URL from WooCommerce My account order number

I want to remove the URL from the WooCommerce My account Orders page and I really don't know how to do it by editing functions.php. Does anyone have any suggestions? Thank you!
order details url
I tried some of the answers I found on StackOverflow but I doesn't seem to work.
Remove the existing order-number column using the first hook as it prints the order number along with the link.
Add a custom order order-number column to print as per your requirement using the second hook
function remove_defualt_order_number_column($columns) {
unset($columns['order-number']);
$custom_order_number = array('order-number-custom' => __('Order', 'woocommerce'));
$columns = array_merge($custom_order_number, $columns);
return $columns;
}
add_filter('woocommerce_account_orders_columns', 'remove_defualt_order_number_column', 10, 1);
add_action('woocommerce_my_account_my_orders_column_order-number-custom', 'add_custom_order_number_column', 10, 1);
function add_custom_order_number_column($order) {
echo esc_html(_x('#', 'hash before order number', 'woocommerce') . $order->get_order_number());
}

How do I add order note on (advanced) custom fields update?

I'd like to add an order note to log the time I added tracking number to existing order. I'm using Advanced Custom Fields to store trackings. Tried function below, and it breaks my site:
function tt_add_tracking_note( $order ) {
$tt_order = $order->id;
if (get_field('tt-track', $tt_order)) {
$tt_tracknum_note = 'Added tracking number ' . the_field('tt-track', $tt_order);
}
$tt_order->add_order_note( $tt_tracknum_note );
}
add_action( 'woocommerce_process_shop_order_meta', 'tt_add_tracking_note', 10, 2 );
What is wrong and how do I add a note the right way?

WooCommerce Wordpress can't find total order after checkout/pyament

I use a custom payment, which I display in an iframe and pass on variables into a form.
I need to pass the total order without currency sign after checkout/payment.
I've found a way to pass the cart total before checkout/payment using
echo WC()->cart->total
On the checkout after payment page (thankyou.php) I've found
echo $order->get_formatted_order_total();
This will display the total order with a currency sign only on the thankyou.php page and will not pass onto any other page no matter what i tried (which isn't important as I can work with ether thankyou.php page or actual checkout page)
Full code looks like this inside a list
<?php _e( 'Total:', 'woocommerce' ); ?>
<strong><?php echo $order->get_formatted_order_total(); ?>
I've tried testing to see how wordpress stores order total both on thankyou.php page and on a seperate page to see if I can call the order total from the array after checkout/payment but I ether get a value of 0.00 or my page breaks
Code I tried so far:
echo $order->get_order_total();
this breaks the page
echo WC()->$order->get_order_total();
this breaks the page
echo $order->get_total();
this also breaks the page
calling total cart echo WC()->cart->total after checkout/payment will display 0 (which makes sense)
If it's the thank you page, then it's simple enough - you just need to use this and it should work without breaking the page.
In your templates/checkout/thankyou.php file, add this code:
if ( ! $order) {
echo 'No order object!';
} else if ($order->has_status( 'failed' ) {
echo 'Order failed for some reason.';
} else {
echo $order->get_total();
}
If you don't have the $order object, you can get it from the order id:
$order = wc_get_order( $order_id );
$order->get_total();
If you don't have the order id OR the $order object available, then we need more information to be able to help.

Add a new field to woocommerce_email_customer_details or any other part of emails

Ok so I am building a woocommerce site for a client and they love it but the only issue is they need a customer number to come through with the email for all new orders. They are willing to go in and fill in this info for each user, but I cannot find out how to get this info to automatically send with the new order email.
I am using WP - Members plugin to add custom information to the users profile, and have created a field there called Customer # (option name: customer_number) I cannot figure how to get woocommerce to add this number to the email that is sent when they place the order (to the admin)
I have tried many different things, it seems like it should just be a hook but I dont know how to get the values or have not been able to figure this out yet in any way.
I have looked and looked and this is my last resort, my minimal understanding of php and hooks is probably my downfall here, so please be very clear if instructing me in that way.
I will not give up on this issue, so I hope someone can help!
We can adapt from my tutorial on customizing the checkout fields to display some extra info in the email:
function kia_display_email_order_user_meta( $order, $sent_to_admin, $plain_text ) {
$user_id = $order->customer_user;
if( ! empty( $user_id ) && ( $data = get_user_meta( $user_id, '_some_user_meta', true ) ) != '' ){
if( $plain_text ){
echo 'The user data for some field is ' . $data;
} else {
echo '<p>The user data for <strong>some field</strong> is ' . $data . '</p>';
}
}
}
add_action('woocommerce_email_customer_details', 'kia_display_email_order_user_meta', 30, 3 );
In this case we'll replace the get_post_meta() of my tutorial with get_user_meta().... and knowing that the user Id is stored in the $order->customer_id property of the $order object.

WP Ecommerce: Display the "Order Notes" in Sales Log

I have the following function that adds a column in the Sales Log so that I may show the Order Notes. Adding the column is good and showing the id of each item ($item->id) as test is good. Where I am getting stuck is how to display the actual order notes for each item.
Tried numerous things including ($purchlog_notes->notes);
This adds the column (all good)
`function addPurchaseLogColumnHead( $columns ){
$columns['OrderNotes']=__('Order Notes');
return $columns;
}
add_filter( 'manage_dashboard_page_wpsc-purchase-logs_columns', 'addPurchaseLogColumnHead');`
This is where I want to show Order Notes content for each sale (issue)
`function addPurchaseLogColumnContent( $default, $column_name, $item ) {
if ($column_name == 'OrderNotes'){
echo $item->notes;
}
}
add_filter( 'wpsc_manage_purchase_logs_custom_column' , 'addPurchaseLogColumnContent', 10, 3);`
Appreciate any ideas.
Thank you.

Categories