As you can see in the image, I have a cart that has two products.
Each of the products has some addons.
The "addon#1" costs 20chf.
If a customer chooses 3 "test product 1" products and adds 2 "addon#1" addons,
I want to calculate first the quantity and then to add the cost of the addons.
For example, in the image, for the first item in the cart, 120 * 3 = 360 and then 2 addons * 20 = 40,
I want in Total to have 400.
I tried some actions like woocommerce_after_calculate_totals,
woocommerce_cart_item_subtotal,
woocommerce_cart_product_subtotal but even if I got the results I want, the total cost of the cart is not the right, it doesn't include the addons.
function add_addons_to_total_price( $product_subtotal, $product, $quantity, $cart ) {
$subtotal_product_price = 0;
$product_price = $product->get_price();
$subtotal_product_price = $product_price * $quantity;
foreach( $cart->get_cart() as $key=>$value ) {
if( isset( $value['total_addons_price'] ) ) {
$subtotal_product_price += $value['total_addons_price'];
}
}
return ($subtotal_product_price) ? wc_price($subtotal_product_price) : $product_subtotal;
}
add_filter( 'woocommerce_cart_product_subtotal', 'add_addons_to_total_price', 10, 4 );
Do you have any ideas on how to change the total cost of each item in cart and update the total cost of the cart?
Related
I want to split cart items of same product in individual lines. When I increase quantity on single product page and add to cart, it shows as separate cart items.
Im using WooCommerce - Treat cart items separate if quantity is more than 1 answer code.
I want when quantity is updated on cart page and clicked on 'Update cart', then items must split on individual lines.
How can I do that?
The code below will split items on individual lines when quantity is updated on the cart page.
Comment with explanation added to the code.
function on_action_cart_updated( $cart_updated ) {
if ( $cart_updated ) {
// Get cart
$cart_items = WC()->cart->get_cart();
foreach ( $cart_items as $cart_item_key => $cart_item ) {
$quantity = $cart_item['quantity'];
// If product has more than 1 quantity
if ( $quantity > 1 ) {
// Keep the product but set its quantity to 1
WC()->cart->set_quantity( $cart_item_key, 1 );
// Run a loop 1 less than the total quantity
for ( $j = 1; $j <= $quantity -1; $j++ ) {
// Set a unique key.
$cart_item['unique_key'] = md5( microtime() . rand() . "Hi Mom!" );
// Get vars
$product_id = $cart_item['product_id'];
$variation_id = $cart_item['variation_id'];
$variation = $cart_item['variation'];
// Add the product as a new line item with the same variations that were passed
WC()->cart->add_to_cart( $product_id, 1, $variation_id, $variation, $cart_item );
}
}
}
}
}
add_action( 'woocommerce_update_cart_action_cart_updated', 'on_action_cart_updated', 20, 1 );
The following working code is a discount for cart with minimum 3 items not including 'sale' products Now I need to ignore categoty 'gift' (if it is in the cart) from this discount:
//10% discount at minimum 3 items in cart not for sale items
add_action('woocommerce_cart_calculate_fees' , 'custom_cart_discount', 20, 1);
function custom_cart_discount( $cart ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Limitations: Only when there is 5 or more non on sale items in cart
$starting_limit = 3;
// Initialising variables
$not_on_sale_subtotal = $discount = $items_count = 0;
// Iterating through each item in cart
foreach( $cart->get_cart() as $cart_item ){
// For cart items is not on sale
if( ! $cart_item['data']->is_on_sale() ){
$not_on_sale_subtotal += (float) $cart_item['line_subtotal'];
$items_count += $cart_item['quantity'];
}
}
// Discount calculation
$discount = $not_on_sale_subtotal * 0.1;
// Applied discount only cart items that are not on sale
if( $discount && $items_count >= $starting_limit )
$cart->add_fee( 'הנחת כמות 10%', -$discount );
I would recommend to work with a different approach. WooCommerce Coupons gives you a full control for what you want to achieve.
First, create a coupon in WC admin, with the 10% discount. Then, use the coupon settings to apply it on the cart or just on specific items/categories. You can see in the coupon limitations how exactly to limit the discount to specific categories (with include / exclude ), or to apply the discount only on items that are not on sale etc...
Finally, all you have to do, is just to ask or encourage the customer to apply the coupon, or automatically apply it yourself behind the scenes with:
WC()->cart->apply_coupon('YouCouponCodeGoesHere');
בהצלחה!
For a client, I need to let backorders for products but they don't want to charge for those. So, I need to update the price dynamically if they stock is 0 or the stock is less than the quantity on the cart.
This is my code:
add_filter( 'woocommerce_cart_product_subtotal', 'modify_cart_product_subtotal', 10, 4 );
function modify_cart_product_subtotal( $product_subtotal, $product, $quantity, $cart ) {
if ( $product->get_stock_quantity() < $quantity ) {
return $product->get_stock_quantity() * $product->get_price();
}
return $product_subtotal;
}
In case of 0 stock, it works like a charm. In case of a quantity greater than the stock, it charges only for the available products, but the tax keeps increasing even for the rest. How can I exclude those from taxes calculations as well?
EDIT:
If I just update the price of the product, it does it for all of them, not only those above the available stock.
function zero_price_for_backorders( $cart_object ) {
global $isProcessed;
if( !WC()->session->__isset( "reload_checkout" )) {
foreach ( $cart_object->get_cart() as $key => $value ) {
if ( $value['data']->get_stock_quantity() < $value['quantity']) {
$value['data']->set_price(0);
}
}
$isProcessed = true;
}
}
add_action( 'woocommerce_before_calculate_totals', 'zero_price_for_backorders', 99 );
You should use before_calculate_total and change product price using set_price function so that you don't have to modify subtotal separately. The rest of the flow will fall in place automatically.
You can have a look at this link on how to change it dynamically:
Change product price dynamically woocommerce
How can I set the maximum weight per product (not per order)?
The customer can buy any number (quantity) of products until the max weight is reached. If he buys 5 products, The weight of total unit of each product cant reach max weight. There can be many products in an order but Total units(quantity) per product has Max weight.
For example, In a single order:
3 units of product A (weighing total 2kg)
5 units of product B (weighing total 1.4kg)
3 units of product C (weighing total 2kg)
In this example, the hooked function is triggered on add to cart action and you can perform all kind of checks to validate or not the action (and display a custom error message).
So you will see that you can target the total item weight individually…
add_filter( 'woocommerce_add_to_cart_validation', 'custom_add_to_cart_validation', 20, 5 );
function custom_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = '', $variations = '' ) {
// HERE define the weight limit per item
$weight_limit = 2; // 2kg
$total_item_weight = 0;
// Check cart items
foreach( WC()->cart->get_cart() as $cart_item ) {
$item_product_id = empty($variation_id) ? $product_id : $variation_id;
// If the product is already in cart
if( $item_product_id == $cart_item['data']->get_id() ){
// Get total cart item weight
$total_item_weight += $cart_item['data']->get_weight() * $cart_item['quantity'];
}
}
// Get an instance of the WC_Product object
$product = empty($variation_id) ? wc_get_product($product_id) : wc_get_product($variation_id);
// Get total item weight
$total_item_weight += $product->get_weight() * $quantity;
if( $total_item_weight > $weight_limit ){
$passed = false ;
$message = __( "Custom warning message for weight exceed", "woocommerce" );
wc_add_notice( $message, 'error' );
}
return $passed;
}
You will need also an additional hooked function that will be triggered on cart item quantity change:
add_filter( 'woocommerce_after_cart_item_quantity_update', 'limit_cart_item_quantity', 20, 4 );
function limit_cart_item_quantity( $cart_item_key, $new_quantity, $old_quantity, $cart ){
// HERE define the weight limit per item
$weight_limit = 2; // 2kg
// Get an instance of the WC_Product object
$product = $cart->cart_contents[ $cart_item_key ]['data'];
$product_weight = $product->get_weight(); // The product weight
// Calculate the limit allowed max quantity from allowed weight limit
$max_quantity = floor( $weight_limit / $product_weight );
// If the new quantity exceed the weight limit
if( ( $new_quantity * $product_weight ) > $weight_limit ){
// Change the quantity to the limit allowed max quantity
$cart->cart_contents[ $cart_item_key ]['quantity'] = $max_quantity;
// Add a custom notice
$message = __( "Custom warning message for weight exceed", "woocommerce" );
wc_add_notice( $message, 'notice' );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
I found this answer in a earlier post here (below), but I want to know of there is any way to add an extra fee based on quantity if the product quantity changes?
Lets say there is 100 items in one package. (the problem is also that there is not they same amount of item in all packages, some can be 100, some can be 150, 200, or 500)
Example:
1-99 = 1$.
100 = no fee.
101 - 199 1$
200 = no fee
201 - 299 = 1$ and so on..
Total will always be 1$ per product but the total can be more if they order several products that have these breaks. The total can be 4$ if there is 4 products with break-cost.
(Also, not sure where to put the code)
Thank you!
The code I found here: Add additional costs based on quantity in Woocommerce
// Hook before adding fees
add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');
/**
* Add custom fee on article specifics
* #param WC_Cart $cart
*/
function add_custom_fees( WC_Cart $cart ){
$fees = 0;
foreach( $cart->get_cart() as $item ){
// Check if odds and if it's the right item
if( $item[ 'quantity' ] % 2 == 1 && get_post_meta( $item[ 'product_id' ], 'custom_fee_for_supplier_name', true) ){
// You can also put a custom price in each produt with get_post_meta
$fees += 10;
}
}
if( $fees != 0 ){
// You can customize the descriptions here
$cart->add_fee( 'Custom fee (odds paquets)', $fees);
}
}
The woocommerce_after_cart_item_quantity_update fires right after a quantity is updated. If you modify your function a little bit (to use WC()->cart to access the cart object) you can run the same function on both hooks. I thought it might keep adding additional fees, but in my testing it seems to just recalculate the right cost for the same fee.
add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');
add_action( 'woocommerce_after_cart_item_quantity_update', 'add_custom_fees' );
/**
* Add custom fee on article specifics
* #param WC_Cart $cart
*/
function add_custom_fees(){
$fees = 0;
foreach( WC()->cart->get_cart() as $item ){
// Check if odds and if it's the right item
if( $item[ 'quantity' ] % 2 == 1 && get_post_meta( $item[ 'product_id' ], '_custom_fee_for_odds', true ) ){
// You can also put a custom price in each produt with get_post_meta
$fees += 10;
}
}
if( $fees > 0 ){
// You can customize the descriptions here
WC()->cart->add_fee( 'Custom fee (odds paquets)', $fees);
}
}