Remove subtotal in woocommerce - woocommerce

Using woocommerce, I need a way to remove the subtotal in the checkout if there is only 1 product in the cart.
Anyone know what the best way to do this and how?

You could always put a if statement around, cart->get_total(); ?>, in you cart.php file. Not sure if it's cart php but something like that.
Just be aware. After an update this will be gone. So please create this in your child theme.
Something like:
$something = echo $woocommerce->cart->get_total();
global $woocommerce;
if ( sizeof( $woocommerce->cart->cart_contents) == 1 ) :
$somthing = '';
endif;
<strong>$somthing</strong>

You can override, 'review-order.php' template file located in, 'checkout' directory.
You can add following condition above 'cart-subtotal' class,
<?php if(WC()->cart->cart_contents_count != 1) : ?>
<tr class="cart-subtotal">
<th><?php _e( 'Subtotal', 'woocommerce' ); ?></th>
<td><?php wc_cart_totals_subtotal_html(); ?></td>
</tr>
<?php endif; ?>

Related

Hide coupon discount on Woocommerce checkout [duplicate]

I'm using a coupon for a calculation in the Woocommerce cart. It automatically adds a discount to the total so the right amount can be sent to payments gateways.
I'd like to hide all infos about this coupons/discount from visitors.
Problem: The only method I've found (see below) hides the coupon field, row (from totals) and messages, but also disable the coupon...
add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field' );
function hide_coupon_field( $enabled ) {
if ( is_cart() || is_checkout() ) {
$enabled = false;
}
return $enabled;
}
Is there a hook allowing to hide everything related to the discount without canceling the coupon?
EDIT:
Looks like it's impossible to simply remove the discount line in the order-details. So a simple solution, inspired by helgatheviking suggestion, might be to remove all the totals generated by this part
<?php if ( $totals = $order->get_order_item_totals() ) foreach ( $totals as $total ) : ?>
<tr>
<th scope="row"><?php echo $total['label']; ?></th>
<td><?php echo $total['value']; ?></td>
</tr>
<?php endforeach; ?>
And then to echo them one by one the way I need it. I'm already able to show the order total with this
<td><?php echo number_format($order->get_total(),2,'.','')."€"; ?></td>
but now I'm trying to retrieve the order subtotal, and this code
<td><?php echo number_format($order->get_item_subtotal(),2,'.','')."€"; ?></td>
gives me a Warning: Missing argument 1 for WC_Order::get_item_subtotal().
I'm not sure if get_item_subtotal() is the right way to get the order subtotal. And if so, what argument is missing? Or should I search around get_line_subtotal or get_subtotal_to_display?
No, there does not seem to be as there is no filter in the get_coupons() method of the cart class. If you went to the WooCommerce git repo and sent a pull request with a filter here and an explanation as to why it should be there, they might consider merging it in. I've done that a few times.
You could also, copy the checkout/review-order.php and cart/cart-totals.php templates into your theme and remove the following two blocks of code:
<?php foreach ( WC()->cart->get_coupons( 'cart' ) as $code => $coupon ) : ?>
<tr class="cart-discount coupon-<?php echo esc_attr( $code ); ?>">
<th><?php wc_cart_totals_coupon_label( $coupon ); ?></th>
<td><?php wc_cart_totals_coupon_html( $coupon ); ?></td>
</tr>
<?php endforeach; ?>
and
<?php foreach ( WC()->cart->get_coupons( 'order' ) as $code => $coupon ) : ?>
<tr class="order-discount coupon-<?php echo esc_attr( $code ); ?>">
<th><?php wc_cart_totals_coupon_label( $coupon ); ?></th>
<td><?php wc_cart_totals_coupon_html( $coupon ); ?></td>
</tr>
<?php endforeach; ?>
Keep in mind that this prevents the display of ALL coupon discounts and will end up looking like the following screenshots:
I'm not a fan of overriding the more complex WC templates... especially not the ones pertaining to the checkout process. I've had to fix many sites that stopped working when their theme template overrides became obsolete as WooCommerce develops.
Edit
I tracked down the Discount row in the order/order-details.php template. It is from the function $order->get_order_item_totals()... this returns an array of rows and can be filtered. So, this removes the row from the order received page:
function so_25714509_get_order_item_totals( $total_rows ){
unset( $total_rows['order_discount'] );
return $total_rows;
}
add_filter( 'woocommerce_get_order_item_totals', 'so_25714509_get_order_item_totals' );

