Skip to content

Instantly share code, notes, and snippets.

@corypina
Last active May 14, 2019 21:28
Show Gist options
  • Save corypina/16b9e4cc7634540e2a1432e2cfde7089 to your computer and use it in GitHub Desktop.
Save corypina/16b9e4cc7634540e2a1432e2cfde7089 to your computer and use it in GitHub Desktop.
Programmatically add coupon code to WooCommerce checkout
<?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