• I dont know is this a bug of WooCommerce or I am doing something wrong.this is my code to add a custom settings tab for and adding fields.but whenever I add a fields with $settings array it outputting two same field.its happening for all type of fields including text,title etc.

    <?php
    /**
    * Main Class for WooFees
    * @package WooFees
    */
    namespace src\classes;

    class WooFees{
    public function __construct(){
    add_action("woocommerce_cart_calculate_fees",[$this, 'woofees']);
    add_action("woocommerce_settings_tabs_array", [$this, 'add_vat_rate_tab'], 50);
    add_action("woocommerce_settings_tabs_vat_rate_tab", [$this, "settings_tab"]);
    }

    public function woofees(){
    global $woocommerce;
    $vat = $woocommerce->cart->cart_contents_total * 0.05;
    $woocommerce->cart->add_fee(__("VAT (5%) ", 'woofees'), $vat);
    }

    public function add_vat_rate_tab($tabs){
    $tabs['vat_rate_tab'] = __("Extra Fee", 'woofees');
    return $tabs;
    }

    public function settings_tab(){
    woocommerce_admin_fields($this->vat_rate_tab_fields());
    }

    public function vat_rate_tab_fields(){
    $settings = [
    'sections_title' => [
    'id' => 'vat_rate_tab_title',
    'name' => __("Configure Addition VAT rate for Customers", 'woofees'),
    'desc' => __("Configure Addition VAT or TAX for Your Products That Your Castomers Will See in The Checkout Page and They Have to Pay The Extra"),
    'type' => 'title'
    ],
    ];

    return apply_filters("vat_rate_tab_fields", $settings);
    }


    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi @kmfoysal06,

    Thank you for reaching out.

    From the code snippet you’ve provided, it seems like you’re trying to add a custom settings tab and fields in WooCommerce. If you’re seeing duplicate fields, it’s likely due to how you’re hooking into the WooCommerce settings.

    However, helping out with custom coding of this nature is outside the scope of support, although I would recommend the following:

    1. Running the exact question you’re asking, along with the code provided, through an AI platform like ChatGPT for recommendations/changes to your code;
    2. Checking whether there are existing plugins in the WordPress plugin repository that might be doing that already.
    3. Joining our WooCommerce Slack community (it does have a developer channel where you can ask coding questions): https://woo.com/community-slack/

    Hope it helps!

    Thread Starter Kazi Mohammad Foysal

    (@kmfoysal06)

    I got the solution of this problem.I was instantiating the class in main plugin file and using plugin_loaded hook for instantiating the class with new keyword.i think for this somehow the class instantiated twice and that give me this result.so now I am using singleton and its working properly.Code and Everything for Someone in Future ??

    Hi @kmfoysal06,

    I’m glad to hear that you were able to resolve the issue you were facing. Your solution of using a singleton instead of instantiating the class in the main plugin file is indeed a good practice to prevent the class from being instantiated twice.

    Thank you for sharing your solution with us and the community. It’s users like you who help make WooCommerce better for everyone.

    Cheers!

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.