Skip to content

Instantly share code, notes, and snippets.

@davebonds
Last active July 27, 2017 16:51
Show Gist options
  • Save davebonds/9ec5ec731067a2f26318594364c485a3 to your computer and use it in GitHub Desktop.
Save davebonds/9ec5ec731067a2f26318594364c485a3 to your computer and use it in GitHub Desktop.
Add coupon to order after order complete
// Add discount total to order meta.
update_post_meta( $order_id, '_cart_discount', $coupon_discount );
// Subtract discount from order total meta.
update_post_meta( $order_id, '_order_total', get_post_meta( $order_id, '_order_total') - $coupon_discount );
// Update coupon meta with user ID that used the coupon.
update_post_meta( $coupon_post_id, '_used_by', $user_id);
// Update coupon meta to increase usage count by 1.
update_post_meta( $coupon_post_id, 'usage_count', get_post_meta( $coupon_post_id, 'usage_count' ) + 1 );
// Add coupon as an order item to the order.
woocommerce_add_order_item( $order_id, array(
'order_item_name' => $coupon_code_name,
'order_item_type' => 'coupon'
) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment