WooCommerce中优惠券验证的分析

在WooCommerce中,优惠券功能其实是很强大的,在创建时,我们能看到许多限制,在很多情况下,我们不能让用户使用优惠券,这就需要对优惠券的验证非常了解。

在woocommerce/includes/class-wc-discounts.php文件中,我们可以看到is_coupon_valid这个函数,就是优惠券的验证过程。源码:

public function is_coupon_valid( $coupon ) {
try {
$this->validate_coupon_exists( $coupon );
$this->validate_coupon_usage_limit( $coupon );
$this->validate_coupon_user_usage_limit( $coupon );
$this->validate_coupon_expiry_date( $coupon );
$this->validate_coupon_minimum_amount( $coupon );
$this->validate_coupon_maximum_amount( $coupon );
$this->validate_coupon_product_ids( $coupon );
$this->validate_coupon_product_categories( $coupon );
$this->validate_coupon_excluded_items( $coupon );
$this->validate_coupon_eligible_items( $coupon );
if ( ! apply_filters( 'woocommerce_coupon_is_valid', true, $coupon, $this ) ) {
throw new Exception( __( 'Coupon is not valid.', 'woocommerce' ), 100 );
}
} catch ( Exception $e ) {
/**
* Filter the coupon error message.
*
* @param string $error_message Error message.
* @param int $error_code Error code.
* @param WC_Coupon $coupon Coupon data.
*/
$message = apply_filters( 'woocommerce_coupon_error', is_numeric( $e->getMessage() ) ? $coupon->get_coupon_error( $e->getMessage() ) : $e->getMessage(), $e->getCode(), $coupon );
return new WP_Error(
'invalid_coupon',
$message,
array(
'status' => 400,
)
);
}
return true;
}
public function is_coupon_valid( $coupon ) {
  try {
    $this->validate_coupon_exists( $coupon );
    $this->validate_coupon_usage_limit( $coupon );
    $this->validate_coupon_user_usage_limit( $coupon );
    $this->validate_coupon_expiry_date( $coupon );
    $this->validate_coupon_minimum_amount( $coupon );
    $this->validate_coupon_maximum_amount( $coupon );
    $this->validate_coupon_product_ids( $coupon );
    $this->validate_coupon_product_categories( $coupon );
    $this->validate_coupon_excluded_items( $coupon );
    $this->validate_coupon_eligible_items( $coupon );

    if ( ! apply_filters( 'woocommerce_coupon_is_valid', true, $coupon, $this ) ) {
      throw new Exception( __( 'Coupon is not valid.', 'woocommerce' ), 100 );
    }
  } catch ( Exception $e ) {
    /**
     * Filter the coupon error message.
     *
     * @param string    $error_message Error message.
     * @param int       $error_code    Error code.
     * @param WC_Coupon $coupon        Coupon data.
     */
    $message = apply_filters( 'woocommerce_coupon_error', is_numeric( $e->getMessage() ) ? $coupon->get_coupon_error( $e->getMessage() ) : $e->getMessage(), $e->getCode(), $coupon );

    return new WP_Error(
      'invalid_coupon',
      $message,
      array(
        'status' => 400,
      )
    );
  }
  return true;
}
public function is_coupon_valid( $coupon ) { try { $this->validate_coupon_exists( $coupon ); $this->validate_coupon_usage_limit( $coupon ); $this->validate_coupon_user_usage_limit( $coupon ); $this->validate_coupon_expiry_date( $coupon ); $this->validate_coupon_minimum_amount( $coupon ); $this->validate_coupon_maximum_amount( $coupon ); $this->validate_coupon_product_ids( $coupon ); $this->validate_coupon_product_categories( $coupon ); $this->validate_coupon_excluded_items( $coupon ); $this->validate_coupon_eligible_items( $coupon ); if ( ! apply_filters( 'woocommerce_coupon_is_valid', true, $coupon, $this ) ) { throw new Exception( __( 'Coupon is not valid.', 'woocommerce' ), 100 ); } } catch ( Exception $e ) { /** * Filter the coupon error message. * * @param string $error_message Error message. * @param int $error_code Error code. * @param WC_Coupon $coupon Coupon data. */ $message = apply_filters( 'woocommerce_coupon_error', is_numeric( $e->getMessage() ) ? $coupon->get_coupon_error( $e->getMessage() ) : $e->getMessage(), $e->getCode(), $coupon ); return new WP_Error( 'invalid_coupon', $message, array( 'status' => 400, ) ); } return true; }

我们可以看到很多关于优惠券的验证,比如是否存在、次数验证、时间验证、用户限制、分类验证、产品验证等等。阅读完这些代码,你可以在任何场景限制用户使用优惠券,我也是依此写过优惠券Tag限制、优惠券产品属性限制等插件。

© 版权声明
THE END
喜欢就支持一下吧
点赞3 分享
I am the luckiest person in the world.
我是世界上最幸运的人
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片快捷回复

    暂无评论内容