Skip to content

Instantly share code, notes, and snippets.

@haet
Created March 21, 2019 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haet/da309c96a0a83439c0bf5f95872db07d to your computer and use it in GitHub Desktop.
Save haet/da309c96a0a83439c0bf5f95872db07d to your computer and use it in GitHub Desktop.
Add coupons codes to your WooCommerce emails using WP HTML Mail
add_filter( 'haet_mail_placeholder_menu', 'add_mailbuilder_coupons_placeholder' );
add_filter( 'haet_mail_order_placeholders', 'populate_mailbuilder_coupons_placeholder', 10, 3 );
function add_mailbuilder_coupons_placeholder( $placeholder_menu ){
if( is_array( $placeholder_menu ) ){
$placeholder_menu[] = array(
'text' => 'Coupons',
'tooltip' => '[COUPONS]',
);
}
return $placeholder_menu;
}
function populate_mailbuilder_coupons_placeholder( $order, $wc_order, $settings ){
$coupons_text = '';
if( $wc_order ){
$coupons = $wc_order->get_used_coupons();
if( is_array( $coupons ) && count( $coupons ) ){
$coupons_text .= 'You have used the following coupons: <br>';
foreach( $coupons as $coupon_name ){
// Retrieving the coupon ID
$coupon_post_obj = get_page_by_title($coupon_name, OBJECT, 'shop_coupon');
$coupon_id = $coupon_post_obj->ID;
// Get an instance of WC_Coupon object in an array(necesary to use WC_Coupon methods)
$coupons_obj = new WC_Coupon($coupon_id);
$discount_type = $coupons_obj->get_discount_type();
$amount_str = '';
if( $discount_type == 'fixed_cart' )
$amount_str = '- €' . $coupons_obj->get_amount();
elseif( $discount_type == 'percent' )
$amount_str = '- ' . $coupons_obj->get_amount() . '%';
//elseif( $discount_type == ...
$coupons_text .= '<strong>' . $coupons_obj->get_code() . '</strong>: ' . $amount_str . ', ';
}
}
}
$order['coupons'] = $coupons_text;
return $order;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment