WooCommerce - "Edit Order" Functionality - php

Thank you for your time!
The below code does the following:
If a customer want to edit there order:
A credit for the full amount previously paid is applied
Customer can add/subtract items from there cart
System calculates the new subtotal
System calculates cart items tax
Subtracts the new total from the customer credit to get the final new total
The issue:
When the system is calculating the Tax it add the Subtotal + Credit * Tax Rate.
For Example:
Order Details:
Subtotal $100.00
Tax $4.48 (Subtotal - Credit * 8.8750%)
Credit -$49.61
Total $54.87
When its supposed to be:
Order Details:
Subtotal $100.00
Tax $8.88 (Subtotal * 8.8750%)
Credit -$49.61
Total $59.27
I tried adding a third argument:
$cart->add_fee( 'Credit', $credit, false);
Screenshot:
The code:
add_action( 'woocommerce_cart_calculate_fees', 'bbloomer_use_edit_order_total', 20, 1 );
function bbloomer_use_edit_order_total( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
$edited = WC()->session->get('edit_order');
if ( ! empty( $edited ) ) {
$order = new WC_Order( $edited );
$credit = -1 * $order->get_total();
$cart->add_fee( 'Credit', $credit);
}
}

Related

Woocommerce Checkout Page - Add Credit Card Surcharge After Tax Amount to Include Checkout Total with Tax

I located the following code that gets me close to what I need to see, but there are two problems.
The Surcharge is showing on the Cart and only needs to present on the Checkout Page.
The Surcharge is listed above the tax line and needs to include the tax.
Ideally, the Surcharge should display AFTER the Tax line. Also, Recurring Totals should include the Surcharge amount.
Checkout Page Example
This is the code suggested by Woocommerce and the instructions say it includes Tax, but it does not when applied. Link: Woocommerce Add a Surcharge to Cart and Checkout (https://woocommerce.com/document/add-a-surcharge-to-cart-and-checkout-uses-fees-api/?quid=4b03f1d2b1e8ee424e6c627eb96a7511#section-1)
Woocommerce code example
Add a surcharge based on the delivery country
/**
* Add a 1% surcharge to your cart / checkout based on delivery country
* Taxes, shipping costs and order subtotal are all included in the surcharge amount
*/
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$county = array('US');
$percentage = 0.01;
if ( in_array( $woocommerce->customer->get_shipping_country(), $county ) ) :
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );
endif;

Edit Order Functionality - WooCommerce

Thank you for your time!
The below code does the following:
If a customer want to edit there order:
A credit for the full amount previously paid is applied
Customer can add/subtract items from there cart
System calculates the new subtotal
System calculates cart items tax
Subtracts the new total from the customer credit to get the final new total
The issue:
When the system is calculating the Tax it add the Subtotal + Credit * Tax Rate.
For Example:
Order Details:
Subtotal $100.00
Tax $4.48 (Subtotal - Credit * 8.8750%)
Credit -$49.61
Total $54.87
When its supposed to be:
Order Details:
Subtotal $100.00
Tax $8.88 (Subtotal * 8.8750%)
Credit -$49.61
Total $59.27
I tried adding a third argument:
$cart->add_fee( 'Credit', $credit, false);
Screenshot:
The code:
add_action( 'woocommerce_cart_calculate_fees', 'bbloomer_use_edit_order_total', 20, 1 );
function bbloomer_use_edit_order_total( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
$edited = WC()->session->get('edit_order');
if ( ! empty( $edited ) ) {
$order = new WC_Order( $edited );
$credit = -1 * $order->get_total();
$cart->add_fee( 'Credit', $credit);
}
}

WooCommerce: Double discount on sale products with coupon

I want to double the discount for products on sale with a coupon code.
For example: The product is on sale with a 10% discount. If I add the coupon code doublediscount I want to double that discount to 20%.
The coupon discount should have limit of 15%.
So if a product is on sale with a 30% discount, the max added discount with the coupon code should be 15%. Resulting in a 45% discount on the regular price (sale + extra discount).
My code so far is this:
add_action( 'woocommerce_before_calculate_totals', 'double_saleprice_coupon' );
function double_saleprice_coupon( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
global $woocommerce;
$coupon_id = 'doublediscount';
// Loop through cart items (first loop)
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ){
// Check if product in cart is on sale
$product = $cart_item['data'];
$cart_item_regular_price = $cart_item['data']->get_regular_price();
$cart_item_sale_price = $cart_item['data']->get_sale_price();
$cart_item_diff = $cart_item_regular_price - $cart_item_sale_price;
$cart_item_per_cent = round( $cart_item_diff / $cart_item_regular_price * 100, 0 );
if ( $product->is_on_sale() && wc_pb_is_bundled_cart_item($cart_item) === false && $cart_item_per_cent < 15 ) {
echo 'on sale';
echo $cart_item_per_cent;
}
}
}
I loop through all cart items and check if they are on sale and if the discount is below 15%. If that's the case, I want to change the discount for these cart items.
If the cart item has a discount above 15% I don't want to do anything. So the coupon code doublediscount would apply 15% to them.
I just don't know how to add/change the discount of a cart item.
You can use the woocommerce_coupon_get_discount_amount hook instead in combination with the following coupon settings:
Set correctly your coupon code: doublediscount
Discount type: Percentage
Amount: 15
Steps applied in this answer:
Only if the specific coupon code matches and the product is on sale
If a product is not on sale, no discount will be applied (by the else condition equal to 0. However, if this doesn't apply, you can simply remove the else condition)
Current percentage discount of the on sale product is calculated.
If this is less than the maximum added discount (15),
then the discount is doubled
If this is more, the maximum discount added (15) will be applied automatically
So you get:
function filter_woocommerce_coupon_get_discount_amount( $discount, $price_to_discount , $cart_item, $single, $coupon ) {
// Returns true when viewing the cart page & only apply for this coupon
if ( is_cart() || is_checkout() && $coupon->get_code() == 'doublediscount' ) {
// Get an instance of the WC_Product object
$product = $cart_item['data'];
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// On sale
if ( $product->is_on_sale() ) {
// Regular price
$cart_item_regular_price = $product->get_regular_price();
// Sale price
$cart_item_sale_price = $product->get_sale_price();
// Calculate the percentage difference
$cart_item_diff = $cart_item_regular_price - $cart_item_sale_price;
$cart_item_percentage = round( $cart_item_diff / $cart_item_regular_price * 100, 0 );
// Get maximum added discount
$max_added_discount = $coupon->get_amount();
// Less than maximum added discount
if ( $cart_item_percentage < $max_added_discount ) {
$discount = round( ( $price_to_discount * $cart_item_percentage ) / 100, 0 );
}
} else {
$discount = 0;
}
}
}
return $discount;
}
add_filter( 'woocommerce_coupon_get_discount_amount', 'filter_woocommerce_coupon_get_discount_amount', 10, 5 );

