WooCommerce - Display Fees per product not per Cart - php

I have to add a charge per shopping cart line in a WooCommerce shop. This fee is intended to change the unit price and the line sum.
This fee is not intended as a fee for shipping and grand total.
There is a service fee ($54) per basket line / per product.
For example, $ 0.81 per product plus a service-fee of $ 54 will result in $ 1.81 per product and a total of $ 94.66 at 50 pieces.
What I have so far ...
// Change the line total price
add_filter( 'woocommerce_get_discounted_price', 'calculate_discounted_price', 10, 2 );
// Display the line total price
add_filter( 'woocommerce_cart_item_subtotal', 'display_discounted_price', 10, 2 );
function calculate_discounted_price( $price, $values ) {
$price += 5;
return $price;
}
function display_discounted_price( $values, $item ) {
return wc_price( $item[ 'line_total' ] );
}
Many thanks for your help!

Related

Woocommerce - Apply 10% discount to the lowest priced product in the cart based on several conditions

I'm struggled with one problem and the problem is as it follows. In our store users could create a package from 3 products. I need to apply a 10% discount to 2 products in cart.
PRODUCT required to get a discount = 10 euros
PRODUCT = 15 euros - you could get a discount from this product
PRODUCT = 12 euros - you could get a discount from this product
Discount should apply always to required product and the lowest price product 2 or 3.
Cart examples
If there is product 1, 2 and 3 in cart you will get a 10% discount to product number 1 and product number 3.
If there is a product number 1 and 3 in cart you will get a 10% discount to the both of them
If there is a product number 1 and 2 in cart you will get a 10% discount to the both of them
If there is a product number 1 only, product number 2 only, product number 3 only, then no discount is applied to your cart
If there is some of the combinations and other products which are not these 3 mentioned products above, the discount will be applied only for the mentioned products as it follows from cart examples above.
If there are more sets - if you want to order 2 products from (product number 1 and product number 2) or (product number 1 and product number 3) you have to add always 1 more to the both of them to get another 10% sale from another set so quantity will be 2/2. If theres quantity 2 (ID1)/1 (ID2) no discount will be applied for the second set only for the first one.
My code so far but I'm totally lost
function apply_discount_ten( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Product ID needs to be in cart to get a discount
$product_id = 2530;
// Products in cart
$products_in_cart = count( $cart->get_cart() );
//Set conditions
if ( $products_in_cart && (matched_cart_items($product_id)) {
// Set array
$product_prices = array();
// Loop though cart items
foreach ( $cart->get_cart() as $cart_item ) {
// Product
$product = $cart_item['data'];
// Get price
$product_price = $product->get_price();
// Push
$product_prices[] = $product_price;
}
// Sort: low to high
asort( $product_prices );
// Number of products receive a discount
$products_receive_discount = What to do here;
// Set variable
$total = 0;
// Loop trough
foreach ( array_slice( $product_prices, 0, $products_receive_discount ) as $product_price ) {
// Calculate
$total += $product_price;
}
// Calculate discount
$discount = ( $total * 10 ) / 100;
// Add fee
$cart->add_fee( 'Discount', $discount, true );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'apply_discount_ten', 10, 1 );
Thank you in advance

Display "excl. VAT" in Woocommerce product-subtotal

in Woocommerce when you choose to display prices during checkout inclusive VAT, Woocommerce then adds a .tax_label after the td.product-subtotal, and a .includes_tax after the total. For customers from countries with 0% VAT, nothing is displayed, I need the tax_label and includes_tax to be displayed though as “excl. VAT”. So in the td.product-subtotal cell there should be the subtotal price with the added "excl. VAT" and in the order-total.td there should be the total price also with the added "excl.VAT".
add_filter( 'woocommerce_cart_hide_zero_taxes', '__return_false' );
Above filter does not do what I want because it does not add "excl.VAT" to the td.product-subtotal.
If you're talking about the cart subtotal there is a filter for that woocommerce_cart_subtotal.
<?php
add_filter( 'woocommerce_cart_subtotal', 'filter_woocommerce_cart_subtotal', 10, 3 );
function filter_woocommerce_cart_subtotal( $cart_subtotal, $compound, $instance ) {
if( ! empty( $cart_subtotal ) ) {
return $cart_subtotal . ' - excl. VAT';
};
}; ?>
Untested but should be working.

WooCommerce doesn't show actual price on cart page without tax

I have build webstore on WooCommerce. I have added standard tax rate when the customer fills their billing address and select country tax will be applied on the checkout page that is working fine I want to show actual price of products on cart page without multiply my tax ratio with my product price
This is checkout page show tax when a country is selected
this is cart page showing tax calculated value and grand total show without tax value
I have added this function in my function.php file but no success
function wc_remove_cart_tax_totals( $tax_totals, $instance ) {
if( is_cart() ) {
$tax_totals = array();
}
return $tax_totals;
}
add_filter( 'woocommerce_cart_tax_totals', 'wc_remove_cart_tax_totals', 10, 2 );
// Show the cart total excluding tax.
function wc_exclude_tax_cart_total( $total, $instance ) {
// If it is the cart subtract the tax
if( is_cart() ) {
$total = round( WC()->cart->cart_contents_total + WC()->cart->shipping_total + WC()->cart->fee_total, WC()->cart->dp );
}
return $total;
}
add_filter( 'woocommerce_calculated_total', 'wc_exclude_tax_cart_total', 10, 2 );
add_filter( 'woocommerce_subscriptions_calculated_total', 'wc_exclude_tax_cart_total', 10, 2 );

Tax exclusive if coupon applied else tax inclusive in items

I have setup taxes inclusive for all my items and filled MRP in price. But now I want to apply tax inclusive if customer didn't applied coupon i.e. buying on MRP. But when customer applies coupon I need to apply taxes on after discount amount.
Is it possible with settings within Woocommerce or is there any plugin available?
For e.g.
**Case I**
Product MRP = 670
Shipping = 50
Tax 18% = 102
Final price = 670 (Including Taxes)
It's Fine.
**Case II**
Product MRP = 670
Discount 40%= 268
Price = 402
Shipping = 50
Tax 18% = 61
Final price = 452 (Including Taxes)
But I need tax to calculated exclusively on discounted price i.e. 402+18% = 474+50 (Ship) = 524
I have tried following filter in my custom plugin:
add_filter( 'woocommerce_calc_tax', 'inc_or_exc',10,3 );
// add_filter( 'woocommerce_calculate_totals', 'calculate_totals',11 );
function inc_or_exc( $taxes,$price,$rates ) {
// echo "<pre>";
if(!empty(WC()->cart->coupon_discount_amounts)){
return WC_Tax::calc_exclusive_tax( $price, $rates );
}else{
return WC_Tax::calc_inclusive_tax( $price, $rates );
}
}
But it calculates taxes bit strange. If item MRP is 100, it shows 98.85 and also totals are not updating with new taxes and shipping rates after plugin run. If I disable plugin then item MRP is shown fine i.e. 100.
Finally I have solved it.
First I applied inclusive exlusive filter. Then called woocommerce_calculated_total with custom condition and achieved my motive.
add_filter( 'woocommerce_calc_tax', 'inc_or_exc',10,3 );
// do_action('add_points');
add_filter( 'woocommerce_calculated_total', 'custom_calculated_total', 10, 2 );
function inc_or_exc( $taxes,$price,$rates ) {
// echo "<pre>";
if(!empty(WC()->cart->coupon_discount_amounts)){
return WC_Tax::calc_exclusive_tax( $price, $rates );
}else{
return WC_Tax::calc_inclusive_tax( $price, $rates );
}
}
function custom_calculated_total( $total, $cart ){
// echo "<pre>";
if(!empty(WC()->cart->coupon_discount_amounts)){
return round( $total + WC()->cart->get_cart_contents_tax(), $cart->dp );
}else{
return round( $total, $cart->dp );
}
}

Adding Custom price with woocomerce product price in Cart & Checkout

I have added a custom field in the woocommerce which has extra price of frames for pictures, now if picture price is 10$ and user selects a frame it will add up 5$ let say and total will be 15$.
Now if i add another product it's selected custom frame price should get added.
e.g Product 1 price is: 10$ and the frame slected on it is:frame1 whos price is 5$ so total will be 15$ for that product and if Product 2 is added with price 10$ and selected frame2 with its price is 6$ total of that product will be 16$ however grandtotal will be 31$
The solution which is near to what i'm trying to do is:
add_filter( 'woocommerce_get_discounted_price', 'calculate_discounted_price', 10, 2 );
// Display the line total price
add_filter( 'woocommerce_cart_item_subtotal', 'display_discounted_price', 10, 2 );
function calculate_discounted_price( $price, $values )
{
$price += $_SESSION['framed_price'];
return $price;
}
}
I'm storing the frame value in session and it gets updated every time user click on the frame i'm using ajax on that and till that everything is working fine. i'm getting values as well.
This function is basically iterating over the added products and adding the last frame price to every product in the cart.
How do we add the product price with it's custom frame price?
I found an answer to this, that's solve the issue:
// Change the line total price
add_filter( 'woocommerce_get_discounted_price', 'calculate_discounted_price', 10, 2 );
// Display the line total price
add_filter( 'woocommerce_cart_item_subtotal', 'display_discounted_price', 10, 2 );
function calculate_discounted_price( $price, $values ) {
// You have all your data on $values;
$price += 10;
return $price;
}
// wc_price => format the price with your own currency
function display_discounted_price( $values, $item ) {
return wc_price( $item[ 'line_total' ] );
}
Reference: woocommerce, how can i add additional cost in cart product total price?

Categories