I want to add an extra charge when the customer chooses geniki taxydromiki as the shipping method and pays cash on delivery or by card or bank deposit and if the customer belongs to specific zip codes. i use this snippet. what is wrong
add_action(‘woocommerce_cart_calculate_fees’, ‘add_extra_charge_for_specific_conditions’);
function add_extra_charge_for_specific_conditions() {
// Define the shipping method, payment methods, and zip codes for the extra charge
$shipping_method_id = array(‘8’, ‘9’); // Replace with the actual shipping method ID
$payment_methods = array(‘cod’, ‘alphabank_gateway’, ‘bacs’); // Replace with the actual payment method IDs
$target_zip_codes = array(‘49100’, ‘29300’, ‘10003’); // Replace with the actual zip codes
// Get the chosen shipping method
$chosen_shipping_methods = WC()->session->get('chosen_shipping_methods');
$chosen_shipping_method = $chosen_shipping_methods[0];
// Get the chosen payment method
$chosen_payment_method = WC()->session->get('chosen_payment_method');
// Get the customer's zip code
$customer_zip_code = WC()->customer->get_billing_postcode();
// Check if the conditions are met
if ($chosen_shipping_method === $shipping_method_id &&
in_array($chosen_payment_method, $payment_methods) &&
in_array($customer_zip_code, $target_zip_codes)) {
// Define the extra charge
$extra_charge = 5.00; // Set the extra charge amount
// Add the extra charge
WC()->cart->add_fee(__('Extra Charge for Geniki Taxydromiki', 'text-domain'), $extra_charge);
}
}
I want to add an extra fee at checkout page
Sokratis Zervos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.