Hide Order Total from Woocommerce Checkout - php

I'm trying to hide the "order-total" section from my checkout if the total is 0.00€ (see [Pic here][1] for reference).
I tried to add a condition to the code (as you can see below) I found suggested here:
How to remove order total from cart and checkout page woocommerce
However when I try it live, it hides the field even if the cart is not 0.00€.
Here's the code:
// On checkout page
add_action( 'woocommerce_checkout_order_review', 'remove_checkout_totals', 1 );
function remove_checkout_totals(){
cart_total = WC()->cart->get_cart_total();
if ( $cart_total == 0 ) {
// Remove cart totals block
remove_action( 'woocommerce_checkout_order_review', 'woocommerce_order_review', 10 );
}
}
What should I do?
Thanks in advance for you help!
24/09: edit after 7uc1f3r's corrections

Related

Disable tax calculation and tax information on a cart page

I would like to disable tax calculation and information on the cart page, to have them shown only on the checkout page.
I tried to disable woocommerce's 'wc_tax_enabled' like below:
if ( class_exists( 'woocommerce') ) {
if ( is_cart() ) {
add_filter( 'wc_tax_enabled', '__return_false' );
}
}
});
The above works at a glance, but when I switch to a different delivery option or select any other available options, the tax calculation is still included in the total price on the cart page. See the image below:
I've also tried to edit total-carts.php to remove the tax information, but it produces a similar result as above.
How can I remove the tax calculation on the cart page entirely (if possible, without editing too much of the source files)?
From an answer to Stack Overflow question How can I remove Shipping from a WooCommerce cart?:
Add the following snippet to your functions.php file:
function disable_shipping_calc_on_cart( $show_shipping ) {
if( is_cart() ) {
return false;
}
return $show_shipping;
}
add_filter( 'woocommerce_cart_ready_to_calc_shipping', 'disable_shipping_calc_on_cart', 99 );

Move selected variation price below product Add-ons form in Woocommerce

