How to show Product Price + Shipping VAT in "Total" Merged - wordpress

I have one little isusue that dont know how to resolve myself. I have set in my shop to show separated Price and shipping costs but in total showed me bad price.
For example my products cost 24.99€ + SHIPPING FEE : 3,95€ = 28.94€ but in calculation in cart page is calculating: 24.99€ + 3.95€ - 0.26€ what is wrong.
i found that Total price is calculated via this function:
<td data-title="<?php esc_attr_e( 'Total', 'woocommerce' ); ?>"><?php wc_cart_totals_order_total_html(); ?></td>
and this is function that control that part:
from cart-totals.php in templates, and bellow is function from wc-cart-functions.php
function wc_cart_totals_order_total_html() {
$value = '<strong>' . WC()->cart->get_total() . '</strong> ';
// If prices are tax inclusive, show taxes here.
if ( wc_tax_enabled() && WC()->cart->display_prices_including_tax() ) {
$tax_string_array = array();
$cart_tax_totals = WC()->cart->get_tax_totals();
if ( get_option( 'woocommerce_tax_total_display' ) == 'itemized' ) {
foreach ( $cart_tax_totals as $code => $tax ) {
$tax_string_array[] = sprintf( '%s %s', $tax->formatted_amount, $tax->label );
}
} elseif ( ! empty( $cart_tax_totals ) ) {
$tax_string_array[] = sprintf( '%s %s', wc_price( WC()->cart->get_taxes_total( true, true ) ), WC()->countries->tax_or_vat() );
}
if ( ! empty( $tax_string_array ) ) {
$taxable_address = WC()->customer->get_taxable_address();
$estimated_text = WC()->customer->is_customer_outside_base() && ! WC()->customer->has_calculated_shipping()
? sprintf( ' ' . __( 'estimated for %s', 'woocommerce' ), WC()->countries->estimated_for_prefix( $taxable_address[0] ) . WC()->countries->countries[ $taxable_address[0] ] )
: '';
$value .= '<small class="includes_tax">' . sprintf( __( '(includes %s)', 'woocommerce' ), implode( ', ', $tax_string_array ) . $estimated_text ) . '</small>';
}
}
echo apply_filters( 'woocommerce_cart_totals_order_total_html', $value );
}
So my question is how to add that 1.63E at Total Price, so will get correct price. Thanks
EDIT: Found the same problem like mine here but answers dont seems to make changes.

First, thanks for your Post, I was almost thinking I'm the only one with this need.
So far, this worked for my shop. I'm shure my code is not very versatile for different shop settings. Maybe someone could make a more general usable version.
Edit: I've added a picture showing the two rates. Image of the result and I've found a minor mistake calculating the shipping tax, corrected now.
/**
* Change Tax Amount including Shipping Taxes
* Referencing to wc-cart-functions.php starting from Line 296
*
*/
add_filter( 'woocommerce_cart_totals_order_total_html', 'woo_rename_tax_inc_cart', 10, 1 );
function woo_rename_tax_inc_cart( $value ) {
/* Get all infos needed */
$shipping_total = WC()->cart->shipping_total;
$taxes = WC()->cart->get_taxes_total( true, true );
$taxrate = 7.7;
$newtaxes = ($shipping_total/(100+$taxrate)*$taxrate) + $taxes; // Shipping is 100% + taxrate %, so we deduct both percentages.
/* Check if Shipment total is active */
if ( ! empty($shipping_total) && $shipping_total != 0 ) {
if ( ! empty( $value ) ) {
// Show Price /wc-cart-functions.php Line 297
$value = '<strong>' . WC()->cart->get_total() . '</strong> ';
$value .= '<small class="includes_tax">' . '(inkl. ' . wc_price( $newtaxes ) . ' MWST)' . '</small>';
}
}
// Attach Tax Info to Price (single line)
$value = str_ireplace( 'Tax', 'GST', $value );
return $value;
}

Related

Display free shipping threshold based on Woocommerce min amount order from shipping settings

