在购买结束时,我设法为每个客户创建了个性化的优惠券,但是该优惠券仅显示在“谢谢”页面上。我希望能够将其作为个性化字段添加到订单的详细信息中,以便客户可以在“我的帐户”中看到它,并且在完成订单时将通过邮件发送。
我试图将信息记录为订单的注释,但是我无法通过邮件发送,也无法将其另存为元数据。
// 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];
$sufix = 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.'_'.$sufix,
'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;
}
}
我已成功将优惠券保存为订购单,但是我无法通过电子邮件发送优惠券,也无法将其放置在“我的帐户”的“订购”部分中。
// Show the coupon on the final purchase page
$message .= get_coupon_discount_for_the_next_purchase( $coupon_code, $expiration, $amount, $discount_type );
// Add the order note
$order->add_order_note( $message );
// Save the data
$order->save();
}
}