Show a string depending on shipping country in WooCommerce checkout

i want to show a string in the checkout in woocommerce which depends on the shipping country. I use the following code:
<?php global $woocommerce; ?>
<?php $current_cc = $woocommerce->customer->get_shipping_country() ?>
<?php if ($current_cc = DE) { ?>
<p><?php echo var_dump($current_cc);?></p>
<tr class="order-total">
<th><?php esc_html_e( 'Total', 'woocommerce' ); ?><br><small
class="shopping_cart_total_vat_message">inkl. MwSt.</small></th>
<td><?php wc_cart_totals_order_total_html(); ?></td>
</tr>
<?php } else { ?>
<tr class="order-total">
<th><?php esc_html_e( 'Total', 'woocommerce' ); ?><br><small
class="shopping_cart_total_vat_message">exkl. MwSt.</small></th>
<td><?php wc_cart_totals_order_total_html(); ?></td>
</tr>
<?php } ?>
<?php do_action( 'woocommerce_review_order_after_order_total' ); ?>
The var_dump always shows DE also when i choose another country. Where is the problem?
i hope somebody can help.
Thanks
PHP is a server-side language. If the page is not reloaded, the content processed by PHP will not be displayed.
You can get what you want by using a JQuery script.
The following code will only be shown on the checkout page and will display custom text after the shipping country paragraph.
// adds custom text based on shipping country value in checkout
add_action( 'wp_footer', 'add_custom_text_based_on_shipping_country' );
function add_custom_text_based_on_shipping_country() {
// only in the checkout
if ( ! is_checkout() ) {
return;
}
?>
<script type="text/javascript">
// show custom text on page load (based on shipping country value)
jQuery('<div id="custom_string"><span>'+jQuery("#shipping_country").find("option:selected").val()+'</span></div>').insertAfter('#shipping_country_field');
// replaces the text of the #custom_string element based on the value of the selected shipping country
jQuery('#shipping_country').change(function() {
var shippingCountry = jQuery(this).find('option:selected').val();
var customContent = '';
switch ( shippingCountry ) {
case 'FR':
customContent = '<span>'+shippingCountry+'</span>';
break;
case 'DE':
customContent = '<span>'+shippingCountry+'</span>';
break;
case 'IT':
customContent = '<span>'+shippingCountry+'</span>';
break;
default:
customContent = '';
break;
}
jQuery('#custom_string').html(customContent);
});
</script>
<?php
}
The code has been tested and works. Add it to your active theme's functions.php.

Change sorting of WooCommerce customer orders --> orders.php [duplicate]

In WooCommerce, customers can log in to their account and see the order history. By default the orders are displaying with the newest order date first.
I want to turn this around, so the order with the oldest date shows first.
I can't find any place to change ordering from ASC/DESC, looking in the template file woocoommerce/myaccount/orders.php file.
<table class="woocommerce-orders-table woocommerce-MyAccount-orders shop_table shop_table_responsive my_account_orders account-orders-table">
<thead>
<tr>
<?php foreach ( wc_get_account_orders_columns() as $column_id => $column_name ) : ?>
<th class="woocommerce-orders-table__header woocommerce-orders-table__header-<?php echo esc_attr( $column_id ); ?>"><span class="nobr"><?php echo esc_html( $column_name ); ?></span></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php foreach ( $customer_orders->orders as $customer_order ) :
Any way to alter the loop to display orders with the oldest date first?
The filter hook woocommerce_my_account_my_orders_query allows to change the 'order' argument to ASC (ascending), changing the sorting behavior on My account customer orders list:
add_filter( 'woocommerce_my_account_my_orders_query', 'my_account_orders_query_change_sorting' );
function my_account_orders_query_change_sorting( $args ) {
$args['order'] = 'ASC'; // Default is 'DESC'
return $args;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and work.

How can I hide the price's inside the Cart for unregisterd users wordpress

Basically I have downloaded a plugin to hide price's for unregistered user's. The problem is if the customer clicks on "Add to Cart" button and they go at the Cart they can see the price. Is there any setting to disable prices on cart for unregisterd users or anything related to that?
Thank's in advance
simple way to do this is overwrite cart template and check if user is login then display. I show you the logic. I hope you know how to overwrite template. still brief I say you here.
This is to overwrite cart.
woocommerce/templates/cart/cart.php to
yourtheme/woocommerce/cart/cart.php
this is for check logic
<?php if(is_user_logged_in()){ ?>
<td class="product-price" data-title="<?php esc_attr_e( 'Price', 'woocommerce' ); ?>">
<?php
echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key ); // PHPCS: XSS ok.
?>
</td>
<?php } ?>
you also need to do same for label.

