添加额外费用 woocommerce 结账

问题描述 投票:0回答:1

当客户选择 geniki Taxydromiki 作为运输方式并支付货到付款或通过银行卡或银行存款付款并且客户属于特定邮政编码时,我想添加额外费用。我用这个片段。怎么了

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);
    }
}

我想在结账页面添加额外费用

php woocommerce code-snippets
1个回答
0
投票

尝试替换以下行:

$chosen_shipping_method = $chosen_shipping_methods[0];

与:

$chosen_shipping_method = (array) explode(':', $chosen_shipping_methods[0]);
$chosen_shipping_method = (string) end($chosen_shipping_method);

现在应该可以工作了。

© www.soinside.com 2019 - 2024. All rights reserved.