1

It would be great if you could help me with this. I am adding a fee basically named as "Additional Add On" on checkout page.

public function change_fee_price($cart){
    $fee_cost = WC()->session->get( 'my_misc_fee_cost' ); // 0 default value.
            WC()->cart->add_fee("Additional Add on", $fee_cost);
    } 

add_action( 'woocommerce_cart_calculate_fees', array($this, 'change_fee_price'), 20, 1 );

I have made this fees amount editable for admin on checkout page.

public function editable_additional_addon_fees($ci, $fee){
    if(WC()->customer->get_role() == "administrator"){
        if(is_cart()){
          if($fee->id == "additional-add-on"){
            $fee_total = WC()->cart->display_prices_including_tax() ? $fee->amount + $fee->tax  :  $fee->amount ;
            $fee_total = WC()->session->get( 'my_misc_fee_cost' ) ? WC()->session->get( 'my_misc_fee_cost' ) : $fee_total;
            $html = "$$fee_total <strong><i>(Edit on checkout page.)</i></strong>";
                return $html;
            }
        }
    
   if(is_checkout()){
     if($fee->id == "additional-add-on"){
        $fee_total = WC()->cart->display_prices_including_tax() ? $fee->amount + $fee->tax  :  $fee->amount ;
        $fee_total = WC()->session->get( 'my_misc_fee_cost' ) ? WC()->session->get( 'my_misc_fee_cost' ) : $fee_total;
        $html = "$<input type='number' id='misc_fee_charge' name='misc_fee_charge' value='".$fee_total."'>";
        return $html;
           }
        }
        return $ci;
    }

    add_filter( 'woocommerce_cart_totals_fee_html', array($this, 'editable_additional_addon_fees'), 10, 2);

Now what if admin wants to rename the "Additional Add On" fee name to "Extra Charges for XYZ" rather than changing the amount.

woocommerce_cart_totals_fee_html helped in editing the html of the fee amount and not the fee name. I want to give user the option to name it's fee dynamically on cart from frontend rather than adding a static name.

Does anyone have any idea how to achieve that?

If you still dont understand the question. Please take a look at the image: enter image description here

To summarize, the question is simple. How to give the option to edit the fee name dynamically from frontend!

  • Your question seems to contain 2 questions. _"...what if admin wants to rename..."_ & _"...I want to give user the option to name..."_. So your question is not so clear, what is the purpose?. When I test your code I get an "500" error.. so please read [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – 7uc1f3r Aug 06 '20 at 17:45
  • @7uc1f3r both the question leads to the same functionality. To provide user the ability to edit the name of the fees. Also, the code is to simply add a custom fees to the cart and then edit it on the checkout page. – Shivanjali Chaurasia Aug 10 '20 at 07:40

0 Answers0