1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| add_action('woocommerce_after_calculate_totals', 'cwpai_get_customer_postcode');
/**
* Get customer postcode after shipping calculation
*/
function cwpai_get_customer_postcode() {
// Get the customer's shipping address object
$customer_shipping_address = WC()->customer->get_shipping_address();
// Check if the customer has a shipping postcode
if (isset($customer_shipping_address['postcode'])) {
// Store the customer's shipping postcode in a global variable
global $cwpai_customer_postcode;
$cwpai_customer_postcode = $customer_shipping_address['postcode'];
// You can now use $cwpai_customer_postcode to calculate the shipping costs or perform any other related operations
// For example:
$shipping_costs = calculate_shipping_costs($cwpai_customer_postcode);
// Update the shipping costs in the cart
WC()->cart->set_shipping_total($shipping_costs);
WC()->cart->calculate_totals();
}
}
/**
* Calculate shipping costs based on customer postcode
*
* @param string $postcode Customer postcode
* @return float Shipping costs
*/
function calculate_shipping_costs($postcode) {
// Implement your logic to calculate the shipping costs based on the customer's postcode
// You can use external APIs, shipping zones, or any other method specific to your setup
// Example: A flat rate of $10 for all postcodes
$shipping_costs = 10.00;
return $shipping_costs;
} |
Partager