I'm currently adding a feee on checkout;
add_action( 'woocommerce_cart_calculate_fees', 'custom_cart_total' );
function custom_cart_total($order_total) {
WC()->cart->add_fee( __('Military Discount', 'woocommerce'), -$totalDiscount );
}
I have some additional conditional logic to add where I may want to remove this specific fee Military Discount. I don't see an option of remove_fee. I could 0 out the discount but then it will show on the invoice which I don't want if it is 0.
How can I remove this discount based on some of my stores logic?
Further details -
I could only add the discount based on logic instead of removing it however, this WooCommerce store is unique in the fact the shopping is done in-store. We have issues where customers are getting the discount due to cache/cookies/ or sessions. So I'm looking to force clear all the discounts for the next user.
Related
Currently, using the WooCommerce Smart Coupons plugin, when combining two different coupons, a store credit is used first and then a percentage is used on the total after the first coupon is used.
This is illogical when you give someone store credit and they want to take that amount off of the cart total AFTER using the full % off. I'd like for this to work the opposite - use the percentage off coupon, then the store credit.
To be clear: The way it works now is...
Cart subtotal = $100
$10 Store credit coupon = -$10
10% off coupon = -$9
Cart total = $81
The way it SHOULD work is...
Cart subtotal = $100
$10 Store credit coupon = -$10
10% off coupon = -$10
Cart total = $80
Does anyone else have this issue? If so, how can it be fixed? Thanks!
EDIT 1: I've narrowed down this issue to being with the Smart Coupons plugin after disabling it and the calculations working as they should once disabled.
The solution for me was to UNCHECK a line item in the Woocommerce settings under the "General" tab and "Enable Coupons" section that says "Calculate coupon discounts sequentially"
We sell trails and the trails are "products" in WooCommerce. >>
Check this link
Try clicking on the "Let's Play" link and it will take you to the checkout. >> (Screenshot)
The price of each trail is set to zero (in WooCommerce admin) because the pricing is "per person".
We had to use the following code snippet to make the products "work", with the "$0" price.
add_filter( 'woocommerce_is_purchasable', 'wpa_109409_is_purchasable', 10, 2 );
function wpa_109409_is_purchasable( $purchasable, $product ){
if( $product->get_price() >= 0 )
$purchasable = true;
return $purchasable;
}
After making the products "work", with the "$0" price, we successfully added some required fields on the checkout page which are used for adding a "fee" for every "person", the visitor will select.
The order total will only update when the user will select the number of players field on the checkout page, notice the "Select No of Players" options.
(Screenshot)
The Issue:
Whenever we apply a "Coupon" (For example "enjoy10" - 10% discount), it only applies to the product price, which is currently set-to "$0".
(Check this Screen Record)
So, technically, the coupon code doesn't work and applied only "$0" discount because the coupon doesn't apply on the order total, it only applies to the product price, which is set to "$0", because we're charging on a per person basis.
I want the coupon code to apply to the final order total, not just the product price.
In default, the coupon will be applied to subtotal or on the product price. Subtotal means the total amount of product.
When you submit some additional fields in the checkout, the additional amount will be added as fee in WooCommerce. The fee is the same as the coupon. Using fee, we can increase or reduce the total amount. When we apply a coupon, WooCommerce applies a negative fee on the subtotal and that so the total amount is reduced.
The options possible are either change your coupon amount as per the latest fee or add a negative fee (additional discount) for the checkout field fee by calculating the amount of coupon.
I am using woocommerce subscription with the gifting extension. With this it is possible to gift a subscription to a recipient. I am trying to make it possible to gift a subscription to multiple people in one order.
The tricky thing is that each product by default can only be gifted to 1 person. So in order to gift to multiple people, the same product has to be added to the cart multiple times.
With the use of this thread: WooCommerce - Treat cart items separate if quantity is more than 1
I have managed to add the same product to the cart and show in on multiple lines. So if I enter quantity 5 of my subscription product and add it to the cart I get five entries in my cart. Which is great, as now I can gift every line to another person.
However, when I proceed to checkout it gets messed up again. Instead of showing the product on separate lines, everything get jammed together. Thus on checkout I get to see 1 line with quantity 5, instead of 5 lines with quantity 1.
This is where the question comes in:
How can I make sure that the order review table on the checkout page shows separate lines for a product with quantity x>1 instead of 1 line with quantity x?
Remark:
I figured out that it only does not work when the woocommerce subscription gifting add-on is active. It overrides seem to override the wc_cart_item_data
you can use this code
function separate_individual_cart_items( $cart_item_data, $product_id ) {
$unique_cart_item_key = md5( microtime() . rand() );
$cart_item_data['unique_key'] = $unique_cart_item_key;
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'separate_individual_cart_items', 10, 2 );
you can put this code into functions.php or into yout own plugin ( a best solution).
And a observation because this question is similar like this question
WooCommerce - Treat cart items separate if quantity is more than 1
but I can't close the question, sorry... my reputation is low)
I wanted to implement a buy 3 free 1 feature, so I wrote a script that detect whether customer has 3 same items in cart and automatically add 1 more same item to the cart. Then using another hook, I overwrite the price of the product to 0.
I googled the solution and used the same approach found on:
WooCommerce: Add product to cart with price override?
woocommerce add custom price while add to cart
Here is the code sample:
function setGiftPriceToZero($cart_object){
foreach($cart_object->cart_contents as $k=>$item):
if(isset($item['variation']['promo']) && ($item['variation']['promo']) == 'buy 3 free 1'):
$item['data']->price = 0;
endif;
endforeach;
}
add_action('woocommerce_before_calculate_totals', 'setGiftPriceToZero');
When Woocommerce calculate the subtotal for the cart, it always add in the original price of the product that is supposed to be free. For example, when I added 3 $100 item to cart, the cart subtotal ends up with $400 instead of $300.
I digged deeper into the Woocommerce code and found that in https://docs.woocommerce.com/wc-apidocs/source-class-WC_Cart.html#1139, $item['data']->get_price() is used which always return the original price for the item.
Is there anyway to fix this using hooks/apis instead of editing Woocommerce core file?
I have found the culprit of this error. It's caused by conflict from another plugin called Woocommerce Role Based Price. This plugin overwrite the cart item price at the end of the cart total calculation flow. This is why get_price() function always return the specified price for the item.
Now I only have to edit the plugin file so that it plays nicely with my logic.
Here's the formula:
(raw material cost) + (fee_1 for processing) + (fee_2 for other processes) = final price.
+What I need is:
Set the (raw material cost) to a number globally, change it accordingly, basically weekly;
(fee_1) is set in the product admin page as an attribute;
same with (fee_2).
+My problem is:
dont have a clue about how to set a global material cost to the database with a admin frontend page.
the (fee_1) and (fee_2) seem like easier, I've read some pages, just need to test it.
Any one can provide me with some links or guides about how to get that?
High appriciated!
Michael
You can use this code to functions.php to add raw material cost to the whole cart and you can update it every week.
function woo_add_cart_fee() {
global $woocommerce;
$woocommerce->cart->add_fee( __('Custom', 'woocommerce'), 5 );
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
But this won't be for every product and will be added to the whole cart no matter how many products there are.
To add raw material cost to each product based on the quantity, a much better way would be to use this plugin "Woocommerce product fee"