1

I've tried searching for an answer to this but most of the code is old or not exactly what I'm looking for. Here is the scenario - when people order digital image rights from our store, they must pay for it's usage as well as a copy fee. The usage fee is variable based on several factors and non-taxable - this would be the product added to the cart. The copy fee is a fixed, taxable, cost per image. I want this to be automatically added to my cart whenever an product from the 'images' category is added.

I found code on WPFactory that adds a fee based on category that works well.

add_action( 'woocommerce_cart_calculate_fees', 'wpf_wc_add_cart_fees_by_product_category' );
if ( ! function_exists( 'wpf_wc_add_cart_fees_by_product_category' ) ) {
    /**
     * wpf_wc_add_cart_fees_by_product_category.
     */
    function wpf_wc_add_cart_fees_by_product_category( $cart ) {
        // You need to enter your fees here, in `category slug` => `fee amount` format
        $fees = array(
            'images'    => 25,
        );
        foreach ( $cart->get_cart() as $cart_item_key => $values ) {
            $product_categories = get_the_terms( $values['product_id'], 'product_cat' );
            if ( $product_categories && ! is_wp_error( $product_categories ) ) {
                $product_categories = wp_list_pluck( $product_categories, 'slug' );
                foreach ( $product_categories as $product_category ) {
                    if ( ! empty( $fees[ $product_category ] ) ) {
                        $name      = 'Digital Image Copy Fee'
                        $amount    = $fees;
                        $taxable   = true;
                        $tax_class = '';
                        $cart->add_fee( $name, $amount, $taxable, $tax_class );
                    }
                }
            }
        }
    }
}

The problem is, it only adds the fee once and I need it to multiply the fee based on the total quantity of items from the category in the cart. I've attempted to calculate $qty and multiply $fee by $qty but my php is incredibly rusty and I have not been successful. Any help would be much appreciated.

0

1 Answer 1

0

Here is the solution:

add_action( 'woocommerce_cart_calculate_fees', 'wpf_wc_add_cart_fees_by_product_category', 10, 1 );
function wpf_wc_add_cart_fees_by_product_category( $cart ) {

    $product_cat_id = 119; // set "image" cat id
    $total_qty = 0; // initialize qty products
    $fee = 25; // set fee amount

    foreach ( $cart->get_cart() as $cart_item ) {
        $product_id = $cart_item['product_id'];
        // get all category ids by product id
        $categories = wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'ids' ) );
        // if product has "image" category
        if ( in_array( $product_cat_id, $categories ) )  {
            // get quantity
            $total_qty += $cart_item['quantity'];
        }
    }
    
    if ( $total_qty > 0 ) {
        $tax = true; // set "false" for no tax
        $cart->add_fee( __( "Digital Image Copy Fee", "woocommerce" ), $fee * $total_qty, $tax ); // add custom fee
    }

}

The code goes into your theme's functions.php file and it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.