0

I searched the whole site and found some solutions Unfortunately, the codes are obsolete.

I would like to add a fixed commission to the cart, but I would like this commission to be multiplied by the number of items.

I used this code to apply the flat fee:


add_action('woocommerce_cart_calculate_fees', function() {
    if (is_admin() && !defined('DOING_AJAX')) {
        return;
    }
    
 WC()->cart->add_fee(__('PCU Eco Tassa', 'txtdomain'), 2.30);
});

This increases € 2.30 of the total, but I need this value 2.30 to be multiplied by the number of products.

This shop sells car tires. For example, if I buy a tire, the extra fee must be 2.30, but if I buy 3 tires, regardless of the cost of each product, The value 2.3 must be multiplied by 3, And it must give me 6.9 as a result.

Unfortunately for me It is difficult and I do not know PHP language, I believe and hope that it is simple AND that someone can help me.

I thank everyone in advance

  • The code you have is correct. You can use Fees API to add extra fee to cart/checkout. If you have only tires in cart you can get total count of products with this code: `WC()->cart->get_cart_contents_count()` or you can loop through all the products `foreach ( WC()->cart->get_cart() as $cart_item ) { $product = $cart_item['data']; ... }` – quarky Aug 25 '20 at 14:43
  • @quarky Thank you for your answer. But I don't know where to put the code you suggested. I'm not that good at programming. You can write the whole code for me, I would appreciate it. Very kind of you, thanks – Symone Trimarchi Aug 25 '20 at 17:15

1 Answers1

1

With the help of @quarky, with reasoning and logic, I did it! I realized this:

add_action('woocommerce_cart_calculate_fees', function() {
    if (is_admin() && !defined('DOING_AJAX')) {
        return;
    }
    
$feetot = WC()->cart->get_cart_contents_count();
    
WC()->cart->add_fee(__('PCU Eco Tassa', 'txtdomain'), 2.30 * $feetot);
});