Hide discount infos without canceling the coupon in woocommerce

I'm using a coupon for a calculation in the Woocommerce cart. It automatically adds a discount to the total so the right amount can be sent to payments gateways.
I'd like to hide all infos about this coupons/discount from visitors.
Problem: The only method I've found (see below) hides the coupon field, row (from totals) and messages, but also disable the coupon...
add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field' );
function hide_coupon_field( $enabled ) {
if ( is_cart() || is_checkout() ) {
$enabled = false;
}
return $enabled;
}
Is there a hook allowing to hide everything related to the discount without canceling the coupon?
EDIT:
Looks like it's impossible to simply remove the discount line in the order-details. So a simple solution, inspired by helgatheviking suggestion, might be to remove all the totals generated by this part
<?php if ( $totals = $order->get_order_item_totals() ) foreach ( $totals as $total ) : ?>
<tr>
<th scope="row"><?php echo $total['label']; ?></th>
<td><?php echo $total['value']; ?></td>
</tr>
<?php endforeach; ?>
And then to echo them one by one the way I need it. I'm already able to show the order total with this
<td><?php echo number_format($order->get_total(),2,'.','')."€"; ?></td>
but now I'm trying to retrieve the order subtotal, and this code
<td><?php echo number_format($order->get_item_subtotal(),2,'.','')."€"; ?></td>
gives me a Warning: Missing argument 1 for WC_Order::get_item_subtotal().
I'm not sure if get_item_subtotal() is the right way to get the order subtotal. And if so, what argument is missing? Or should I search around get_line_subtotal or get_subtotal_to_display?
No, there does not seem to be as there is no filter in the get_coupons() method of the cart class. If you went to the WooCommerce git repo and sent a pull request with a filter here and an explanation as to why it should be there, they might consider merging it in. I've done that a few times.
You could also, copy the checkout/review-order.php and cart/cart-totals.php templates into your theme and remove the following two blocks of code:
<?php foreach ( WC()->cart->get_coupons( 'cart' ) as $code => $coupon ) : ?>
<tr class="cart-discount coupon-<?php echo esc_attr( $code ); ?>">
<th><?php wc_cart_totals_coupon_label( $coupon ); ?></th>
<td><?php wc_cart_totals_coupon_html( $coupon ); ?></td>
</tr>
<?php endforeach; ?>
and
<?php foreach ( WC()->cart->get_coupons( 'order' ) as $code => $coupon ) : ?>
<tr class="order-discount coupon-<?php echo esc_attr( $code ); ?>">
<th><?php wc_cart_totals_coupon_label( $coupon ); ?></th>
<td><?php wc_cart_totals_coupon_html( $coupon ); ?></td>
</tr>
<?php endforeach; ?>
Keep in mind that this prevents the display of ALL coupon discounts and will end up looking like the following screenshots:
I'm not a fan of overriding the more complex WC templates... especially not the ones pertaining to the checkout process. I've had to fix many sites that stopped working when their theme template overrides became obsolete as WooCommerce develops.
Edit
I tracked down the Discount row in the order/order-details.php template. It is from the function $order->get_order_item_totals()... this returns an array of rows and can be filtered. So, this removes the row from the order received page:
function so_25714509_get_order_item_totals( $total_rows ){
unset( $total_rows['order_discount'] );
return $total_rows;
}
add_filter( 'woocommerce_get_order_item_totals', 'so_25714509_get_order_item_totals' );

Resources