• Resolved mcarner5

    (@mcarner5)


    I need to give flat fees to specific products so that they show up on the line item totals. It appears that I can do this through use of a hook (woocommerce_before_calculate_totals or woocommerce_after_calculate_totals) and by altering the cart items in the WC_Cart object. However after I do this, any changes I make to line_subtotal and line_total are wiped away by the time everything is spit out to the cart.

    Is there a way to do this? Here is the code I’m using:

    
     add_action( 'woocommerce_before_calculate_totals', 'calculate_product_totals_with_hidden_tm_epo_fees' );
     function calculate_product_totals_with_hidden_tm_epo_fees( $cart_object ){
     
     
        $cart_contents = $cart_object->get_cart_contents();
     
        foreach ( $cart_contents as $cart_content ) {
     
           if (sizeof($cart_content['tmcartfee']) > 0){
     
              $cart_content['tmcartfee_hidden'] = $cart_content['tmcartfee'];
              $cart_content['tmcartfee'] = array();
              $cart_content['tmdata']['tmcartfee_data'] = array();
     
              //Fees are hidden and overridden. Now you calculate everything in:
              // 'woocommerce_before_calculate_totals'
     
              foreach ( $cart_content['tmcartfee_hidden'] as $fee ) {
                 $cart_content['line_subtotal'] += $fee['price'];
                 $cart_content['line_total'] += $fee['price'];
              }
           }
        }
     
        $cart_object->set_cart_contents($cart_contents);
     
     
     }
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • laceyrod

    (@laceyrod)

    Automattic Happiness Engineer

    Hi there!

    Thanks for reaching out.

    Since this would require more complex development, and I’m not a developer myself, I’ll go ahead and leave this thread open for a while in case anyone else wants to chime in or take a look at the code. In the meantime, I can also recommend the following resources for more development-oriented questions:

    – WooCommerce Slack Community: https://woocommerce.com/community-slack/
    – Advanced WooCommerce group on Facebook: https://www.facebook.com/groups/advanced.woocommerce/

    Thanks!

    AJ a11n

    (@amandasjackson)

    We haven’t heard back from you in a while, and no one else had jumped in to offer suggestions, so I’m going to mark this as resolved – if you have any further questions, you can start a new thread.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Add product specific fee, not cart-wide fee’ is closed to new replies.