Asked  1 Year ago    Answers:  5   Viewed   8 times

I already found this code here and it works for like 80/90% for me (see below).

This code adds 60 euro to my cart when there is a product from category ID 349 in the cart. When I add a product from that category to my cart when the cart is empty it works fine. But when there is already a product in my cart from a different category and then I add the product with category 349 it doesn't add the 60 euro extra fee. How is this possible?

 function woo_add_cart_fee() {

$category_ID = '349';
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) {
        // 349 is the ID of the category for which we want to remove the payment gateway
        if($term->term_id == $category_ID){
         $excost = 60;
         }
         }
        $woocommerce->cart->add_fee('Extra bezorgkosten kunstgras', $excost, $taxable = false, $tax_class = '');
}
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );

 Answers

1

The code you are using is a bit outdated and you should use has_term() Wordpress conditional function to target a product category this way:

add_action( 'woocommerce_cart_calculate_fees','custom_pcat_fee', 20, 1 );
function custom_pcat_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Set HERE your categories (can be term IDs, slugs or names) in a coma separated array
    $categories = array('349');
    $fee_amount = 0;

    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item ){
        if( has_term( $categories, 'product_cat', $cart_item['product_id']) )
            $fee_amount = 60;
    }

    // Adding the fee
    if ( $fee_amount > 0 ){
        // Last argument is related to enable tax (true or false)
        WC()->cart->add_fee( __( "Extra bezorgkosten kunstgras", "woocommerce" ), $fee_amount, false );
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

A fee of 60 will always be added if there is in cart an item from 349 product category ID.

Thursday, April 1, 2021
 
EurekA
 
4

It can be done very easily with a custom function hooked in woocommerce_cart_calculate_fees action hook…

Updated:

  • Added conversion of cart weight in grams (instead of kilos by default)
  • Now for the first 1500g the fee is 50$ (instead of 500g)
  • Now above 1500g it add $10 by steps of 1000g.
add_action( 'woocommerce_cart_calculate_fees', 'shipping_weight_fee', 30, 1 );
function shipping_weight_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Convert cart weight in grams
    $cart_weight = $cart->get_cart_contents_weight() * 1000;
    $fee = 50; // Starting Fee below 500g

    // Above 500g we add $10 to the initial fee by steps of 1000g
    if( $cart_weight > 1500 ){
        for( $i = 1500; $i < $cart_weight; $i += 1000 ){
            $fee += 10;
        }
    }
    // Setting the calculated fee based on weight
    $cart->add_fee( __( 'Weight shipping fee' ), $fee, false );
}

Code goes in function.php file of your active child theme (or active theme).

Tested and works.

Thursday, April 1, 2021
 
muffe
 
3

The code that you are using works just perfectly. Your code is a bit incomplete too:

add_filter( 'template_include', 'custom_single_product_template_include', 50, 1 );
function custom_single_product_template_include( $template ) {
    if ( is_singular('product') && (has_term( 'custom', 'product_cat')) ) {
        $template = get_stylesheet_directory() . '/woocommerce/single-product-mock.php';
    } 
    return $template;
}

So the problem could be related to:

  1. The location of that custom template, that should be in a 'woocommerce' folder inside your active child theme (or inside your active theme).
  2. Woocommerce support need to be enable for your theme.

Solutions:

  1. be sure that inside your active child theme (or active theme) there is a "woocommerce" folder and add inside it your custom template single-product-mock.php
    (but not inside a "templates" sub-folder)
  2. To check Woocommerce support is enabled:
    • Copy the default single-product.php template inside the "woocommerce" folder located in your active child theme (or active theme)
    • Go in backend Woocommerce > Status … and you will normally see in "templates" section (at the end of this page):

      With an active child-theme:
      With an active theme:
    • If is not enable, you should search and look to the related Woocommerce documentation.

One of those, should solve your issue.

Saturday, May 29, 2021
 
4

The right hook to do that is woocommerce_before_calculate_totals using has_term() WordPress conditional function to filter product categories in cart items. This way you can null the price for that cart items.

This is the code (added compatibility for Woocommerce 3+):

add_action( 'woocommerce_before_calculate_totals', 'custom_price_product_category', 20, 1 );
function custom_price_product_category( $cart ) {

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

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    foreach ( $cart->get_cart() as $cart_item ) {
        // When a product has 'glass' as category we null the price.
        if( has_term( 'glass', 'product_cat', $cart_item["product_id"] ) ){
            // Added Woocommerce 3+ compatibility
            if( version_compare( WC_VERSION, '3.0', '<' ) )
                $item['data']->price = '0';
            else
                $item['data']->set_price(0);
        }
    }
}

This goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works.

Saturday, May 29, 2021
 
2

Since WooCommerce 2.6+, shipping has been remodeled, and is quite complicated to customize it as you would like, without a plugin… Instead you do, is adding a fee based on your user levels and other conditions if needed.

Here is my code:

function add_shipping_fee( ) {

    // Set here your shipping fee amount
    $fee = 4;

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

    if ( pmpro_hasMembershipLevel('2') == false)
        WC()->cart->add_fee( __('Shipping Fee:', 'your_theme_slug'), $fee, false );
}
add_action( 'woocommerce_cart_calculate_fees','add_shipping_fee' );

This goes naturally in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and working.


References:

  • WooCommerce - Make a set of coupons adding a fixed fee to an order
  • WooCommerce class - WC_Cart - add_fee() method
Saturday, August 28, 2021
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share