Skip to content

Early-bird lifetime licence: $199 once, for the first 200 buyers only. See the offer

Cash on delivery fee (αντικαταβολή)

Adds a fee to the order when the customer pays cash on delivery. The checkout totals update as soon as they pick it.

  • Type PHP
  • Runs Everywhere (runs as WordPress loads)
  • Needs WooCommerce
  • Category Greek e-shop
  • Tested with WordPress 7.1, WooCommerce 11.1

Before you switch it on: Set your fee in $fee (before VAT).

It arrives switched off, even when you install its whole pack with "Switch on after installing".

PHP
<?php

// The fee before VAT (WooCommerce adds VAT to fees): with 24% VAT, 1.61
// becomes 2.00. Change the label if you like.
$fee   = 1.61;
$label = 'Έξοδα αντικαταβολής';

add_action(
	'woocommerce_cart_calculate_fees',
	static function ( $cart ) use ( $fee, $label ) {
		if ( is_admin() && ! wp_doing_ajax() ) {
			return;
		}
		if ( 'cod' === WC()->session?->get( 'chosen_payment_method' ) ) {
			$cart->add_fee( $label, $fee, true );
		}
	}
);

// Classic checkout: recalculate the totals when the customer picks another payment method.
add_action(
	'wp_footer',
	static function () {
		if ( function_exists( 'is_checkout' ) && is_checkout() && ! is_order_received_page() ) {
			echo "<script>jQuery( function ( $ ) { $( 'form.checkout' ).on( 'change', 'input[name=\"payment_method\"]', function () { $( document.body ).trigger( 'update_checkout' ); } ); } );</script>";
		}
	}
);

// Block checkout: tell the cart which payment method is picked, so the totals update.
add_action(
	'woocommerce_blocks_loaded',
	static function () {
		if ( function_exists( 'woocommerce_store_api_register_update_callback' ) ) {
			woocommerce_store_api_register_update_callback(
				array(
					'namespace' => 'snippet-gr-cod-fee',
					'callback'  => static function ( $data ) {
						WC()->session?->set( 'chosen_payment_method', sanitize_key( (string) ( $data['method'] ?? '' ) ) );
					},
				)
			);
		}
	}
);
add_action(
	'wp_enqueue_scripts',
	static function () {
		if ( ! function_exists( 'is_checkout' ) || ! is_checkout() || is_order_received_page() || ! has_block( 'woocommerce/checkout', wc_get_page_id( 'checkout' ) ) ) {
			return;
		}
		wp_register_script( 'snippet-gr-cod-fee', false, array( 'wp-data', 'wc-blocks-checkout' ), null, true );
		wp_enqueue_script( 'snippet-gr-cod-fee' );
		wp_add_inline_script(
			'snippet-gr-cod-fee',
			"( function () { var last = ''; wp.data.subscribe( function () { var store = wp.data.select( 'wc/store/payment' ); var method = store && store.getActivePaymentMethod ? store.getActivePaymentMethod() : ''; if ( method && method !== last ) { last = method; wc.blocksCheckout.extensionCartUpdate( { namespace: 'snippet-gr-cod-fee', data: { method: method } } ); } } ); } )();"
		);
	}
);

Good to know

Works with the classic checkout and the block checkout: the fee shows in the totals as soon as the customer picks cash on delivery.

In these packs

  • Greek e-shop: For shops in Greece: receipt or invoice with a checked VAT number (ΑΦΜ), Greek phone and postcode checks, a cash on delivery fee, and greeklish addresses.

Move your snippets over this afternoon.

Importing changes nothing until you switch over, and your old shortcodes keep working.