I would like to add a fee to certain categories if the total number of products from all the defined category is 1 or separate fee if the quantity is 2-9.. (IE. + $10 to the product if they only order 1, +$5 per product if they order 2-9 items).
I started on this base: Custom Fee based on product dimensions and categories
I have maid some changes, but I can't get it working and I am stuck.
Here is my code:
add_action( 'woocommerce_cart_calculate_fees','custom_applied_fee');
function custom_applied_fee() {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Set HERE your categories (can be an ID, a slug or the name… or an array of this)
$category1 = 'plain';
$category2 = 'plywood';
// variables initialisation
$fee = 0;
// Iterating through each cart item
foreach(WC()->cart->get_cart() as $cart_item){
// Get the product object
$product = new WC_Product( $cart_item['product_id'] );
$quantiy = $value['quantity']; //get quantity from cart
// Initialising variables (in the loop)
$cat1 = false; $cat2 = false;
// ## CALCULATIONS ## (Make here your conditional calculations)
$quantity = GET TOTAL QUANTITY
if($quantity <= 1){
$fee += 10 * $quanity;
} elseif($quantity > 1 && $dimention <= 9){
$fee += 5 * $quanity;
} elseif($dimention > 10){
$fee += 1 * $quanity;
}
}
// Adding the fee
if ( $fee != 0 )
WC()->cart->add_fee( $fee_text, $fee, true );
}
What I am doing wrong? How can I get this working?