WooCommerce购物车验证通过后如何自动应用优惠券?
问题
我正在WooCommerce中开发功能,允许用户添加商品到购物车时输入外部生成的代金券代码,验证该代金券未被使用后,需为订单应用通用优惠券。
我已通过以下代码实现代金券验证功能:
// Define field label name function get_field_label_name() { return __("Voucher"); } // Add a custom product note below product meta in single product pages add_action('woocommerce_before_single_variation', 'my_product_custom_field', 100); function my_product_custom_field() { $voucher_check = get_field('voucher'); $voucher_text = get_field('voucher_text'); if ($voucher_check) { echo '<div class="vouchers_single">'; echo '<p>' . $voucher_text . '</p>'; woocommerce_form_field('custom_field1', array( 'type' => 'text', 'class' => array('my-field-class form-row-wide'), 'label' => get_field_label_name(), 'placeholder' => __("The field placeholder…"), 'required' => true, // Or false ), ''); echo '</div>'; } } // Check if the custom field value is unique add_filter('woocommerce_add_to_cart_validation', 'wc_add_on_feature', 20, 3); function wc_add_on_feature($passed, $product_id, $quantity) { if (isset($_POST['custom_field1']) && !empty($_POST['custom_field1'])) { global $wpdb; $label = get_field_label_name(); $value = sanitize_text_field($_POST['custom_field1']); // Check if value exits already $result = $wpdb->get_var(" SELECT COUNT(meta_value) FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key LIKE '$label' AND meta_value LIKE '$value' "); // If it exist we don't allow add to cart if ($result > 0) { // Display an error notice wc_add_notice(sprintf(__('This "%s" input already exist. Please choose another one…'), $value), 'error'); $passed = false; } } return $passed; }
同时我有以下优惠券应用代码片段:
add_action('woocommerce_before_cart_table', 'ts_apply_discount_to_cart'); function ts_apply_discount_to_cart() { $order_total = WC()->cart->get_subtotal(); if ($order_total > 250) { $coupon_code = 'retrofitwest'; if (!WC()->cart->add_discount(sanitize_text_field($coupon_code))) { wc_print_notices(); } } }
请问需要使用哪个过滤器,才能在自定义字段验证通过后应用该优惠券?
解决方案
你不需要额外的新过滤器,直接在**woocommerce_add_to_cart_validation**验证通过的逻辑后,添加优惠券应用代码即可。这个过滤器在验证通过、商品即将加入购物车时触发,此时执行优惠券添加操作时机刚好。
修改后的wc_add_on_feature函数如下:
// Check if the custom field value is unique and apply coupon on validation pass add_filter('woocommerce_add_to_cart_validation', 'wc_add_on_feature', 20, 3); function wc_add_on_feature($passed, $product_id, $quantity) { if (isset($_POST['custom_field1']) && !empty($_POST['custom_field1'])) { global $wpdb; $label = get_field_label_name(); $value = sanitize_text_field($_POST['custom_field1']); // Check if value exits already $result = $wpdb->get_var(" SELECT COUNT(meta_value) FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key LIKE '$label' AND meta_value LIKE '$value' "); // If it exist we don't allow add to cart if ($result > 0) { // Display an error notice wc_add_notice(sprintf(__('This "%s" input already exist. Please choose another one…'), $value), 'error'); $passed = false; } else { // 验证通过,应用优惠券 $coupon_code = 'retrofitwest'; // 先检查优惠券是否已添加,避免重复添加 if (!WC()->cart->has_discount($coupon_code)) { if (!WC()->cart->add_discount(sanitize_text_field($coupon_code))) { wc_add_notice(__('优惠券应用失败,请重试。'), 'error'); } else { wc_add_notice(sprintf(__('优惠券 "%s" 已成功应用!'), $coupon_code), 'success'); } } } } return $passed; }
补充说明:
- 移除原来的
woocommerce_before_cart_table钩子代码,避免重复执行优惠券添加逻辑。 - 在代金券验证通过(
$result == 0)的分支里直接执行优惠券添加操作,确保只有验证通过后才触发优惠。 - 增加
has_discount检查,防止用户重复添加商品时多次应用同一优惠券。 - 添加成功/失败提示通知,提升用户体验。
内容的提问来源于stack exchange,提问作者Paddy Winsley
相关产品推荐
相关产品推荐

