Cash on Delivery Fee (Block Checkout Compatible)

By default, WooCommerce uses the classic editor for products. This snippet forces WordPress to use the block editor for the product post type, allowing you to build product pages with modern Gutenberg blocks, patterns, and Full Site Editing–compatible layouts.




/**
 * 1. DÍJ SZÁMÍTÁSA (PHP) - Bruttó 500 Ft
 */
add_action( 'woocommerce_cart_calculate_fees', 'borsuttogo_final_fee_calculation', 20 );
function borsuttogo_final_fee_calculation( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    $chosen_payment = WC()->session->get( 'chosen_payment_method' );
    $shipping_methods = WC()->session->get( 'chosen_shipping_methods', [] );

    $fee_eligible_payments = [ 'cod', 'cheque' ];

    if ( in_array( $chosen_payment, $fee_eligible_payments ) ) {
        $apply_fee = false;
        foreach ( (array) $shipping_methods as $method ) {
            if ( strpos( $method, 'local_pickup' ) === false ) {
                $apply_fee = true;
                break;
            }
        }

        if ( $apply_fee ) {
            // 393.7 Ft + 27% áfa = ~500 Ft bruttó
            $cart->add_fee( 'Utánvét díja', 393.7, true );
        }
    }
}

/**
 * 2. FIX: DÍJ MENTÉSE A RENDELÉSHEZ (Hogy benne legyen az emailben)
 * A Block Checkout API-ja hívja meg ezt a pontot a rendelés leadásakor.
 */
add_action( 'woocommerce_store_api_checkout_update_order_from_request', function( $order, $request ) {
    $payment_method = isset( $request['payment_method'] ) ? $request['payment_method'] : '';
    
    // Frissítjük a session-t a biztonság kedvéért
    if ( ! empty( $payment_method ) ) {
        WC()->session->set( 'chosen_payment_method', $payment_method );
    }

    // Újraszámoljuk a kosarat a beküldött adatokkal
    WC()->cart->calculate_fees();

    // Manuálisan átmásoljuk a díjakat a kosárból a tényleges rendelésbe (order)
    // Ez biztosítja, hogy az adatbázisba bekerüljön és az email kiküldhető legyen
    foreach ( WC()->cart->get_fees() as $fee ) {
        $order->add_item( $fee );
    }
    
    $order->calculate_totals();
    $order->save();
}, 10, 2 );

/**
 * 3. GYORSÍTOTT FRISSÍTÉS (JavaScript)
 */
add_action( 'wp_enqueue_scripts', function() {
    if ( has_block( 'woocommerce/checkout' ) || is_checkout() ) {
        $js_code = "
            (function() {
                const register = () => {
                    if ( window.wc && window.wc.checkoutFilters ) {
                        window.wc.checkoutFilters.registerCheckoutFilters( 'borsuttogo-final-sync', {
                            paymentMethodId: ( value ) => value,
                            shippingMethodIds: ( value ) => value
                        });
                    } else {
                        setTimeout(register, 50);
                    }
                };
                register();
            })();
        ";
        wp_register_script( 'block-fee-email-fix', '', [], '1.6', true );
        wp_enqueue_script( 'block-fee-email-fix' );
        wp_add_inline_script( 'block-fee-email-fix', $js_code );
    }
});

What it does

  • Adds +500 Ft COD fee (gross) at checkout
  • Applies only if:
    • payment method is cod or cheque
    • shipping method is not local_pickup
  • Works with Block Checkout (Store API)
  • Ensures the fee is stored on the order, so it shows in:
    • confirmation emails
    • WooCommerce order view
    • PDFs / integrations that read order totals

Why it’s needed

Block Checkout can calculate fees in the cart UI, but without syncing, some fees may not persist into the final order. This snippet forces the fee calculation and copies the fees from the cart into the order during Store API checkout.

There are no reviews yet.

Be the first to review “Cash on Delivery Fee (Block Checkout Compatible)”

Your email address will not be published. Required fields are marked *