Search code examples
wordpresswoocommerceorderscoupon

Auto-generated Coupon does not show on Order Email


I've made two functions whereof each is hooked to a action. They are both working fine except for one thing; I need it to work together.

When the order is marked as complete, a coupon will be generated using wp_rand. I've then added custom text to the order email and used $coupon_code within the span tag.

How do I combine these two so that the email shows the generated discount code? Any ideas?

Here's the code:

add_action( 'woocommerce_email_before_order_table', 'add_coupon_code_to_order_email', 20, 4 );
function add_coupon_code_to_order_email( $order, $sent_to_admin, $plain_text, $email ) {
if ( $email->id == 'customer_completed_order' ) {
echo '<h2 class="discount-coupon-title">THIS IS YOUR NEXT DISCOUNT</h2><p class="discount-coupon-text">When you\'re ready, welcome back to a 10% discount!<br/> This is your discount code: <code><strong>'.$coupon_code.'</strong></code></p>';
}
?>
<style>
.discount-coupon-title { color: red; }
.discount-coupon-text { color: black; }
</style>
<?php
}


add_action( 'woocommerce_order_status_completed', 'create_coupon_on_order_completed', 10, 1 );
function create_coupon_on_order_completed( $order_id ) {
$order = wc_get_order( $order_id );
$coupon_code = wp_rand();
$amount = '10';
$discount_type = 'fixed_cart'; // 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 );
update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
update_post_meta( $new_coupon_id, 'individual_use', 'no' );
update_post_meta( $new_coupon_id, 'product_ids', $product_ids );
update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
update_post_meta( $new_coupon_id, 'usage_limit', '1' );
update_post_meta( $new_coupon_id, 'expiry_date', '' );
update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
unset($product_ids);
}

Solution

  • add_action('woocommerce_email_before_order_table', 'add_coupon_code_to_order_email', 20, 4);
    
    function add_coupon_code_to_order_email($order, $sent_to_admin, $plain_text, $email) {
        if ($email->id == 'customer_completed_order') {
    
            $product_ids = '';
            foreach ($order->get_items() as $item) {
                $product_ids .= $item->get_product_id() . ',';
            }
    
    
    
            $coupon_code = wp_rand();
            $amount = '10';
            $discount_type = 'fixed_cart'; // 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);
            update_post_meta($new_coupon_id, 'discount_type', $discount_type);
            update_post_meta($new_coupon_id, 'coupon_amount', $amount);
            update_post_meta($new_coupon_id, 'individual_use', 'no');
            update_post_meta($new_coupon_id, 'product_ids', $product_ids);
            update_post_meta($new_coupon_id, 'exclude_product_ids', '');
            update_post_meta($new_coupon_id, 'usage_limit', '1');
            update_post_meta($new_coupon_id, 'expiry_date', '');
            update_post_meta($new_coupon_id, 'apply_before_tax', 'yes');
            update_post_meta($new_coupon_id, 'free_shipping', 'no');
            unset($product_ids);
    
    
            echo '<h2 class="discount-coupon-title">THIS IS YOUR NEXT DISCOUNT</h2><p class="discount-coupon-text">When you\'re ready, welcome back to a 10% discount!<br/> This is your discount code: <code><strong>' . $coupon_code . '</strong></code></p>';
        }
    ?>
        <style>
        .discount-coupon-title { color: red; }
        .discount-coupon-text { color: black; }
        </style>
        <?php
    
    }
    

    Tested OK with WC 3.5.5 and WP 5.1