GET a coupon code via URL and apply it in WooCommerce Checkout page [closed]

Update 3: This can be done in a very simple way with the following 2 hooked functions: The first one will catch the coupon code in the Url and will set it in WC_Sessions. The second one will apply the coupon code from session in checkout page. Here is this code: add_action(‘init’, ‘get_custom_coupon_code_to_session’); function get_custom_coupon_code_to_session(){ … Read more

Applied coupons disable Free shipping conditionally in Woocommerce

The below code will enable “Free shipping” for applied coupons only if cart subtotal reaches a minimal amount (discounted including taxes): add_filter( ‘woocommerce_package_rates’, ‘coupons_removes_free_shipping’, 10, 2 ); function coupons_removes_free_shipping( $rates, $package ){ if ( is_admin() && ! defined( ‘DOING_AJAX’ ) ) return $rates; $min_subtotal = 250; // Minimal subtotal allowing free shipping // Get needed … Read more

Allow specific products to be purchased only if a coupon is applied in Woocommerce

For defined products, the following code will not allow checkout if a coupon is not applied, displaying an error message: add_action( ‘woocommerce_check_cart_items’, ‘mandatory_coupon_for_specific_items’ ); function mandatory_coupon_for_specific_items() { $targeted_ids = array(37); // The targeted product ids (in this array) $coupon_code=”summer2″; // The required coupon code $coupon_applied = in_array( strtolower($coupon_code), WC()->cart->get_applied_coupons() ); // Loop through cart items … Read more

Get coupon data from WooCommerce orders

Update 3 Since WooCommerce 3.7, you should now use the WC_Abstract method get_coupon_codes() on the WC_Order instance object to get the used coupons from an order, as get_used_coupons() method is deprecated. So you will replace in the code: foreach( $order->get_used_coupons() as $coupon_code ){ by: foreach( $order->get_coupon_codes() as $coupon_code ){ Then you can get coupon details … Read more

Apply a coupon programmatically in Woocommerce

First, create a discount coupon (via http://docs.woothemes.com/document/create-a-coupon-programatically/): $coupon_code=”UNIQUECODE”; // Code – perhaps generate this from the user ID + the order ID $amount=”10″; // Amount $discount_type=”percent”; // Type: fixed_cart, percent, fixed_product, percent_product $coupon = array( ‘post_title’ => $coupon_code, ‘post_content’ => ”, ‘post_status’ => ‘publish’, ‘post_author’ => 1, ‘post_type’ => ‘shop_coupon’ ); $new_coupon_id = wp_insert_post( $coupon … Read more