WooCommerce add non-taxable fee and discount to cart calculates wrong tax amount

I have a WooCommerce site where products may have a non-taxable deposit fee, may have a delivery fee ($7.50), and may have a discount. Without applying the negative fee (discount) the tax is calculated correctly. Once I add the negative fee, the tax includes the non-taxable deposit fee in its calculation. I read somewhere that negative fees are not recommended. I also found this post but don't know if this applies here. Is there another way to accomplish this in the cart and also show in the orders, emails, etc.? FYI the tax rate is 15%. Here's the code I'm using:
function woocommerce_custom_fees( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
$item_data = $cart_item['data'];
$deposit = $item_data->get_attribute('deposit');
$delivery = $cart_item['delivery'];
if ( $deposit ) {
$total_deposit += $cart_item['quantity'] * $deposit;
}
if ( $delivery == 'deliver' ) {
$total_delivery += $cart_item['quantity'] * 7.5;
}
}
if ( $total_deposit > 0 ) {
// non-taxable
$cart->add_fee( 'Deposit', $total_deposit, FALSE );
}
if ( $total_delivery > 0 ) {
// taxable
$cart->add_fee( 'Delivery', $total_delivery, TRUE );
}
// test $10 discount
$cart->add_fee( 'Test discount', -10.00);
}
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_fees', 25, 1 );
correct tax amount without negative fee
incorrect tax amount with negative fee
UPDATE: I found this post Apply a discount on the cart content total excluding taxes in WooCommerce which says that using a negative fee will cause the taxes to always get applied. Is there an alternative method to apply discounts in the cart other than to use negative fees or coupons?
function woocommerce_custom_fees( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// non-taxable
$cart->add_fee( 'Deposit', 6, FALSE );
// taxable
$cart->add_fee( 'Delivery', 7, TRUE );
// test $10 discount
//$cart->add_fee( 'Test discount', -10.00 , FALSE);
}
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_fees', 25, 1 );
add_filter( 'woocommerce_calculated_total', 'discounted_calculated_total', 10, 2 );
function discounted_calculated_total( $total, $cart ){
$total = $total - 10;
return $total;
}
Tried this way?

Add custom fee based on total weight in Woocommerce

In WooCommerce, I am trying to add a an additional shipping fee based on cart weight.
For the first 1500g the fee is 50$.
Above 1500g we add 10$ to this initial $50 by steps of 1000g
So for example:
if cart weight is 700g we add a fee of $50,
if cart weight is 2600g we add a fee of $70 ($50 + $10 +$10) …
I am stuck on the calculation:
function weight_add_cart_fee() {
$feeaddtocart = get_option('feeaddtocart');
$customweight = get_option('customweight');
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$cart_weight = WC()->cart->get_cart_contents_weight();
if ($cart_weight <= 500 ) {
$get_cart_total = $woocommerce->cart->get_cart_total();
$newtotal = $get_cart_total + 50;
WC()->cart->add_fee( __('Extra charge (weight): ', 'your_theme_slug'), $newtotal, false );
}
}
How can I achieve this? Any help is appreciated.
It can be done very easily with a custom function hooked in woocommerce_cart_calculate_fees action hook…
Updated:
Added conversion of cart weight in grams (instead of kilos by default)
Now for the first 1500g the fee is 50$ (instead of 500g)
Now above 1500g it add $10 by steps of 1000g.
add_action( 'woocommerce_cart_calculate_fees', 'shipping_weight_fee', 30, 1 );
function shipping_weight_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Convert cart weight in grams
$cart_weight = $cart->get_cart_contents_weight() * 1000;
$fee = 50; // Starting Fee below 500g
// Above 500g we add $10 to the initial fee by steps of 1000g
if( $cart_weight > 1500 ){
for( $i = 1500; $i < $cart_weight; $i += 1000 ){
$fee += 10;
}
}
// Setting the calculated fee based on weight
$cart->add_fee( __( 'Weight shipping fee' ), $fee, false );
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works.

Categories