I have the official WooCommerce Product Addon's plugin and I am using it on a variable product. however I would like the price to show above the add to cart button instead of below the variation select list as it doesn't now.
I have looked everywhere and I have moved the price for single products to below the short description but can not work out out to do this for variable products with product add-ons.
Screenshot showing current location and desired location:
Any help is greatly appreciated.
WooCommerce Product Addon's plugin displays already a custom pricing table just before add to cart buttons, where the selected variation price is displayed, with the selected option pricing with the subtotal:
You can't only move the displayed selected variation price alone, as it's driven by javascript on live selection event and grouped with variation availability and variation description (if any).
So what you can do is:
1) To move the variation price with its availability and description before add to cart button using:
add_action( 'woocommerce_before_variations_form', 'reposition_display_for_variable_products', 10 );
function reposition_display_for_variable_products() {
global $product;
if( $product_addons = $product->get_meta('_product_addons') ) {
if( sizeof($product_addons) > 0 ) {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation', 10 );
add_action( 'woocommerce_single_variation', 'woocommerce_single_variation', 16 );
}
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
But it will be displayed after the custom "Addon's" pricing table:
2) hide the Woocommerce variation price (as it's already displayed by the custom "Addon's" pricing table)
add_filter( 'woocommerce_available_variation', 'hide_variation_selected_price', 10, 3 );
function hide_variation_selected_price( $data, $product, $variation ) {
if( $product_addons = $product->get_meta('_product_addons') ) {
if( sizeof($product_addons) > 0 ) {
$data['price_html'] = '';
}
}
return $data;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
3) Use both together, moving the variation availability and variation description after "Addon's" pricing table and before add to cart button:
jQuery('.price').insertAfter('.sauce')
a front-end solution (just put the right selectors)
To move the Product Add-Ons Totals placement you can add this to your functions.php file:
if( isset($GLOBALS['Product_Addon_Display']) ){
remove_action( 'woocommerce_product_addons_end', array( $GLOBALS['Product_Addon_Display'], 'totals' ), 10 );
}
And if you want to add it back:
if( isset($GLOBALS['Product_Addon_Display']) ){
add_action( 'some_action_hook', array( $GLOBALS['Product_Addon_Display'], 'totals' ), 10 );
}
You have to make sure of two things though:
You have to pass the id (or some id) as parameter to your new hook.
The JS that generates the totals looks for the #product-addons-total inside some .cart element.

Woocommerce - Disable Add to Cart Conditionally on Custom Field

Everything I can find on removing the WooComm Add To Cart button will remove not just the add to cart button but also the pricing/variations, aka the whole add to cart area.
My goal is to enable/disable the ability to purchase a product with a checkbox/selector on the product info page. BUT I still have to be able to see the product variation pricing and the variation drop down menu.
This is important. The pricing shown under the product title, for a variation, will be something like $20.00 - $40.00 and not until you select the variation choice will it show the price next to the add to cart button.
So far I have things working wherein I can remove the add to cart area — variations and all — conditionally on my custom field, but I have no idea how to hide/disable click/remove just the add to cart button and allow variations to still be chosen with the variation price displayed.
function remove_add_to_cart(){
if(get_post_meta(get_the_ID(), 'woo_callforinfo', true)) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
} add_action('woocommerce_single_product_summary','remove_add_to_cart');
Here's what I did. The conditional IF statement is because I have a RETAIL shop with variable products that I don't want affected.
function remove_add_to_cart(){
if ( has_term( 'wholesale', 'product_tag' ) ) {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
}
}
add_action('woocommerce_single_variation','remove_add_to_cart');
Added this to get rid of the 'Sorry..' message if price is not set.
add_filter( 'gettext', 'customizing_product_variation_message', 10, 3 );
function customizing_product_variation_message( $translated_text,
$untranslated_text, $domain )
{
if ($untranslated_text == 'Sorry, this product is unavailable. Please choose a different combination.') {
$translated_text = __( '-type anything you want here, or leave a space- ', $domain );
}
return $translated_text;
}
Just add the following code in your functions.php and you will find button hidden
I don't know whether my solution is perfect. But it works. Normally if is_purchasable is returned to the filter woocommerce_is_purchasable, the ‘Add to Cart’ button is displayed, and if false is returned the button is hidden.
So, you just need to add the following:
add_filter('woocommerce_is_purchasable', 'my_woocommerce_is_purchasable', 10, 2);
function my_woocommerce_is_purchasable($is_purchasable, $product) {
// Write code to access custom field value in this function
// let $custom_value be the value from checkbox
return ($custom_value == false ? false : $is_purchasable);
}
No incompatibility issues would creep up.

Change Cart total price in WooCommerce

I am running into issues with the cart total only displaying 0
Essentially what I am trying to do is only accept a deposit total of a certain amount after all cart items have been added to the carts subtotal.
So for example if the customer adds $100 worth of items, they would only pay $10 initially or (10%) of the subtotal as the total value.
I took the code from here: Change total and tax_total Woocommerce and customize it this way:
add_action('woocommerce_cart_total', 'calculate_totals', 10, 1);
function calculate_totals($wc_price){
$new_total = ($wc_price*0.10);
return wc_price($new_total);
}
But the total amount shows 0.00 when that code is enabled. If removed the code, I get the standard total.
I also could not find on the woocommerce site where the full api is listed, only generic articles related to how to create a plugin.
Any help or a point in the right direction would be great.
This does not answer this question. Loic's does. This is another way of doing it to show a line item of 10% off:
function prefix_add_discount_line( $cart ) {
$discount = $cart->subtotal * 0.1;
$cart->add_fee( __( 'Down Payment', 'yourtext-domain' ) , -$discount );
}
add_action( 'woocommerce_cart_calculate_fees', 'prefix_add_discount_line' );
Since Woocommerce 3.2+
it does not work anymore with the new Class WC_Cart_Totals ...
New answer: Change Cart total using Hooks in Woocommerce 3.2+
First woocommerce_cart_total hook is a filter hook, not an action hook. Also as wc_price argument in woocommerce_cart_total is the formatted price, you will not be able to increase it by 10%. That's why it returns zero.
Before Woocommerce v3.2 it works as some WC_Cart properties can be accessed directly
You should better use a custom function hooked in woocommerce_calculate_totals action hook this way:
// Tested and works for WooCommerce versions 2.6.x, 3.0.x and 3.1.x
add_action( 'woocommerce_calculate_totals', 'action_cart_calculate_totals', 10, 1 );
function action_cart_calculate_totals( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( !WC()->cart->is_empty() ):
## Displayed subtotal (+10%)
// $cart_object->subtotal *= 1.1;
## Displayed TOTAL (+10%)
// $cart_object->total *= 1.1;
## Displayed TOTAL CART CONTENT (+10%)
$cart_object->cart_contents_total *= 1.1;
endif;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Is also possible to use WC_cart add_fee() method in this hook, or use it separately like in Cristina answer.

How can I remove the product/inventory count from the shop page on Woocommerce?

Do anyone know how I can remove the number of current available stock that is shown on my Woocommerce product page next the the title of the product? I guess this has changed since the recent Woocommerce update because adding the code snippet
add_filter( 'woocommerce_subcategory_count_html', 'woo_remove_category_products_count' );
function woo_remove_category_products_count() {
return;
}
no longer works. Does anyone have a solution to this? All suggestions are very much appreciated and thank you in advance!
Navigate to WooCommerce > Settings > Product > Inventory. There is a setting "Stock Display Format". Select the "Never Show Stock Amount" from the drop down.
by CSS .stock { display:none; }
The proper way to do it. Copy and paste this code into you functions.php file in your child theme. Code is tested and working.
/* Remove "in stock" text form single products */
function remove_in_stock_text_form_single_products( $html, $text, $product ) {
$availability = $product->get_availability();
if ( isset( $availability['class'] ) && 'in-stock' === $availability['class'] ) {
return '';
}
return $html;
}
add_filter( 'woocommerce_stock_html', 'remove_in_stock_text_form_single_products', 10, 3 );
Accepted answer will not show the stock count, but stock status will appear if css isn't added. woocommerce_get_stock_html can be used to remove it completely.
Complete snippet for this:
add_filter( 'woocommerce_get_stock_html', function ( $html, $product ) {
return '';
}, 10, 2);
This is standard woocommerce functionality, it shows “out of stock” products on the shop page, in the woocommerce widgets, everywhere.
Only on the product single is “Out of stock” shown instead of an “Add to cart”.
Go to Woocommerce → Settings and click the Products tab
Click the Inventory link at the top
Check the Out Of Stock Visibility option to hide out of stock items

Categories