Ecommerce Shopify WordPress Discussion

Create a custom AddFee woocommerce plugin (Add a percentage tax fee for selected payment gateways and allow excluding products from tax calculation.)

Following Custom checkbox in WooCommerce admin edit product for a payment fee calculation answer to my previous question. Now, I am creating a plugin based on that code, with some additional features. In any case, I need help with this! We want these things to be done not by function but by a plugin in a simpler and more modular way. Let me give a simple and brief explanation: We have a function that does several important things. Select a specific payment gateway id's with the array. If that payment gateway is selected, 9% will be added to the total amount of products for purchase as value-added tax. There is a check box by which some products can be excluded. If we want to activate it for each product separately (can do this on each product edit page) and for that product, no additional cost or tax will be added. This tax is different from WooCommerce's default tax. This tax or better name its additional fee is what is built with this function. What do we need now? A plugin that has options in its settings. You can enter the percentage manual (custom) of additional cost in the payment as desired in a box in plugin settings. By default, 9% was considered in the function. But with this setting, each person enters that number in the corresponding field as much as he likes. The payment gateways (are active) are shown and can be selected by the checkbox of that gateway, or those desired gateways that we want to create an additional fee if selected. a checkbox in each product edit page to exclude from add fee (Of course, this possibility is already written in the function.) To do this, I've made the following changes to the code and added some things that I'm not sure work cleanly and without problems, and if my method is correct... or not... Here is my code: /* Plugin Name: Custom Tax Fee Description: Adds a percentage tax fee for selected payment gateways and allows excluding products from tax calculation. Version: 1.0 Author: LoicTheAztec, PaulVek */ // Exit if accessed directly. if (!defined('ABSPATH')) { exit; } // Plugin functionality add_action('woocommerce_cart_calculate_fees', 'add_checkout_fee_for_gateway'); function add_checkout_fee_for_gateway($cart) { if (is_admin() && !defined('DOING_AJAX')) { return; } $selected_payment_methods = get_option('selected_payment_methods', array()); // Get selected payment gateways $additional_cost_enabled = get_option('additional_cost_enabled', false); // Check if custom percentage is enabled $additional_cost_percentage = floatval(get_option('additional_cost_percentage', 0.09)); // Get additional cost percentage if ($additional_cost_enabled && is_checkout() && !is_wc_endpoint_url() && in_array(WC()->session->get('chosen_payment_method'), $selected_payment_methods)) { $custom_subtotal = 0; foreach ($cart->get_cart() as $item) { $product = wc_get_product($item['product_id']); if ($product->get_meta('_tax_fee_excluded') !== 'yes') { $custom_subtotal += (float)$item['line_subtotal']; } } if ($custom_subtotal > 0) { $cart->add_fee(__('Additional Payment Fee', 'custom-tax-fee'), ($custom_subtotal * $additional_cost_percentage), true, ''); } } } add_action('woocommerce_checkout_init', 'update_checkout_on_payment_method_change'); function update_checkout_on_payment_method_change() { wc_enqueue_js("$('form.checkout').on( 'change', 'input[name=payment_method]', function(){ $(document.body).trigger('update_checkout'); });"); } add_action('woocommerce_product_options_general_product_data', 'add_admin_product_custom_field'); function add_admin_product_custom_field() { woocommerce_wp_checkbox(array( 'id' => '_tax_fee_excluded', 'wrapper_class' => 'show_if_simple', 'label' => __('Exclude from Tax Calculation'), 'description' => __('Check this box to exclude this product from tax calculation.') )); } add_action('woocommerce_admin_process_product_object', 'save_admin_product_custom_field_value'); function save_admin_product_custom_field_value($product) { $product->update_meta_data('_tax_fee_excluded', isset($_POST['_tax_fee_excluded']) ? 'yes' : 'no'); } // Add settings page for plugin add_action('admin_menu', 'custom_tax_fee_settings_page'); function custom_tax_fee_settings_page() { add_options_page('Custom Tax Fee Settings', 'Custom Tax Fee', 'manage_options', 'custom-tax-fee-settings', 'custom_tax_fee_settings_callback'); } // Settings page callback function function custom_tax_fee_settings_callback() { ?> <div class="wrap"> <h1>Custom Tax Fee Settings</h1> <form method="post" action="options.php"> <?php settings_fields('custom_tax_fee_settings_group'); ?> <?php do_settings_sections('custom-tax-fee-settings'); ?> <?php submit_button(); ?> </form> </div> <?php } // Register and define settings add_action('admin_init', 'custom_tax_fee_settings'); function custom_tax_fee_settings() { register_setting('custom_tax_fee_settings_group', 'selected_payment_methods'); register_setting('custom_tax_fee_settings_group', 'additional_cost_enabled'); register_setting('custom_tax_fee_settings_group', 'additional_cost_percentage'); add_settings_section('selected_payment_methods_section', 'Select Payment Gateways', 'selected_payment_methods_section_callback', 'custom-tax-fee-settings'); add_settings_field('selected_payment_methods_field', 'Payment Gateways', 'selected_payment_methods_field_callback', 'custom-tax-fee-settings', 'selected_payment_methods_section'); add_settings_section('additional_cost_section', 'Additional Cost Settings', 'additional_cost_section_callback', 'custom-tax-fee-settings'); add_settings_field('additional_cost_enabled_field', 'Enable Custom Percentage', 'additional_cost_enabled_field_callback', 'custom-tax-fee-settings', 'additional_cost_section'); add_settings_field('additional_cost_field', 'Additional Cost Percentage', 'additional_cost_field_callback', 'custom-tax-fee-settings', 'additional_cost_section'); } // Callback functions for settings sections and fields function selected_payment_methods_section_callback() { echo '<p>Select the payment gateways to apply the additional fee:</p>'; } function selected_payment_methods_field_callback() { $active_gateways = WC()->payment_gateways->get_available_payment_gateways(); $selected_payment_methods = get_option('selected_payment_methods', array()); foreach ($active_gateways as $gateway) { printf( '<label><input type="checkbox" name="selected_payment_methods[]" value="%s" %s> %s</label><br>', esc_attr($gateway->id), in_array($gateway->id, $selected_payment_methods) ? 'checked' : '', esc_html($gateway->get_title()) ); } } function additional_cost_section_callback() { echo '<p>Set the additional cost percentage:</p>'; } function additional_cost_enabled_field_callback() { $additional_cost_enabled = get_option('additional_cost_enabled', false); echo '<label><input type="checkbox" name="additional_cost_enabled" value="1" ' . checked(1, $additional_cost_enabled, false) . '> Enable Custom Percentage</label>'; } function additional_cost_field_callback() { $additional_cost_percentage = get_option('additional_cost_percentage', 0.09); echo '<input type="number" step="0.01" min="0" name="additional_cost_percentage" value="' . esc_attr($additional_cost_percentage) . '">'; }
TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.