WooCommerce:检查优惠券是不是有效
Posted
技术标签:
【中文标题】WooCommerce:检查优惠券是不是有效【英文标题】:WooCommerce: Check if coupon is validWooCommerce:检查优惠券是否有效 【发布时间】:2017-02-06 07:59:02 【问题描述】:我正在尝试检查优惠券是否仍然有效(未达到其使用限制)并在此条件下显示内容。
这样做的原因是我希望能够向特定访问者发放优惠券代码,但显然不想发放已经达到使用限制的优惠券。
我正在尝试使用 php 来实现这一点,并想象代码是这样的:
<?php if (coupon('mycouponcode') isvalid)
echo "Coupon Valid"
else
echo "Coupon Usage Limit Reached"
?>
这里的任何帮助都会很棒:)
【问题讨论】:
一张优惠券取决于很多条件,所以你只想要使用限制? @RaunakGupta 是的,使用限制是我感兴趣的唯一标准:) 【参考方案1】:我相信最新的方法encouraged by WooCommerce 是使用\WC_Discounts 类。这是一个例子:
function is_referral_coupon_valid( $coupon_code )
$coupon = new \WC_Coupon( $coupon_code );
$discounts = new \WC_Discounts( WC()->cart );
$valid_response = $discounts->is_coupon_valid( $coupon );
if ( is_wp_error( $valid_response ) )
return false;
else
return true;
重要的是要记住至少在 wp_loaded 钩子中这样做,就像这样:
add_action( 'wp_loaded', function()
is_referral_coupon_valid('my-coupon-code');
);
更新
现在我相信使用is_valid() 的这种更简单的方法也可以工作,但我自己还没有测试过:
$coupon = new WC_Coupon( 'my-coupon-code' );
$coupon->is_valid();
【讨论】:
【参考方案2】:$code = 'test123';
$coupon = new WC_Coupon($code);
$coupon_post = get_post($coupon->id);
$coupon_data = array(
'id' => $coupon->id,
'code' => $coupon->code,
'type' => $coupon->type,
'created_at' => $coupon_post->post_date_gmt,
'updated_at' => $coupon_post->post_modified_gmt,
'amount' => wc_format_decimal($coupon->coupon_amount, 2),
'individual_use' => ( 'yes' === $coupon->individual_use ),
'product_ids' => array_map('absint', (array) $coupon->product_ids),
'exclude_product_ids' => array_map('absint', (array) $coupon->exclude_product_ids),
'usage_limit' => (!empty($coupon->usage_limit) ) ? $coupon->usage_limit : null,
'usage_count' => (int) $coupon->usage_count,
'expiry_date' => (!empty($coupon->expiry_date) ) ? date('Y-m-d', $coupon->expiry_date) : null,
'enable_free_shipping' => $coupon->enable_free_shipping(),
'product_category_ids' => array_map('absint', (array) $coupon->product_categories),
'exclude_product_category_ids' => array_map('absint', (array) $coupon->exclude_product_categories),
'exclude_sale_items' => $coupon->exclude_sale_items(),
'minimum_amount' => wc_format_decimal($coupon->minimum_amount, 2),
'maximum_amount' => wc_format_decimal($coupon->maximum_amount, 2),
'customer_emails' => $coupon->customer_email,
'description' => $coupon_post->post_excerpt,
);
$usage_left = $coupon_data['usage_limit'] - $coupon_data['usage_count'];
if ($usage_left > 0)
echo 'Coupon Valid';
else
echo 'Coupon Usage Limit Reached';
代码已经过测试并且功能齐全。
参考
WC_API_Coupons::get_coupon( $id, $fields )【讨论】:
这总是返回“已达到优惠券使用限制”。我已经用许多优惠券(有效的、过期的、用完的、用完的等)替换了 test123,但没有任何乐趣:( @NuclearApe:使用任何有效的优惠券打印$coupon_data['usage_limit']
、$coupon_data['usage_count']
和$coupon_data['expiry_date']
,看看他们返回了什么。
仅供参考:WC_Coupon 类有 2 个相关方法(is_valid()
和 is_valid_for_cart()
)。 但不幸的是,它们不起作用...有一个 错误,因为它们总是返回 false。所以我在 WordPress > Woocommerce 支持线程中发布了一个线程:wordpress.org/support/topic/…
@LoicTheAztec:是的,我见过这两种方法,但我猜is_valid_for_cart()
是针对当前购物车项目的。但是我上面的自定义代码运行良好,因为优惠券存储在wp_posts
表中,所有元数据都存储在wp_postmeta
中,所以我用少量优惠券进行了一些随机测试,对于这个问题,只需要 meta_key usage_count
和@ 987654333@.
我之前有过类似你的测试。但是我尝试过使用这两种方法,并且搜索了很多。与此方法相关的所有内容都会报告错误,因为它们总是返回 false。通常这种方法在这种情况下非常有用,可以编写非常简单紧凑的代码。但没有什么是完美的:)【参考方案3】:
您可以使用this 插件来实现您的目的。
【讨论】:
Link-only answers should be avoided:Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline.
【参考方案4】:
add_action( 'rest_api_init', 'coupon' );
function coupon($request)
register_rest_route('custom-plugin', '/coupon_code/',
array(
'methods' => 'POST',
'callback' => 'coupon_code',
)
);
function coupon_code($request)
$code = $request->get_param('coupon');
$applied_coupon = $request->get_param('applied_coupon');
$cart_amount = $request->get_param('cart_amount');
$user_id = $request->get_param('user_id');
// $code = 'test123';
$coupon = new WC_Coupon($code);
$coupon_post = get_post($coupon->id);
$coupon_data = array(
'id' => $coupon->id,
'code' => esc_attr($coupon_post->post_title),
'type' => $coupon->type,
'created_at' => $coupon_post->post_date_gmt,
'updated_at' => $coupon_post->post_modified_gmt,
'amount' => wc_format_decimal($coupon->coupon_amount, 2),
'individual_use' => ( 'yes' === $coupon->individual_use ),
'product_ids' => array_map('absint', (array) $coupon->product_ids),
'exclude_product_ids' => array_map('absint', (array) $coupon->exclude_product_ids),
'usage_limit' => (!empty($coupon->usage_limit) ) ? $coupon->usage_limit : null,
'usage_count' => (int) $coupon->usage_count,
'expiry_date' => (!empty($coupon->expiry_date) ) ? date('Y-m-d', $coupon->expiry_date) : null,
'enable_free_shipping' => $coupon->enable_free_shipping(),
'product_category_ids' => array_map('absint', (array) $coupon->product_categories),
'exclude_product_category_ids' => array_map('absint', (array) $coupon->exclude_product_categories),
'exclude_sale_items' => $coupon->exclude_sale_items(),
'minimum_amount' => wc_format_decimal($coupon->minimum_amount, 2),
'maximum_amount' => wc_format_decimal($coupon->maximum_amount, 2),
'customer_emails' => $coupon->customer_email,
'description' => $coupon_post->post_excerpt,
);
if ($coupon_data['code'] != $code)
$response['status'] = "failed";
$response['message'] = "COUPON ".$code." DOES NOT EXIST!";
return new WP_REST_Response($response);
$Acoupon = new WC_Coupon($applied_coupon);
// print_r($Acoupon);exit();
if($Acoupon->individual_use == 'yes')
$response['status'] = "failed";
$response['message'] = "SORRY, COUPON ".$applied_coupon." HAS ALREADY BEEN APPLIED AND CANNOT BE USED IN CONJUNCTION WITH OTHER COUPONS.";
$response['result'] = $Acoupon->individual_use;
return new WP_REST_Response($response);
if ($coupon_data['minimum_amount'] > $cart_amount)
$response['status'] = "failed";
$response['message'] = "THE MINIMUM SPEND FOR THIS COUPON IS $".$coupon_data['minimum_amount'].".";
return new WP_REST_Response($response);
if ($coupon_data['maximum_amount'] < $cart_amount)
$response['status'] = "failed";
$response['message'] = "THE MAXIMUM SPEND FOR THIS COUPON IS $".$coupon_data['maximum_amount'].".";
return new WP_REST_Response($response);
if ($coupon_data['exclude_sale_items'] == true)
$response['status'] = "failed";
$response['message'] = "SORRY, THIS COUPON IS NOT VALID FOR SALE ITEMS.";
return new WP_REST_Response($response);
$usage_left = $coupon_data['usage_limit'] - $coupon_data['usage_count'];
if ($usage_left == 0)
$response['status'] = "failed";
$response['message'] = "SORRY, COUPON USAGE LIMIT HAS BEEN REACHED.";
return new WP_REST_Response($response);
$current_time = date('Y-m-d');
$coupon_Expiry_date = $coupon->expiry_date;
if ($coupon_Expiry_date < $current_time)
$response['status'] = "failed";
$response['message'] = "THIS COUPON HAS EXPIRED.";
return new WP_REST_Response($response);
$user_limite = get_post_meta($coupon_data['id'], 'usage_limit_per_user', true);
$p_id = $coupon_data['id'];
global $wpdb;
$count_of_per_user = $wpdb->get_results("SELECT * FROM wp_postmeta where post_id = $p_id and meta_key = '_used_by' and meta_value = $user_id");
$count = count($count_of_per_user);
if ( $count >= $user_limite)
$response['status'] = "failed"`enter code here`;
$response['message'] = "COUPON USAGE LIMIT HAS BEEN REACHED.";
return new WP_REST_Response($response);
enter code here
else
$response['status'] = "Success";
$response['message'] = "COUPON APPLIED.";
return new WP_REST_Response($response);
use this function
【讨论】:
以上是关于WooCommerce:检查优惠券是不是有效的主要内容,如果未能解决你的问题,请参考以下文章
在woocommerce中手动对优惠券应用电子邮件限制的代码