我做出了两个功能,每个功能都挂在一个动作中。除了一件事外,他们都工作正常。我需要它一起工作。
当订单被标记为完整时,将使用wp_rand
生成优惠券。然后,我在订单电子邮件中添加了自定义文本,并在跨度标签中使用了$coupon_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);
}
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
}
测试ok 带有 WC 3.5.5 和 WP 5.1