我做了两个函数,每个函数都与一个动作挂钩。除了一件事情,他们俩都很好。我需要它一起工作。
当订单标记为完成时,将使用wp_rand
生成优惠券。然后,我将自定义文本添加到订单电子邮件中,并在span标签中使用了$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);
}
答案 0 :(得分:0)
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
}
通过 WC 3.5.5 和 WP 5.1
进行了确定的测试