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

Display the discounted percentage near sale price in Single product pages for WC 3.0+

Updated – 2019 (avoid rounding price issue) – 2017 (avoid NAN% percentage value) woocommerce_sale_price_html hook has been replaced by a different hook in WooCommerce 3.0+, that has now 3 arguments (but not the $product argument anymore). add_filter( ‘woocommerce_format_sale_price’, ‘woocommerce_custom_sales_price’, 10, 3 ); function woocommerce_custom_sales_price( $price, $regular_price, $sale_price ) { // Getting the clean numeric prices … Read more