Last active
July 26, 2022 08:36
-
-
Save runezero/560ece9ed0a22d604064778913a7e783 to your computer and use it in GitHub Desktop.
[Coupon admin email] Add the used coupon code in the email to the shop admin #woocommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| add_action( 'woocommerce_email_before_order_table', 'dev_maybe_add_used_coupon_code_admin_emails', 20, 4 ); | |
| function dev_maybe_add_used_coupon_code_admin_emails( $order, $sent_to_admin, $plain_text, $email ) { | |
| if ( $sent_to_admin !== false ) { | |
| $coupon_codes = $order->get_used_coupons(); | |
| if(!empty($coupon_codes)): | |
| foreach( $coupon_codes as $coupon_code ): | |
| // Get the WC_Coupon object | |
| $coupon = new WC_Coupon($coupon_code); | |
| $coupon_used_code = $coupon->get_code(); // Get coupon code | |
| $discount_type = $coupon->get_discount_type(); // Get coupon discount type | |
| $coupon_amount = $coupon->get_amount(); // Get coupon amount | |
| echo '<p class="email-coupon-used"><b>Voor deze bestelling is gebruik gemaakt van een kortingscode:</b><br/>'; | |
| echo 'Kortingscode: '. $coupon_used_code .'<br/>'; | |
| echo 'Korting aantal: '. $coupon_amount .'<br/>'; | |
| echo 'Kortingscode type: '. $discount_type .'</p>'; | |
| endforeach; | |
| endif; | |
| } | |
| } | |
| add_filter( 'woocommerce_get_order_item_totals', 'dev_coupon_filter_woocommerce_get_order_item_totals', 10, 3 ); | |
| function dev_coupon_filter_woocommerce_get_order_item_totals( $total_rows, $order, $tax_display ) { | |
| // Exit if there is no coupons applied | |
| if ( sizeof ( $order->get_coupon_codes() ) == 0 ) return $total_rows; | |
| $coupons = []; | |
| foreach ( $order->get_items('coupon') as $coupon ) { | |
| $coupons[] = $coupon->get_code(); | |
| } | |
| // Overwrite | |
| if(count($coupons) > 0 && isset($total_rows['discount'])){ | |
| $total_rows['discount']['label'] = __('Discount:', 'woocommerce') . " <span style='font-size:12px;'>" . implode(" ", $coupons) . "</span>"; | |
| } | |
| return $total_rows; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment