• Resolved Alexandros Raikos

    (@alexandrosraikos)


    I need to apply an extra fee when the default CoD (Cash on Delivery) payment method is selected as an option. I have applied the following PHP code snippet (with priority 1) using WP Code Snippets as per the official WooCommerce documentation:

    add_action(
    'woocommerce_cart_calculate_fees',
    function () {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
    return;
    }

    $chosen_payment_method_id = WC()->session->get( 'chosen_payment_method');
    $cart = WC()->cart;

    if ( 'cod' === $chosen_payment_method_id ) {
    $fee = 1.50; // Define the fee
    $cart->add_fee(__('Cash on Delivery', 'woocommerce'), $fee, true, '');
    }
    }
    );

    This approach is not working on my store when using either:

    • The old shortcode checkout
    • The new checkout blocks

    The checkout order review does not refresh with the additional fee applied when the CoD payment method is selected. Some details I’ve noticed from my background as a web developer:

    • No XHR request is being made when selecting a payment method to check for the additional fee. I tried doing that using another JS Code Snippet by force-refreshing the cart when a new payment is selected, but I get the same result.
    • Using debugging logs in the aforementioned PHP snippet, it seems like it never detects the cod payment method in the cart at all until the order is sent. In the checkout page it always selects the first default one in the list (bank transfer). Probably because the XHR request is never being made and the action does not have the user selected payment method.

    To make things absolutely certain before sending here:

    • ✅ I am using the Twenty-Twenty Four WordPress theme by default
    • ✅ I tried turning off any other plugins
    • ✅ I tried using a 3rd party plugin and got the exact same issue

    System Status Report

    ### WordPress Environment ###
    
    WordPress address (URL): [Redacted]
    Site address (URL): [Redacted]
    WC Version: 9.7.1
    Legacy REST API Package Version: Το πρόσθετο Legacy REST API δεν είναι εγκατεστημένο σε αυτόν τον ιστότοπο.
    Action Scheduler Version: ✔ 3.9.2
    Log Directory Writable: ✔
    WP Version: 6.7.2
    WP Multisite: –
    WP Memory Limit: 384 MB
    WP Debug Mode: –
    WP Cron: ✔
    Language: el
    External object cache: –
    
    ### Server Environment ###
    
    Server Info: Apache
    PHP Version: 8.2.28
    PHP Post Max Size: 64 MB
    PHP Time Limit: 120
    PHP Max Input Vars: 10000
    cURL Version: 7.61.1
    OpenSSL/1.1.1k
    
    SUHOSIN Installed: –
    MySQL Version: 10.6.21-MariaDB-cll-lve
    Max Upload Size: 64 MB
    Default Timezone is UTC: ✔
    fsockopen/cURL: ✔
    SoapClient: ✔
    DOMDocument: ✔
    GZip: ✔
    Multibyte String: ✔
    Remote Post: ✔
    Remote Get: ✔
    
    ### Database ###
    
    [REDACTED]
    
    ### Post Type Counts ###
    
    attachment: 119
    mc4wp-form: 1
    page: 12
    post: 5
    product: 30
    revision: 775
    shop_coupon: 2
    shop_order_placehold: 97
    spectra-popup: 1
    wp_block: 14
    wp_font_face: 67
    wp_font_family: 22
    wp_global_styles: 1
    wp_navigation: 3
    wp_template: 13
    wp_template_part: 5
    wpforms: 1
    
    ### Security ###
    
    Secure connection (HTTPS): ✔
    Hide errors from visitors: ✔
    
    ### Active Plugins (27) ###
    
    The SEO Framework: από τον/την The SEO Framework Team – 5.1.2
    Cardlink Payment Gateway: από τον/την Cardlink – 1.0.11
    Code Snippets Pro (Premium): από τον/την Code Snippets Pro – 3.6.9
    Code Snippets: από τον/την Code Snippets Pro – 3.6.8
    Complianz | GDPR/CCPA Cookie Consent: από τον/την Really Simple Plugins – 7.3.1
    Facebook for WooCommerce: από τον/την Facebook – 3.4.4
    Google for WooCommerce: από τον/την WooCommerce – 2.9.10
    Site Kit by Google: από τον/την Google – 1.149.1
    Loco Translate: από τον/την Tim Whitlock – 2.7.1
    Mailchimp for WooCommerce: από τον/την Mailchimp – 5.3
    MC4WP: Mailchimp for WordPress: από τον/την ibericode – 4.10.2
    Meta pixel for WordPress: από τον/την Facebook – 4.1.2
    Really Simple Security: από τον/την Really Simple Security – 9.3.2.1
    Stackable - Gutenberg Blocks: από τον/την Gambit Technologies
    Inc – 3.15.2
    
    TikTok: από τον/την TikTok – 1.3.1
    Web Expert WooCommerce Shopflix XML Feed: από τον/την Web Expert – 1.0.15
    WooCommerce Shopflix Connector: από τον/την Web Expert – 1.0.6
    Discount Rules PRO 2.0: από τον/την Flycart – 2.6.8
    Discount Rules Core: από τον/την Flycart – 2.6.8
    WooCommerce.com Update Manager: από τον/την Automattic – 1.0.3
    Google Analytics for WooCommerce: από τον/την WooCommerce – 2.1.13
    WooCommerce Min/Max Quantities: από τον/την Woo – 5.2.3
    WooCommerce: από τον/την Automattic – 9.7.1
    Wordfence Security: από τον/την Wordfence – 8.0.4
    WP Consent API: από τον/την RogierLankhorst – 1.0.8
    WP Mail SMTP: από τον/την WP Mail SMTP – 4.4.0
    WPForms Lite: από τον/την WPForms – 1.9.4.2
    
    ### Inactive Plugins (0) ###
    
    
    ### Settings ###
    
    Legacy API Enabled: –
    Force SSL: –
    Currency: EUR (€)
    Currency Position: right_space
    Thousand Separator: .
    Decimal Separator: ,
    Number of Decimals: 2
    Taxonomies: Product Types: external (external)
    grouped (grouped)
    simple (simple)
    variable (variable)
    
    Taxonomies: Product Visibility: exclude-from-catalog (exclude-from-catalog)
    exclude-from-search (exclude-from-search)
    featured (featured)
    outofstock (outofstock)
    rated-1 (rated-1)
    rated-2 (rated-2)
    rated-3 (rated-3)
    rated-4 (rated-4)
    rated-5 (rated-5)
    
    Connected to WooCommerce.com: ✔
    Enforce Approved Product Download Directories: ✔
    HPOS feature enabled: ✔
    Order datastore: Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore
    HPOS data sync enabled: –
    
    ### Logging ###
    
    Enabled: ✔
    Handler: Automattic\WooCommerce\Internal\Admin\Logging\LogHandlerFileV2
    Retention period: 30 ημέρες
    Level threshold: –
    Log directory size: 72 MB
    
    ### WC Pages ###
    
    Βάση καταστήματος: #9 - /shop/
    Καλάθι: #10 - /cart/ -  Contains the woocommerce/cart block
    Ταμείο: #11 - /checkout/ -  Contains the woocommerce/checkout block
    Ο λογαριασμός μου: #12 - /my-account/ -  This page's content is overridden by custom template content
    Όροι και Προϋποθέσεις: #530 - /%cf%8c%cf%81%ce%bf%ce%b9-%cf%87%cf%81%ce%ae%cf%83%ce%b7%cf%82/
    
    ### Theme ###
    
    Name: Twenty Twenty-Four
    Version: 1.3
    Author URL: https://wordpress.org
    Child Theme: ❌ – Αν κάνετε μετατροπή του WooCommerce πάνω σε ένα γονικό θέμα που δεν δημιουργήσατε εσείς προσωπικά
    τότε σας προτείνουμε να χρησιμοποιήσετε ένα θέμα απόγονο. Δείτε: Πως να δημιουργήσετε ένα θέμα απόγονο
    
    Theme type: Θέμα εμφάνισης με μπλοκ
    WooCommerce Support: ❌ Δεν υποστηρίζει
    
    ### Templates ###
    
    Overrides: /home/ze569226/moominbabyshop/wp-content/plugins/woocommerce/templates/block-notices/error.php
    /home/ze569226/moominbabyshop/wp-content/plugins/woocommerce/templates/block-notices/notice.php
    /home/ze569226/moominbabyshop/wp-content/plugins/woocommerce/templates/block-notices/success.php
    
    
    ### Admin ###
    
    Enabled Features: activity-panels
    analytics
    product-block-editor
    coupons
    core-profiler
    customize-store
    customer-effort-score-tracks
    import-products-task
    experimental-fashion-sample-products
    shipping-smart-defaults
    shipping-setting-tour
    homescreen
    marketing
    mobile-app-banner
    onboarding
    onboarding-tasks
    pattern-toolkit-full-composability
    product-custom-fields
    remote-inbox-notifications
    remote-free-extensions
    payment-gateway-suggestions
    printful
    shipping-label-banner
    subscriptions
    store-alerts
    transient-notices
    woo-mobile-welcome
    wc-pay-promotion
    wc-pay-welcome-page
    launch-your-store
    add-to-cart-with-options-stepper-layout
    
    Disabled Features: product-data-views
    experimental-blocks
    coming-soon-newsletter-template
    minified-js
    product-pre-publish-modal
    settings
    async-product-editor-category-field
    product-editor-template-system
    blueprint
    reactify-classic-payments-settings
    use-wp-horizon
    blockified-add-to-cart
    
    Daily Cron: ✔ Next scheduled: 2025-04-02 08:28:46 +00:00
    Options: ✔
    Notes: 89
    Onboarding: completed
    
    ### Action Scheduler ###
    
    Canceled: 2
    Oldest: 2025-03-18 13:59:56 +0000
    Newest: 2025-03-26 17:54:34 +0000
    
    Complete: 1.953
    Oldest: 2025-03-04 10:09:51 +0000
    Newest: 2025-04-02 05:30:31 +0000
    
    Failed: 25
    Oldest: 2024-08-17 11:02:57 +0000
    Newest: 2025-04-02 05:28:19 +0000
    
    Pending: 11
    Oldest: 2025-04-02 06:06:48 +0000
    Newest: 2025-04-07 15:03:25 +0000
    
    
    ### Status report information ###
    
    Generated at: 2025-04-02 05:32:54 +00:00
