【问题标题】:Auto-generated Coupon does not show on Order Email自动生成的优惠券未显示在订单电子邮件中
【发布时间】:2019-07-21 14:49:40
【问题描述】:

我制作了两个函数,每个函数都与一个动作挂钩。除了一件事外,它们都工作正常;我需要它一起工作。

当订单标记为完成时,将使用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);
}

【问题讨论】:

    标签: wordpress woocommerce orders coupon


    【解决方案1】:
    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.5WP 5.1

    测试正常

    【讨论】:

      猜你喜欢
      • 2020-07-28
      • 2016-12-22
      • 1970-01-01
      • 2020-09-18
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 2020-11-24
      • 1970-01-01
      相关资源
      最近更新 更多