Organizational Research By

Surprising Reserch Topic

add a fee to woocommerce per product based on category using -'php,wordpress,wordpress-plugin,woocommerce'


add a fee to woocommerce per product based on category  using -'php,wordpress,wordpress-plugin,woocommerce'

I've been trying for a while now to get this working but I haven't find any solution that does exactly what we need, and I'm far from an expert in PHP so I'm a bit lost.

We use WooCommerce and WooTickets. The goal is to add a 5% fee for "Service Fee" only to products in the "Tickets" category (ID:34).

We have found this code snipper, that add a fixed cost based on a product category :

// Add Service Fee to Category
function woo_add_cart_fee() {

$category_ID = '23';
global $woocommerce;

foreach ($woocommerce->cart->cart_contents as $key => $values ) {
    // Get the terms, i.e. category list using the ID of the product
$terms = get_the_terms( $values['product_id'], 'product_cat' );
    // Because a product can have multiple categories, we need to iterate through the list of the products category for a match
    foreach ($terms as $term) {
        // 23 is the ID of the category for which we want to remove the payment gateway
        if($term->term_id == $category_ID){
         $excost = 6;
         }
         }
        $woocommerce->cart->add_fee('Service Fee', $excost, $taxable = false, $tax_class = '');
}
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );


The main problem with this solution is that it adds a fixed cost, whereas we need a percentage cost.

We also found this code snippet from WooThemes themselves :

/**
 * Add a 1% surcharge to your cart / checkout
 * change the $percentage to set the surcharge to a value to suit
 * Uses the WooCommerce fees API
 *
 * Add to theme functions.php
 */
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
  global $woocommerce;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $percentage = 0.05;
    $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;    
    $woocommerce->cart->add_fee( 'Service Fee', $surcharge, true, 'standard' );

}


But once again, there are a few problems with this solution...

1) The product category is not taken into consideration
2) It adds a fee based on the entire cart value, but it should only add a 5% fee to products in the "Tickets" product category, not the entire cart
    

asked Sep 8, 2015 by rajesh
0 votes
76 views



Related Hot Questions

3 Answers

0 votes
I was having the same issue and came across a code snippet that set me on the correct path. For the life of me, I cannot find the original site that housed the snippet, but here is my reworked version. function df_add_ticket_surcharge( $cart_object ) { global $woocommerce; $specialfeecat = 86; // category id for the special fee $spfee = 0.00; // initialize special fee $spfeeperprod = 0.05; //special fee per product foreach ( $cart_object->cart_contents as $key => $value ) { $proid = $value['product_id']; //get the product id from cart $quantiy = $value['quantity']; //get quantity from cart $itmprice = $value['data']->price; //get product price $terms = get_the_terms( $proid, 'product_cat' ); //get taxonamy of the prducts if ( $terms && ! is_wp_error( $terms ) ) : foreach ( $terms as $term ) { $catid = $term->term_id; if($specialfeecat == $catid ) { $spfee = $spfee + $itmprice * $quantiy * $spfeeperprod; } } endif; } if($spfee > 0 ) { $woocommerce->cart->add_fee( 'Ticket Concierge Charge', $spfee, true, 'standard' ); } } add_action( 'woocommerce_cart_calculate_fees', 'df_add_ticket_surcharge' ); This will add a 5% surcharge to each item in the cart with the category of ticket (id: 86). I hope this helps you out, if you have not found a solution already.
answered Sep 8, 2015 by rajesh
0 votes
thanks for posting this above code. I had needed exactly this, but there were some problems for, me. I didn't want the category to be a part of it, i wanted it to apply to all products, and I also required a fixed fee per product. The other problem I had was if I had 2 different products in the cart it would only calculate it as 1 product. so I did some research and found a way for it to calculate the total number of products in the cart. So here is the final code I came out with. Thought it may help someone else looking for a similar answer /** add handling fee **/ function df_add_handling_fee( $cart_object ) { global $woocommerce; // $specialfeecat = 3711; // category id for the special fee $spfee = 0.00; // initialize special fee $spfeeperprod = 10; //special fee per product //Getting Cart Contents. $cart = $woocommerce->cart->get_cart(); //Calculating Quantity foreach($cart as $cart_val => $cid){ $qty += $cid['quantity']; } foreach ( $cart_object->cart_contents as $key => $value ) { $proid = $value['product_id']; //get the product id from cart //$quantiy = $value['quantity']; //get quantity from cart $itmprice = $value['data']->price; //get product price $terms = get_the_terms( $proid, 'product_cat' ); //get taxonamy of the prducts if ( $terms && ! is_wp_error( $terms ) ) : foreach ( $terms as $term ) { //$catid = $term->term_id; //if($specialfeecat == $catid ) { $spfee = $qty * $spfeeperprod; //} } endif; } if($spfee > 0 ) { $woocommerce->cart->add_fee( 'Handling Fee', $spfee, true, 'standard' ); } } add_action( 'woocommerce_cart_calculate_fees', 'df_add_handling_fee' ); As you can see I have just commented out the parts from the original code. Hope someone can benefit from this :)
answered Sep 8, 2015 by rajesh
0 votes
Awesome snippet thank you. The only problem is that it doesn't add a charge to multiple products in the cart. It's brilliant when you have 10x one item for example but if you add 1x another item, it just defaults to the most recent item added. Any chance this can effect all the products in the cart which are in the same category? Hard to explain! // BEFORE USING: Move the vantage-child theme into the /themes/ folder. // // You can add your own actions, filters and code below. function df_add_ticket_surcharge( $cart_object ) { global $woocommerce; $specialfeecat = 31; // category id for the special fee $spfee = 0.00; // initialize special fee $spfeeperprod = 0.05; //special fee per product foreach ( $cart_object->cart_contents as $key => $value ) { $proid = $value['product_id']; //get the product id from cart $quantiy = $value['quantity']; //get quantity from cart $itmprice = $value['data']->price; //get product price $terms = get_the_terms( $proid, 'product_cat' ); //get taxonamy of the prducts if ( $terms && ! is_wp_error( $terms ) ) : foreach ( $terms as $term ) { $catid = $term->term_id; if($specialfeecat == $catid ) { $spfee = $spfeeperprod * $quantiy; } } endif; } if($spfee > 0 ) { $woocommerce->cart->add_fee( 'Washing Up Charge', $spfee, true, 'standard' ); } } add_action( 'woocommerce_cart_calculate_fees', 'df_add_ticket_surcharge' ); Thanks @BartoszKP @thisisbbc @Sartorius @user3674860
answered Sep 8, 2015 by rajesh

...