I want to display shipping notice based on free shipping settings for min amount order. I have more then one shipping zone based on different country and currency. Tried to use the following code, it works when you add product to cart, the shipping notice is display but as if you update quantity the shipping notice is removed, maybe duo to Ajax load? Can I add something to this code to make it work with qty update on product?
add_action( 'woocommerce_before_mini_cart_contents', 'display_free_shipping_cart_notice_zones' );
function display_free_shipping_cart_notice_zones() {
// Get Shipping Methods for Current Zone
global $woocommerce;
$shipping_methods = $woocommerce->shipping->get_shipping_methods();
// Loop through the array to find min_amount value/s
foreach($shipping_methods as $key => $value) {
if ( $shipping_methods[$key]->min_amount > 0 ) {
$min_amounts[$key] = $shipping_methods[$key]->min_amount;
}
}
if ( is_array($min_amounts) ) {
// Find lowest min_amount
$min_amount = min($min_amounts);
// Get Cart Subtotal inc. Tax excl. Shipping
$current = WC()->cart->subtotal;
// If Subtotal < Min Amount Echo Notice
// and add "Continue Shopping" button
if ( $current < $min_amounts ) {
$added_text = 'Get free shipping if you order ' . wc_price( $min_amount - $current ) . ' more!';
$return_to = wc_get_page_permalink( 'shop' );
$notice = sprintf( '%s %s', esc_url( $return_to ), 'Continue Shopping', $added_text );
wc_print_notice( $notice, 'notice' );
}
else if ( $current = $min_amounts ) {
$added_text = 'Congratulations - Your shipping is now on us and absolutely free :)';
$return_to = wc_get_page_permalink( 'shop' );
//$notice = sprintf( '%s %s', esc_url( $return_to ), 'Continue Shopping', $added_text );
$notice = sprintf( '%s', $added_text );
wc_print_notice( $notice, 'notice' );
}
}
}

How to restrict customer order in woocommerce for less than a desired cart value

I want to restrict the customer to place order of less than INR 400 for 2 states. So far i have tried this
add_action( 'woocommerce_check_cart_items', 'set_min_total' );
function set_min_total() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
global $woocommerce;
// Set minimum cart total
$minimum_cart_total = 400;
?>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('#billing_state').on('change',function(){
var optionText = jQuery("#billing_state option:selected").val();
});
});
<?php $selected_state = '<script type="text/javascript">optionText</script>'?>
</script>;
<?php
$allowed_state = 'UP';
if($allowed_state != $selected_state) {
$total = WC()->cart->subtotal;
if( $total <= $minimum_cart_total ) {
// Display our error message
wc_add_notice( sprintf( '<strong>A Minimum of %s %s is required before checking out.</strong>'
.'<br />Current cart\'s total: %s %s',
$minimum_cart_total,
get_option( 'woocommerce_currency'),
$total,
get_option( 'woocommerce_currency') ),
'error' );
}
}
}
}
but it didn't worked, please advice where am i doing wrong.
add_action( 'woocommerce_check_cart_items', 'cldws_set_min_total');
function cldws_set_min_total() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
global $woocommerce;
// Set minimum cart total
$minimum_cart_total = 100;
// A Minimum of 100 AUD is required before checking out.
$total = WC()->cart->subtotal;
// Compare values and add an error is Cart's total
if( $total <= $minimum_cart_total ) {
// Display our error message
wc_add_notice( sprintf( '<strong>A Minimum of %s %s is required before checking out.</strong>'
.'<br />Current cart\'s total: %s %s',
$minimum_cart_total,
get_option( 'woocommerce_currency'),
$total,
get_option( 'woocommerce_currency') ),
'error' );
}
}
}
Please check with above code , here amount is 100 you can use as you need
Add the follows codes snippet to achieve the above task -
/**
* Set a minimum order amount for checkout
*/
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
// Set minimum order value
$minimum = 400;
if ( WC()->cart->total < $minimum ) {
if( is_cart() ) {
wc_print_notice(
sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to place your order ' ,
wc_price( WC()->cart->total ),
wc_price( $minimum )
), 'error'
);
} else {
$billing_state = ( $_POST && isset( $_POST['billing_state'] ) ) ? $_POST['billing_state'] : '';
if( in_array( $billing_state, array( 'UP' ) ) ) {
wc_add_notice(
sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to place your order' ,
wc_price( WC()->cart->total ),
wc_price( $minimum )
), 'error'
);
}
}
}
}
Codes goes to your active theme's functions.php.

woocommerce cart coupon price without tax to display

