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

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.

Related

AJAX - How to update cart count and check for status changes every 5 minutes

So I have 2 similar questions (both related to Ajax, not too familiar with it)
The first is, I have an AJAX updating my cart in Woocommerce. I have 2 "mini-carts" - one for desktop and a different one for mobile. The one for desktop is functioning perfectly. The cart count doesn't show until there is a product in it, and once AJAX adds a product the count shows.
The mobile one is a bit different. It is updating fine and the count shows, but the page needs to be refreshed in order to see the count. I know the issue is due to the IF statement, but I can't figure out how to show the count if something is added via AJAX, without a page refresh (I hope that makes sense).
Below is the code in my footer.php file
<a class="col" data-toggle="modal" data-target="#mobileAccountModal">
<img src="/img/cart-icon-mobile.svg" />
CART
</a>
<?php
if ( is_woocommerce_activated() ) {
$count = WC()->cart->cart_contents_count; ?>
<?php if ( $count > 0 ) { ?>
<span class="cart-contents-count" id="test-cart"><?php
$cart_count = WC()->cart->get_cart_contents_count();
echo sprintf ( _n( '%d', '%d', $cart_count ), $cart_count );
?></span>
<?php } ?>
<?php } ?>
And below the functions.php file.
add_filter( 'woocommerce_add_to_cart_fragments', 'refresh_cart_count', 50, 1 );
function refresh_cart_count( $fragments ){
ob_start();
?>
<span class="cart-contents-count" id="test-cart"><?php
$cart_count = WC()->cart->get_cart_contents_count();
echo sprintf ( _n( '%d', '%d', $cart_count ), $cart_count );
?></span>
<?php
$fragments['#test-cart'] = ob_get_clean();
return $fragments;
}
The second question is very similar. I have a section that displays ORDER STATUS on the front-end and displays different HTML (div with the message) depending on the status. This is working fine, however, the page needs to be refreshed in order to see the updates.
Is there a way I can check for status updates via AJAX every 5 minutes? TIA

Woocommerce custom shortcode returns zero value

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' );

How can I show total price at checkout page [OpenCart]

I would like to make some calculations at the checkout page. If for example the total price of all products is 80€, I want to show a message that says: "You can add more products on cart worth of 20€, so you can get a discount which activates when total cost is 100€".
To do this I have to get the total price as a variable and do this:
$tp = totalprice;
if ( $tp < 100 ) {
$tp_less = 100 - $tp;
}
echo 'You can add more products on cart worth of ' . $tp_less . '€.';
The problem here is that I don't know how to get the total price as a variable.
<?php echo $cart_total = $this->cart->getTotal(); ?>
I placed the above at the file /checkout.tpl but I get an error so it's not working. Actually, whenever I use any code that has 'this' inside it I get error:
"Notice: Undefined property: Template\Basic::$cart"
$this->cart->getTotal() - Calculates cart total including taxes
$this->cart->getSubTotal() - This calculates products subtotal, I think you need to use this.
$this->cart->getSubTotal() - call this function in your controller and store value in a variable and pass it to your tpl file.
Regarding your error:
By default cart library is included in opencart, Make sure cart library is included, check registry file
Hope this helps.
All I had to do was:
At file checkout.php
$data['total'] = $this->cart->getTotal();
At file checkout.tpl
echo $total

WooCommerce "CONTACT" button when out of stock

Trying to achieve something that should be simple, but I've tried 3 approaches with multiple code variations and I just can't make it work. I'm trying to create a button that will appear in place of the "ADD TO CART" button on single product pages when the item is out of stock. Clicking the button will fire a popup contact form.
Is creating an add action in functions the right way to go, or should I replace the normal button with an if statement? I've tried both, so help with coding either would be greatly appreciated.
You can either hook into woocommerce_loop_add_to_cart_args using a filter in your functions.php or edit the template file directly by pulling it into your theme. Either way will require a bit of PHP.
If doing it in your functions.php, it would look something like this (untested but should send you down the right path):
<?php
add_filter( 'woocommerce_loop_add_to_cart_link', 'my_out_of_stock_button' );
function my_out_of_stock_button( $args ){
global $product;
if( $product && !$product->is_in_stock() ){
return 'Contact us';
}
return $args;
}
I don't know what your button code should actually look like or what other information you need to capture, but this is how you could override the "Add to Cart" button and replace it if out of stock.
UPDATE
LoicTheAztec brought up a great point - the filter provided only affects the button on the archive, category, tag overview pages - not the individual product pages. There are no hooks for the individual product page buttons BUT you can copy the templates to your theme and override them.
You'll want to look at the files in templates/single-product/add-to-cart. Use a similar if statement as above:
#simple.php
<?php if ( $product->is_in_stock() ) : ?>
// Standard WooCommerce code
<?php else: ?>
// Your button code
<?php endif; ?>
Just add below code in functions.php file of your enabled theme reference
add_action('woocommerce_after_shop_loop_item', 'themelocation_change_outofstock_to_contact_us', 1);
// for shop page
function themelocation_change_outofstock_to_contact_us() {
global $product;
if (!$product->is_in_stock()) {
remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart');
//change the link to your contact us page
echo ' Contact Us ';
}
}
// for single page
add_filter('woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability($availability, $_product) {
// Change In Stock Text
if ($_product->is_in_stock()) {
$availability['availability'] = __('Available!', 'woocommerce');
}
// Change Out of Stock Text
if (!$_product->is_in_stock()) {
$availability['availability'] = __(' Contact Us ', 'woocommerce');
}
return $availability;
}
I was looking for a way to show a contact button on bespoke products and
#ahwychkchih solution works great. One issue I had though is that schema markup will show as out of stock for those products which is not the case for beskpoke products is just they can't be purchased straight away so I've added this to force in_stock markup for my products. I'm aware that this solution would affect all products so you can always add a product id filter if needed
// Force In Stock schema markup
function fix_my_product_offers_schema ($markup_offer, $product) {
if (!$product->is_in_stock()) {
$markup_offer['availability'] = 'https://schema.org/InStock';
}
return $markup_offer;
}
add_filter('woocommerce_structured_data_product_offer', 'fix_my_product_offers_schema', 1, 2);

Display WooCommerce variable product price on other pages

I'm using WooCommerce on a WordPress site that I'm building and I need to be able to display a specific product's price throughout the site. Normally that wouldn't be an issue, but in this instance it's a product which has 2 variations, so I need to show both of them (e.g. £4.99 - £9.99). How can I retrieve these values and echo them out?
Put the following in your theme's functions.php file:
function so_28073705( $product_id ) {
$wc_product_variable = new WC_Product_Variable( $product_id );
$variation_price_html = $wc_product_variable->get_price_html( );
return $variation_price_html;
}
When you want to use it:
<?php echo so_28073705( <product_id> ); ?>
returns:
<span class="amount">$low-price</span>–<span class="amount">$high-price</span>

Categories