How to set minimum order amount for WooCommerce excluding shipping? - woocommerce

Is there a code to set minimum order amount for WooCommerce excluding shipping?
With this one I can set minimum order amount but with shipping:
/**
* 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 this variable to specify a minimum order value
$minimum = 16;
if ( WC()->cart->total < $minimum ) {
if( is_cart() ) {
wc_print_notice(
sprintf( 'Стойност на Вашата поръчка: %s <p>Минималната стойност на поръчката към ресторанта: %s' ,
wc_price( WC()->cart->total ),
wc_price( $minimum )
), 'error'
);
} else {
wc_add_notice(
sprintf( 'Стойност на Вашата поръчка: %s <p>Минималната стойност на поръчката към ресторанта: %s' ,
wc_price( WC()->cart->total ),
wc_price( $minimum )
), 'error'
);
}
}
}

As I mentioned in the comments,
You just need to subtract the cost of shipping from the cart total.
You can use the WC_Cart::get_shipping_total method.
It should look something like this:
<?php
/**
* 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 this variable to specify a minimum order value
$minimum = 16;
$cart_total = WC()->cart->total; // Cart total incl. shipping
$shipping_total = WC()->cart->get_shipping_total(); // Cost of shipping
if ( ($cart_total - $shipping_total) < $minimum ) {
if( is_cart() ) {
wc_print_notice(
sprintf( 'Стойност на Вашата поръчка: %s <p>Минималната стойност на поръчката към ресторанта: %s' ,
wc_price( WC()->cart->total ),
wc_price( $minimum )
), 'error'
);
} else {
wc_add_notice(
sprintf( 'Стойност на Вашата поръчка: %s <p>Минималната стойност на поръчката към ресторанта: %s' ,
wc_price( WC()->cart->total ),
wc_price( $minimum )
), 'error'
);
}
}
}
?>

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

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

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;
}

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'];
}
}
}

Resources