I want woocommerce to display price in cart without tax, reduced by using a coupon.
I want to have:
price without tax
coupon value
price without tax reduced by coupon
tax
price with tax
Can anybody help me please?
I was trying to play wit this code:
add_action( 'woocommerce_cart_calculate_fees','new_customers_discount', 10, 1 );
function new_customers_discount( $wc_cart ) {
if ( is_admin() && ! defined('DOING_AJAX') ) // We exit
// Only for logged in users
if ($woocommerce->cart->applied_coupons) // We exit
// Only for new customers without orders
if ( wc_get_customer_order_count( get_current_user_id() ) != 10000 ) return; // We exit
// Calculation
$discount = $wc_cart->cart_contents_total - $coupon ;
$wc_cart->add_fee( __( 'Netto po rabacie', 'woocommerce')."", $discount);
echo '<div id="product-meta"><span class="detaliczna"><p class="item-description" style="text-align:center; font-size: 14px; display: none; ">' . $wc_cart->add_fee( __( 'TEST', 'woocommerce')."", -$discount ) . ' zł netto</p></span></div>';
but no luck. I am not a programmer. :)
I got it, but i want to change the order. I want to move the nett price on top of VAT
add_action( 'woocommerce_cart_totals_before_order_total', 'bbloomer_wc_discount_total_30', 10, 1 );
add_action( 'woocommerce_review_order_before_order_total', 'bbloomer_wc_discount_total_30', 10, 1 );
function bbloomer_wc_discount_total_30() {
global $woocommerce;
$discount_total = 0;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values) {
$_product = $values['data'];
if ( $_product->get_regular_price() ) {
$regular_price = $_product->get_regular_price();
$sale_price = $_product->get_sale_price();
$discount = ($regular_price - $coupon) ;
$discount_total += $discount;
}
}
if ( $discount_total > 0 ) {
echo '<tr class="cart-discount">
<th>'. __( 'Razem netto', 'woocommerce' ) .'</th>
<td data-title=" '. __( 'Razem netto', 'woocommerce' ) .' ">'
. wc_price( $discount_total - $woocommerce->cart->discount_cart ) .'</td>
</tr>';
}
}

Woocommerce Show default variation price

