Php 将自定义优惠券添加到订单数据

Php 将自定义优惠券添加到订单数据,php,woocommerce,Php,Woocommerce,我已经设法在购买结束时为每位客户创建了一张个性化优惠券,但它只显示在感谢页面上。我希望能够将其作为个性化字段添加到订单的详细信息中,客户可以在“我的帐户”中看到它,并在完成订单时通过邮件发送 // Create a unique discount coupon for each customer at the end of their purchase if ( in_array( 'woocommerce/woocommerce.php', get_option( 'active_plugin

我已经设法在购买结束时为每位客户创建了一张个性化优惠券,但它只显示在感谢页面上。我希望能够将其作为个性化字段添加到订单的详细信息中,客户可以在“我的帐户”中看到它,并在完成订单时通过邮件发送

// Create a unique discount coupon for each customer at the end of their purchase
if ( in_array( 'woocommerce/woocommerce.php', get_option( 'active_plugins' ) ) ){

    add_filter( 'woocommerce_thankyou_order_received_text', 'create_automatic_coupon_only_for_this_customer', 99, 2 );
    function create_automatic_coupon_only_for_this_customer( $message, $order ){

        // Coupon configuration
        $discount_type = 'percent'; // Here you define the type of discount that this coupon applies. By default it is set in percentage 'percent', for fixed discount use 'fixed_cart'
        $amount = 10; //Here you define the discount amount associated with this coupon
        $expiration = 180; // Here you must indicate the validity of the coupon in days

        // Eliminate expired coupons and check if there is already a coupon associated with this order
        $coupon_id = clean_expired_coupons_and_check_coupons_order( $order );

            $order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( $_GET['key'] ) );

            if ( $order->get_id() > 0 ) {
    
                    // Check that the order exists and that there is still no coupon associated with the order
                    if ( $order->get_order_key() == $order_key ) {
            
                if ( !$coupon_id ) {
                    
                        //Create a unique coupon code for this customer
                    $customer_email = $order->get_billing_email();
                    $customer_email = explode( '@', $customer_email );
                    $customer_email = $customer_email[0];
                    $suffix = date( 'Hms' );
                
                    // Calculate the expiration date
                    $date = date( 'Y-m-d' );
                    $expiry_date = strtotime( $date .'+ '. $expiration .' days' );
                    $expiry_date = date( 'Y-m-d', $expiry_date );
                
                    $args = array(
                        'coupon_code'           => $customer_email.'_'.$suffix,
                        'discount_type'         => $discount_type,
                        'amount'            => $amount,
                        'individual_use'        => 'yes',
                        'usage_limit'           => 1,
                        'product_ids'           => '',
                        'exclude_product_ids'       => '',
                        'expiry_date'           => $expiry_date,
                        'apply_before_tax'      => '',
                        'free_shipping'         => 'no',
                    );
                
                    $coupon_code = strtoupper( $args[ 'coupon_code' ] );
                
                    $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', $args[ 'discount_type' ] );
                    update_post_meta( $new_coupon_id, 'coupon_amount', $args[ 'amount' ] );
                    update_post_meta( $new_coupon_id, 'individual_use', $args[ 'individual_use' ] );
                    update_post_meta( $new_coupon_id, 'product_ids', $args[ 'product_ids' ] );
                    update_post_meta( $new_coupon_id, 'exclude_product_ids', $args[ 'exclude_product_ids' ] );
                    update_post_meta( $new_coupon_id, 'usage_limit', $args[ 'usage_limit' ] );
                    update_post_meta( $new_coupon_id, 'expiry_date', $args[ 'expiry_date' ] );
                    update_post_meta( $new_coupon_id, 'apply_before_tax', $args[ 'apply_before_tax' ] );
                    update_post_meta( $new_coupon_id, 'free_shipping', $args[ 'free_shipping' ] );
                    update_post_meta( $new_coupon_id, 'order-id', $order->get_id() );
                
                    if ( is_wp_error( $new_coupon_id ) ){
                
                        return false;
                    }
                }else{

                    $coupon = get_post( $coupon_id );
                    $coupon_code = $coupon->post_title;
                }
        
                // Show the coupon on the final purchase page
                $message .= get_coupon_discount_for_the_next_purchase( $coupon_code, $expiration, $amount, $discount_type );
                    }
            }

            return $message;
    }
    
    function get_coupon_discount_for_the_next_purchase( $coupon_code, $expiration, $amount, $discount_type ){
    
        $discount = $amount;
        $discount .= 'percent' == $discount_type? '%' : get_woocommerce_currency();
        ob_start();
        ?>
        <div style="margin: 0 0 3.5em; text-align:center;">
            <p>To thank you for your trust in us, we have prepared a <strong> discount coupon for you <?php echo $discount; ?> for your next purchase:</strong></p>
            <h3 style="text-align:center;">YOUR DISCOUNT</h3>
            <div style="text-align: center; font-weight: bold; border: 2px #c7c7c7; border-style: dashed; padding: 13px; width: 50%; margin: 10px auto;"><?php echo $coupon_code;?></div>
            <p><strong>The coupon is valid for de <?php echo $expiration; ?> Days</strong>. But do not leave it too much because once that period has elapsed, the coupon will no longer be valid.</p>
        </div>
        <?php
        $content = ob_get_contents();
        ob_end_clean();

        return $content;
    }

    function clean_expired_coupons_and_check_coupons_order( $order = false ){
        $coupon_exist = false;

        if ( isset( $order ) ) {
            
            $arg = array(
                'post_type' => array( 'shop_coupon' ),
                    'meta_key' => 'order-id'
            );

            $auto_coupon_list = new WP_Query( $arg );

            foreach ( $auto_coupon_list->posts as $key => $coupon ) {
                
                // Eliminate expired coupons
                $coupon_expiry_date = get_post_meta( $coupon->ID, 'expiry_date', true );
                $today_date = date( 'Y-m-d' );

                if ( !empty( $coupon_expiry_date ) && ( $today_date > $coupon_expiry_date ) ) {
                    
                    wp_delete_post( $coupon->ID );
                    continue;
                }

                // Find an automatic coupon associated with this order
                $order_id_coupon_vinculated = get_post_meta( $coupon->ID, 'order-id', true );
                if ( !$coupon_exist && ( $order_id_coupon_vinculated == $order->get_id() ) ) {

                    $coupon_exist = $coupon->ID;
                }
            }
        }

        return $coupon_exist;
    }
}
我已尝试将信息记录为订单的备注,但我无法通过邮件发送,也无法将其保存为订单中的元数据

