【发布时间】:2019-04-24 08:28:45
【问题描述】:
我为每位客户从网站购买产品时动态生成优惠券代码,并且我设置了某些条件。
当我动态创建优惠券代码时,它存储在 woocommerce > 优惠券部分。
function couponCodeGeneration($order_id, $i){
// Get the order ID
$coupon_code = $order_id."".$i; // Coupon Code
$amount = '100%'; // Amount
$discount_type = 'percent'; // Type: fixed_cart, percent, fixed_product, percent_product
wp_coupon_exist( $coupon_code );
if( wp_coupon_exist( $coupon_code ) ) {
//coupon code exists.
} else {
//coupon code not exists, so inserting coupon code
$coupon = array(
'post_title' => $coupon_code,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'shop_coupon'
//'post_category' => array(1)
);
$new_coupon_id = wp_insert_post( $coupon );
//SET THE PRODUCT CATEGORIES
wp_set_object_terms($post_id, 'Holiday Season offers', 'product_cat');
// Add meta
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', 'yes' );
update_post_meta( $new_coupon_id, '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', '2019-07-31' );
update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
update_post_meta( $new_coupon_id, 'limit_usage_to_x_items', '1' );
update_post_meta( $new_coupon_id, 'usage_limit_per_user', '1' );
update_post_meta( $post_id, 'times', '1' );
echo '<div class="couponCode"><strong>Your Coupon Code for your next purchase - '.$coupon_code.'</strong><hr></div>';
}
}
在以下情况下我需要帮助。
生成的优惠券代码不应被其他客户使用。优惠券仅供该客户个人使用。优惠券代码无法转让。
当客户下订单时,生成的优惠券代码未存储在管理订单页面中。我怎么知道哪个优惠券代码是由哪个客户生成的。
谁能给我一个建议。
【问题讨论】:
-
您应该始终在问题中包含自定义的相关代码(或者如果它来自现有的 StackOverFlow 答案,您应该提供链接)。你是如何生成优惠券代码的?你的代码在哪里?
标签: php wordpress woocommerce checkout coupon