Skip to content

Instantly share code, notes, and snippets.

@aboglioli
Last active February 16, 2017 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aboglioli/c79e10b648a03062e76e5fdc7d1c356c to your computer and use it in GitHub Desktop.
Save aboglioli/c79e10b648a03062e76e5fdc7d1c356c to your computer and use it in GitHub Desktop.
WooCommerce - Filter cash on delivery (COD) by state (or any attribute)
<?php
/**
* Process the checkout.
* This can be used with any attibute from checkout form.
* Cash on delivery field. Checking if it is correct and if customer is from Mendoza (state)
* Note: wc_add_notice is the newest function to send error messages. There are more but they are deprecated.
*/
function k_cod_and_state() {
// Check if payment method is Cash On Delivery (COD)
if(isset( $_POST['payment_method']) && $_POST['payment_method'] === 'cod') {
// If you want to send the product to other address, you can't buy through COD
// You have to pay when you receive the products
if($_POST['billing_state'] === $_POST['shipping_state']) {
// Here we filter if the customer is from the state we have COD available
// If not, we send an error message
if($_POST['billing_state'] !== 'Mendoza') {
// wc_add_notice is the newest function to send error/success/notice messages
wc_add_notice(__('Cash on delivery is only available for Mendoza'), 'error');
}
} else {
wc_add_notice(__('If you want to pay through cash on delivery, your billing and shipping addresses have to be the same.'), 'error');
}
}
}
add_action('woocommerce_checkout_process', 'k_cod_and_state');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment