1

I have a client who needs a complicated pricing structure for a Woocommerce subscription.

I need to calculate the number of products in the cart (besides the subscription product).

For each of those additional products, I need to add $50 to the initial sign-up fee.

But I also want to add a fee to the monthly subscription price, this would depend on which subscription product is in the cart.

My code is below. The initial sign-up fee changes are working, but the monthly subscription fee is not changing. Any ideas why?

add_filter( 'woocommerce_cart_calculate_fees', 'add_administration_fees', 10, 1 );

function add_administration_fees() {
    if(!WC()->cart->is_empty()) {
        
        $item_count = 0;
        $subs = 0;
    
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            
            $product = $cart_item['data'];
            
            if(!$product->is_type('subscription')) {
                $item_count+= $cart_item['quantity'];
            }
            
            else {
                $subs += $cart_item['quantity'];
            }
        }
        
    }
    
    if ($subs == 1) {
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            $product = $cart_item['data'];
            if($product->is_type('subscription')) {
                switch($cart_item['product_id']) {
                    case 400:               
                        $cart_item['data']->update_meta_data('_subscription_sign_up_fee', 99+($item_count * 50));
                        $cart_item['data']->update_meta_data('_subscription_price', 35+($item_count * 7));
                        break;
                    case 401:               
                        $cart_item['data']->update_meta_data('_subscription_sign_up_fee', 99+($item_count * 50));
                        $cart_item['data']->update_meta_data('_subscription_price', 30+($item_count * 5));
                        break;
                    case 402:               
                        $cart_item['data']->update_meta_data('_subscription_sign_up_fee', 299+($item_count * 50));
                        $cart_item['data']->update_meta_data('_subscription_price', 40+($item_count * 7));
                        break;
                    case 403:               
                        $cart_item['data']->update_meta_data('_subscription_sign_up_fee', 299+($item_count * 50));
                        $cart_item['data']->update_meta_data('_subscription_price', 35+($item_count * 5));
                        break;
                    case 404:               
                        $cart_item['data']->update_meta_data('_subscription_sign_up_fee', 499+($item_count * 50));
                        $cart_item['data']->update_meta_data('_subscription_price', 45+($item_count * 7));
                        break;
                    case 405:               
                        $cart_item['data']->update_meta_data('_subscription_sign_up_fee', 499+($item_count * 50));
                        $cart_item['data']->update_meta_data('_subscription_price', 40+($item_count * 5));
                        break;
                }
            }
    }
    
    
}
}
mujuonly
  • 11,370
  • 5
  • 45
  • 75
Dinoboy
  • 49
  • 6

1 Answers1

0
// All fees are recurring
add_filter( 'woocommerce_subscriptions_is_recurring_fee', '__return_true' );

add_filter( 'woocommerce_cart_calculate_fees', 'add_fees', 10 );

function add_fees() {
    WC()->cart->add_fee( 'Fee', '10' );
}

You may need to add it as a fee instead. Please check the doc for adding recurring fees to the subscription.

mujuonly
  • 11,370
  • 5
  • 45
  • 75
  • Thanks I have looked at that page. I don't want all fees to recur, I need different fees for the initial fee and the recurring fees. I did try the other function on that page but couldn't get that one to work either: `add_filter( 'woocommerce_cart_calculate_fees', 'add_recurring_postage_fees', 10, 1 ); function add_recurring_postage_fees( $cart ) { if ( ! empty( $cart->recurring_cart_key ) ) { $cart->add_fee( 'Postage', 5 ); } }` – Dinoboy Jul 07 '22 at 09:54
  • Besides, this adds another fee, it doesn't edit the subscription fees themselves. So not quite what I'm looking for. – Dinoboy Jul 07 '22 at 09:57