woocommerce additional fees based on product input - wordpress

The products contain 2 different Attributes (packaging= crate/container) and I have to put 3 different fee and name for 2 different attributes . The fee also need Multiply Fee by Quantity and display name & fee 1 by 1 on cart page and checkout page. In addition, the fee on cart page and checkout page will auto calculate to the total price . Any plugin or add the code for this problem? thank you.

add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 0.01;
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );
}
Here is a sample code on how to add fee

Related

Woocommerce Carriage Surcharge

so I found some code via
Global surcharge WooCommerce exclude VAT
I've modified this to suit my requirement:
// Packing fee
add_action( 'woocommerce_cart_calculate_fees','wc_add_surcharge' );
function wc_add_surcharge( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
/** Declare variable for surcharge custom field state **/
$surcharge_status = get_field('surcharge');
/** Assign surcharge_fee to fee **/
$fee = $surcharge_status;
$cart->add_fee( 'Surcharge', $fee );
}
I'm trying to modify the above to take a value (surcharge) from the product (custom field, as a select value - in this case referenced by get_field) then add that as a fee to the cart. For some reason, it doesn't recognise the surcharge code above and displays '0.00'. Is there any way around this.

WooCommerce: conditional handling fee that is not applied to only one coupon code

I have implemented code that will charge a $1.50 handling fee to all WooCommerce products unless a coupon code is used. However, I only want the handling fee to be removed for a single coupon as opposed to when any coupon is used. Thoughts? Below is my current code and the only coupon that should not have the handling fee applied is QFPZ8KSS (the post ID for the coupon is 4432 if that is required).
add_action( 'woocommerce_cart_calculate_fees','conditional_handling_fee' );
function conditional_handling_fee() {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Get the applied coupons + the count (in cart)
$applied_coupons_arr = WC()->cart->get_applied_coupons();
$applied_coupons_count = count($applied_coupons_arr);
$fee = 1.50;
if( 0 == $applied_coupons_count )
WC()->cart->add_fee( ''.$applied_coupons_count, $fee, true, 'standard' );
}
You just need to compare the coupon used in your order against your free shipping one. I think the below should work, bear in mind I haven't tested this. Hopefully it will help.
<?php
add_action( 'woocommerce_cart_calculate_fees','conditional_handling_fee' );
function conditional_handling_fee() {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Get the applied coupons + the count (in cart)
$applied_coupons_arr = WC()->cart->get_applied_coupons();
$fee = 1.50;
$applied_coupon_ids;
// Coupons used in the order loop
foreach( $applied_coupons_arr as $coupon ){
// Get Coupon ID
$coupon_post_obj = get_page_by_title($coupon, OBJECT, 'shop_coupon');
$coupon_id = $coupon_post_obj->ID;
// Store in array
array_push($applied_coupon_ids, $coupon_id);
};
// Check if your free shipping coupon is used
if(! in_array('COUPON_ID_HERE', $applied_coupon_ids)){
WC()->cart->add_fee( ''.$applied_coupons_count, $fee, true, 'standard' );
};
?>

exclude a category from woocommerce discount

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');
בהצלחה!

Woocommerce: recognize custom fee as "shipping" specific charge

I know I can add a fee to the cart using the below code, but it just adds a generic fee to the cart. Woocommerce recognizes shipping specific fees and I am wondering if there is a way to make the fees addes via the below method something that woocommerce sees as a "shipping" specific fee. Some sort of metadata that gets added to shipping specific fees. our API is not recognizing these fees as shipping charges and therefore does not log them and that is what I am trying to resolve.
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 0.01;
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );
}
You can do something like this:
add_filter( 'woocommerce_package_rates', 'woocommerce_package_rates', 10, 2 );
function woocommerce_package_rates( $rates, $package ) {
$percentage = 0.01;
foreach($rates as $key => $rate ) {
$surcharge = ( wc()->cart->cart_contents_total + $rates[$key]->cost ) * $percentage;
$rates[$key]->label .= ": {$rates[$key]->cost} + Fee: {$surcharge}";
$rates[$key]->cost += $surcharge;
}
return $rates;
}
add the surcharge to the rates of shipping. Code above will produce something like this:
More about shipping fees here: http://reigelgallarde.me/programming/woocommerce-shipping-fee-changes-condition-met/

Add dynamic conditional fee based on radio buttons in Woocommerce checkout

In woocommerce, I am trying to add a fee using this answer code:
Update fee dynamically based on radio buttons in Woocommerce checkout (credits: LoicTheAztec)
I need to update the code to add third conditional option to the fee like "ribbon" in this hooked function:
// Add a custom dynamic packaging fee
add_action( 'woocommerce_cart_calculate_fees', 'add_packaging_fee', 20, 1 );
function add_packaging_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$packing_fee = WC()->session->get( 'chosen_packing' ); // Dynamic packing fee
$fee = $packing_fee == 'box' ? 9.00 : 3.00;
$cart->add_fee( __( 'Packaging fee', 'woocommerce' ), $fee );
}
But I am stuck trying to add another entry with this line of code:
$fee = $packing_fee == 'box' ? 9.00 : 3.00;
How can I add an additional fee option?

Resources