// Create a unique discount coupon for each customer at the end of their purchase
if ( in_array( 'woocommerce/woocommerce.php', get_option( 'active_plugins' ) ) ){

    add_filter( 'woocommerce_thankyou_order_received_text', 'create_automatic_coupon_only_for_this_customer', 99, 2 );
    function create_automatic_coupon_only_for_this_customer( $message, $order ){

        // Coupon configuration
        $discount_type = 'percent'; // Here you define the type of discount that this coupon applies. By default it is set in percentage 'percent', for fixed discount use 'fixed_cart'
        $amount = 10; //Here you define the discount amount associated with this coupon
        $expiration = 180; // Here you must indicate the validity of the coupon in days

        // Eliminate expired coupons and check if there is already a coupon associated with this order
        $coupon_id = clean_expired_coupons_and_check_coupons_order( $order );

            $order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( $_GET['key'] ) );

            if ( $order->get_id() > 0 ) {
    
                    // Check that the order exists and that there is still no coupon associated with the order
                    if ( $order->get_order_key() == $order_key ) {
            
                if ( !$coupon_id ) {
                    
                        //Create a unique coupon code for this customer
                    $customer_email = $order->get_billing_email();
                    $customer_email = explode( '@', $customer_email );
                    $customer_email = $customer_email[0];
                    $suffix = date( 'Hms' );
                
                    // Calculate the expiration date
                    $date = date( 'Y-m-d' );
                    $expiry_date = strtotime( $date .'+ '. $expiration .' days' );
                    $expiry_date = date( 'Y-m-d', $expiry_date );
                
                    $args = array(
                        'coupon_code'           => $customer_email.'_'.$suffix,
                        'discount_type'         => $discount_type,
                        'amount'            => $amount,
                        'individual_use'        => 'yes',
                        'usage_limit'           => 1,
                        'product_ids'           => '',
                        'exclude_product_ids'       => '',
                        'expiry_date'           => $expiry_date,
                        'apply_before_tax'      => '',
                        'free_shipping'         => 'no',
                    );
                
                    $coupon_code = strtoupper( $args[ 'coupon_code' ] );
                
                    $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', $args[ 'discount_type' ] );
                    update_post_meta( $new_coupon_id, 'coupon_amount', $args[ 'amount' ] );
                    update_post_meta( $new_coupon_id, 'individual_use', $args[ 'individual_use' ] );
                    update_post_meta( $new_coupon_id, 'product_ids', $args[ 'product_ids' ] );
                    update_post_meta( $new_coupon_id, 'exclude_product_ids', $args[ 'exclude_product_ids' ] );
                    update_post_meta( $new_coupon_id, 'usage_limit', $args[ 'usage_limit' ] );
                    update_post_meta( $new_coupon_id, 'expiry_date', $args[ 'expiry_date' ] );
                    update_post_meta( $new_coupon_id, 'apply_before_tax', $args[ 'apply_before_tax' ] );
                    update_post_meta( $new_coupon_id, 'free_shipping', $args[ 'free_shipping' ] );
                    update_post_meta( $new_coupon_id, 'order-id', $order->get_id() );
                
                    if ( is_wp_error( $new_coupon_id ) ){
                
                        return false;
                    }
                }else{

                    $coupon = get_post( $coupon_id );
                    $coupon_code = $coupon->post_title;
                }
        
                // Show the coupon on the final purchase page
                $message .= get_coupon_discount_for_the_next_purchase( $coupon_code, $expiration, $amount, $discount_type );
                    }
            }

            return $message;
    }
    
    function get_coupon_discount_for_the_next_purchase( $coupon_code, $expiration, $amount, $discount_type ){
    
        $discount = $amount;
        $discount .= 'percent' == $discount_type? '%' : get_woocommerce_currency();
        ob_start();
        ?>
        <div style="margin: 0 0 3.5em; text-align:center;">
            <p>To thank you for your trust in us, we have prepared a <strong> discount coupon for you <?php echo $discount; ?> for your next purchase:</strong></p>
            <h3 style="text-align:center;">YOUR DISCOUNT</h3>
            <div style="text-align: center; font-weight: bold; border: 2px #c7c7c7; border-style: dashed; padding: 13px; width: 50%; margin: 10px auto;"><?php echo $coupon_code;?></div>
            <p><strong>The coupon is valid for de <?php echo $expiration; ?> Days</strong>. But do not leave it too much because once that period has elapsed, the coupon will no longer be valid.</p>
        </div>
        <?php
        $content = ob_get_contents();
        ob_end_clean();

        return $content;
    }

    function clean_expired_coupons_and_check_coupons_order( $order = false ){
        $coupon_exist = false;

        if ( isset( $order ) ) {
            
            $arg = array(
                'post_type' => array( 'shop_coupon' ),
                    'meta_key' => 'order-id'
            );

            $auto_coupon_list = new WP_Query( $arg );

            foreach ( $auto_coupon_list->posts as $key => $coupon ) {
                
                // Eliminate expired coupons
                $coupon_expiry_date = get_post_meta( $coupon->ID, 'expiry_date', true );
                $today_date = date( 'Y-m-d' );

                if ( !empty( $coupon_expiry_date ) && ( $today_date > $coupon_expiry_date ) ) {
                    
                    wp_delete_post( $coupon->ID );
                    continue;
                }

                // Find an automatic coupon associated with this order
                $order_id_coupon_vinculated = get_post_meta( $coupon->ID, 'order-id', true );
                if ( !$coupon_exist && ( $order_id_coupon_vinculated == $order->get_id() ) ) {

                    $coupon_exist = $coupon->ID;
                }
            }
        }

        return $coupon_exist;
    }
}

对不起,除了评论,我已经翻译了所有内容。全是英文的,所以你可以帮我。谢谢。请使用插件“”来实现自动优惠券功能。对不起,除了注释之外,我已经翻译了所有内容。全是英文的,所以你可以帮我。谢谢。请使用插件“”实现自动优惠券功能。