I'm using Woocommerce and product variations and all my variations have a default variation defined.
How I can find the default variation and display its price?
This is the code I got so far, but it displays the cheapest variation price, and I'm looking for my default variation product price instead.
// Use WC 2.0 variable price format, now include sale price strikeout
add_filter( 'woocommerce_variable_sale_price_html', 'wc_wc20_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'wc_wc20_variation_price_format', 10, 2 );
function wc_wc20_variation_price_format( $price, $product ) {
// Main Price
$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
$price = $prices[0] !== $prices[1] ? sprintf( __( 'HERE YOUR LANGUAGE: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
// Sale Price
$prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
sort( $prices );
$saleprice = $prices[0] !== $prices[1] ? sprintf( __( 'HERE YOUR LANGUAGE: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
if ( $price !== $saleprice ) {
$price = '<del>' . $saleprice . '</del> <ins>' . $price . '</ins>';
}
return $price;
I found the code example here Woocommerce variation product price to show default
I did not find answer for this question (how to display price from default product variation instead of price range?) so i created the code:
add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
function custom_variation_price( $price, $product ) {
foreach($product->get_available_variations() as $pav){
$def=true;
foreach($product->get_variation_default_attributes() as $defkey=>$defval){
if($pav['attributes']['attribute_'.$defkey]!=$defval){
$def=false;
}
}
if($def){
$price = $pav['display_price'];
}
}
return woocommerce_price($price);
}
This plugin show price from default variation, of course if you set default values before. Tested on woocommerce version 2.6.2.
This is my own solution, think it could be better, but I'm not a php developer, so feel free to add improvements.
function wc_wc20_variation_price_format( $price, $product ) {
// Main Price
$available_variations = $product->get_available_variations();
$selectedPrice = '';
foreach ( $available_variations as $variation )
{
$tmp = implode($variation['attributes']);
if (strpos($tmp,'standard') !== false) {
$selectedproduct = wc_get_product($variation['variation_id']);
$price = $selectedproduct->get_price();
$selectedPrice = wc_price($price);
}
}
return $selectedPrice;
}
I modified your code a bit to show discounted price as well.
// show default variation price
add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
function custom_variation_price( $price, $product ) {
$default_attributes = $product->get_variation_default_attributes();
foreach($product->get_available_variations() as $variation) {
$is_default=true;
foreach($default_attributes as $attribute_key => $attribute_value) {
if($variation['attributes']['attribute_' . $attribute_key] != $attribute_value){
$is_default=false;
break;
}
}
if($is_default){
return $variation['price_html'];
}
}
}

Problems Adding Woocommerce Bookings info into the Description of Google Calendar

I have synched Woocommerce Bookings with Google Calendar so that employees (with access to a shared Google Calendar) can see instantly when a customer books a trip.
I'm trying to get product add-ons to show up in the description as well. So far the only thing showing up are resources and # of persons. The following code kicks back an error:
Fatal error: Call to a member function get_order_item_totals() on a non-object in /home/content/12/10265512/html/wp-content/plugins/woocommerce-bookings/includes/integrations/class-wc-bookings-google-calendar-integration.php on line 454
I'm just learning my way around PHP out of necessity to get this system to fit our needs. Any help or suggestions would be / are greatly appreciated!
public function sync_booking( $booking_id ) {
$event_id = get_post_meta( $booking_id, '_wc_bookings_gcalendar_event_id', true );
$booking = get_wc_booking( $booking_id );
$api_url = $this->calendars_uri . $this->calendar_id . '/events';
$access_token = $this->get_access_token();
$timezone = wc_booking_get_timezone_string();
$product = $booking->get_product();
$summary = '#' . $booking->id . ' - ' . $product->get_title();
$description = '';
// Add resources in description
if ( $resource = $booking->get_resource() ) {
$description .= __( 'Resource #', 'woocommerce-bookings' ) . $resource->ID . ' - ' . $resource->post_title;
}
// Add ProductAdd on in description testing this
if ( $totals = $order->get_order_item_totals() ) {
$i = 0;
foreach ( $totals as $total ) {
$i++;
if ( $i == 1 ) {
$description .= sprintf($total['label']) . PHP_EOL;
$description .= sprintf($total['value']) . PHP_EOL;
}
}
}
// Add persons in description
if ( $booking->has_persons() ) {
$description .= ( '' != $description ) ? PHP_EOL . PHP_EOL : '';
foreach ( $booking->get_persons() as $id => $qty ) {
if ( 0 === $qty ) {
continue;
}
$person_type = ( 0 < $id ) ? get_the_title( $id ) : __( 'Person(s)', 'woocommerce-bookings' );
$description .= sprintf( __( '%s: %d', 'woocommerce-bookings'), $person_type, $qty ) . PHP_EOL;
$description .= wp_kses_post( $product->post->post_excerpt, array() ) . PHP_EOL;
$description .= sprintf("ADDITIONAL NOTES:") . PHP_EOL;
$description .= sprintf("Hey John passing static notes to Calender test") . PHP_EOL;
}
}
Edit
I tried adding Billing Email and Billing Phone and still cannot get it right. All I see in Google Calendar is Email followed by blank space.
$description .= sprintf( __('Email: %s', 'woocommerce'), $order->billing_email ) . PHP_EOL;
$description .= sprintf( __('Phone: %s', 'woocommerce'), $order->billing_phone ) . PHP_EOL;
the wp_kses_post method in the code up above does return the short description from WooCommerce and places into the Google Calendar description so I will try that next.
I'm still pluggin away at this and after looking at some other websites tried the following
$description .= sprintf( __('%s', 'woocommerce'), $order->email_order_items_table( true, false, true )) . PHP_EOL;
Which is used to pass the same information on in an email to the site admin.
<?php echo $order->email_order_items_table( true, false, true ); ?>
I'm still getting a Fatal Error call to a member function on a non-object. I contacted support at WooCommerce a few days ago. I'll report back here incase someon else is trying to do the same thing as me.
I was able to solve the issue by pulling all order info from the email_orders_table. I'm posting my answer in case someone else is attempting to do the same thing. Customer Support at WooCommerce was unable to help stating it was outside of their support policy and considered custom code.
// Add First Name of Guest
$description .= sprintf( __( 'NAME: %s', 'woocommerce-bookings' ), $booking->get_order()->billing_first_name ) . PHP_EOL;
// Add their email address
$description .= sprintf( __('EMAIL: %s', 'woocommerce-bookings'), $booking->get_order()->billing_email ) . PHP_EOL;
// Add their Phone Number
$description .= sprintf( __('PHONE: %s', 'woocommerce-bookings'), $booking->get_order()->billing_phone ) . PHP_EOL;
// Purchase Notes and WooCommerce Product Add Ons
$description .= sprintf("ADDITIONAL NOTES:") . PHP_EOL;
$description .= sprintf ( __('%s', 'woocommerce-bookings'), strip_tags($order->email_order_items_table( $order->is_download_permitted(), true, true ))) . PHP_EOL ;
This works really well after stripping the HTML tags and all the Guest information is shown in a Private Google Calendar for the entire Staff to Reference. Looking back I certainly worked around in a circle. I had forgotten.
do_action( 'woocommerce_email_after_order_table', $order, true, false );

Resources