Last active
May 14, 2019 21:28
-
-
Save corypina/16b9e4cc7634540e2a1432e2cfde7089 to your computer and use it in GitHub Desktop.
Programmatically add coupon code to WooCommerce checkout
This file contains hidden or 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 | |
// Automatically add a WooCommerce coupon code | |
// Currently added when headed to checkout, but can be connected to different hook. | |
add_action('woocommerce_before_checkout_form', function() { | |
global $woocommerce; | |
$coupon_code = 'COUPON_CODE'; // Replace with your code | |
// Add additional logic if necessary | |
if ( ! $woocommerce->cart->add_discount( sanitize_text_field( $coupon_code )) ) : | |
$woocommerce->show_messages(); | |
endif; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment