Last active
February 16, 2017 21:52
-
-
Save aboglioli/c79e10b648a03062e76e5fdc7d1c356c to your computer and use it in GitHub Desktop.
WooCommerce - Filter cash on delivery (COD) by state (or any attribute)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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