Viewing 6 replies - 1 through 6 (of 6 total)
  • Saif

    (@babylon1999)

    This caught my attention because I’ve also used this snippet on several client websites.

    While this is expected with block-based checkout, I can see the same behaviour on my test site using legacy shortcodes—it appears that changing the payment method no longer triggers the update_checkout event.

    You can re-add this functionality with the following snippet, which will trigger the event manually when the payment method is changed, but I’m not sure if it will work with all themes.

    add_action('wp_footer', 'recalculate_fee_when_payment_method_change');
    function recalculate_fee_when_payment_method_change() {
    if (!is_checkout()) return;

    wc_enqueue_js("
    jQuery(function($) {
    $(document.body).on('change', 'input[name=\"payment_method\"]', function() {
    $(document.body).trigger('update_checkout');
    });
    });
    ");
    }

    Since you’re the first to report this @alexandrosraikos , I suggest opening an issue about it on GitHub. : )

    Thread Starter Alexandros Raikos

    (@alexandrosraikos)

    @babylon1999 I’m afraid this fix is outdated, at least for the new Gutenberg blocks. It seems on GitHub there were many issues. I managed to open #56999 with the same information to keep track just in case it really is a technical issue and I’m not missing anything.

    Saif

    (@babylon1999)

    I’m afraid this fix is outdated, at least for the new Gutenberg blocks.

    Yes, it won’t work for Gutenberg blocks. The snippet is for legacy shorcodes.

    Plugin Support shahzeen(woo-hc)

    (@shahzeenfarooq)

    Hi there!

    Please note that in this forum, we only provide support for WooCommerce core features. Support for customization is outside our scope.

    I can recommend WooExperts and Codeable.io as options for getting professional help. Alternatively, you can also ask your development questions in the  WooCommerce Community Slack as custom code falls outside our usual scope of support.

    Thread Starter Alexandros Raikos

    (@alexandrosraikos)

    @shahzeenfarooq this isn’t a matter of customization anymore. There is either a critical bug in WooCommerce core messing with any plugin that adds fees in the checkout page, or the latest documentation provided is just false. I would be happy to admit any mistakes in my approach.

    Hi @alexandrosraikos,

    I understand your concern and how frustrating this situation can be. However, this forum is intended for addressing core WooCommerce features and issues. Since your request involves customization, it falls outside the scope of our support.

    To achieve your desired result, you can use plugins like Payment Gateway Based Fees for WooCommerce or Extra Fees for WooCommerce. If these plugins don’t work, you can create a support ticket through this link if you purchased the plugin via WooCommerce.com.

    If you’d prefer to use your own custom script, I recommend consulting with WooCommerce experts or seeking help in the WooCommerce Community Slack.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Custom additional fee per instructions not applied’ is closed to new replies.