send voucher codes via email woocommerce
-
Hi.. I am working on a webshop which sells voucher codes. I am using woocommerce. I already have voucher codes for each product. i have placed them in custom field created in inventory section of each product.
what i am trying to do is when payment is done then depending upon the item unique voucher code is sent with order processing email.
i have achieved that as well.but problem is when two or more orders are placed at same time same voucher codes are sent.but i wanna send unique codes.
can anyone get me out of this problem.. pls
is there anyway that my function will run once at a time per order?
my so far my code is as below:add_action( 'woocommerce_email_before_order_table', 'add_order_email_instructions', 10, 2 ); function add_order_email_instructions( $order, $sent_to_admin ) { if ( 'processing' == $order->status ) { if ( count( $order->get_items() ) > 0 ) { echo '<table cellpadding="6" color: #696969; style="border-collapse: collapse; width: 100%; font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif;" > <tr> <th style="text-align:left; border:1px solid #9f9f9f;" >Product</th> <th style="text-align:left; border:1px solid #9f9f9f;" >Quantity</th> <th style="text-align:left; border:1px solid #9f9f9f;" >voucher Code</th></tr>'; $items = $order->get_items() ; foreach( $items as $item ) { $product_id = $item['product_id']; $product_name = $item['name']; $product_qty = $item['qty']; $v_codes = get_post_meta($product_id, '_voucher_codes_field', true); $v_codes_array = explode(',', $v_codes ); echo '<tr> <td style="text-align:left; border:1px solid #9f9f9f;" >'.$product_name.'</td> <td style="text-align:left; border:1px solid #9f9f9f;" >'.$product_qty.'</td> <td style="text-align:left; border:1px solid #9f9f9f;" >'; if($product_qty > 1){ $v_qty = get_post_meta($product_id, '_stock', true); $v_qty = $v_qty; for($i = 1; $i <= $product_qty; $i++){ $pointer = $v_qty - $i; echo 'code-'. $i.' : '.$v_codes_array[$pointer]. ' : '.$pointer .''; } }else if($product_qty == 1){ $v_qty = get_post_meta($product_id, '_stock', true) - 1; echo 'code :'. $v_codes_array[$v_qty]. ' : '.$v_qty; } echo '</td></tr>'; } echo '</table>'; $order->reduce_order_stock(); } } }
[Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been damaged by the forum’s parser.]
note: i am using virtual products. and i am aware of pdf voucher but that plugin doesn’t work with virtual products.
- The topic ‘send voucher codes via email woocommerce’ is closed to new replies.