WooCommerce: Check if coupon is valid

被刻印的时光 ゝ 提交于 2019-12-10 09:34:14

问题


I am trying to check if a coupon is still valid (hasn't reached its usage limit) and display content under this condition.

The reason for this is that I want to be able to hand out a coupon code to particular visitors, but obviously don't want to hand out a coupon that has already reached it's usage limit.

I am trying to achieve this with PHP and imagine the code to be something like this:

<?php if (coupon('mycouponcode') isvalid) {
  echo "Coupon Valid"
} else {
  echo "Coupon Usage Limit Reached"
} ?>

Any help here would be great :)


回答1:


$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';
}

The code is tested and fully functional.

Reference

  • WC_API_Coupons::get_coupon( $id, $fields )



回答2:


I believe the most recent method encouraged by WooCommerce is using the \WC_Discounts Class. Here is an example:

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;
    }
}

It's important to remember doing that inside the wp_loaded hook at least, like this:

add_action( 'wp_loaded', function(){
   is_referral_coupon_valid('my-coupon-code');
});



回答3:


You can use this plugin for your purpose.



来源:https://stackoverflow.com/questions/39745791/woocommerce-check-if-coupon-is-valid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!