如何在WooCommerce优惠券使用限制标签的排除产品字段前添加自定义复选框
解决WooCommerce优惠券自定义字段移至「排除产品」之前的问题
你当前使用的woocommerce_coupon_options_usage_restriction钩子默认会在「使用限制」标签的所有默认字段之后输出内容,要将自定义字段移到「排除产品」字段之前,可通过以下两种方法实现:
方法一:JavaScript调整DOM位置(简单快速)
保持原有字段添加代码不变,通过后台页脚注入JS,在页面加载完成后调整字段位置:
// 添加自定义字段(保留原有代码) add_action('woocommerce_coupon_options_usage_restriction', 'add_custom_coupon_condition_field', 10, 1); function add_custom_coupon_condition_field($coupon) { $apply_cart_condition_on_products = get_post_meta($coupon->get_id(), 'apply_cart_condition_for_customer_on_products', true); woocommerce_wp_checkbox( [ 'id' => 'apply_cart_condition_for_customer_on_products', 'label' => esc_html__( 'Product Cart Condition', 'hexcoupon' ), 'description' => esc_html__( 'Check this box to add a cart condition for the customer based on product.', 'hexcoupon' ), 'value' => $apply_cart_condition_on_products, ] ); } // 注入JS调整字段位置 add_action('admin_footer', 'move_custom_coupon_field_position'); function move_custom_coupon_field_position() { global $pagenow; // 仅在优惠券编辑页面执行 if ($pagenow === 'post.php' && get_post_type() === 'shop_coupon') { ?> <script> jQuery(document).ready(function($) { // 定位自定义字段容器 const customFieldRow = $('#apply_cart_condition_for_customer_on_products').closest('tr'); // 定位排除产品字段容器 const excludeProductsRow = $('#exclude_product_ids').closest('tr'); // 将自定义字段移至排除产品之前 customFieldRow.insertBefore(excludeProductsRow); }); </script> <?php } } // 保存自定义字段值 add_action('woocommerce_coupon_options_save', 'save_custom_coupon_field_value', 10, 1); function save_custom_coupon_field_value($post_id) { $field_value = isset($_POST['apply_cart_condition_for_customer_on_products']) ? 'yes' : 'no'; update_post_meta($post_id, 'apply_cart_condition_for_customer_on_products', $field_value); }
方法二:纯PHP重写字段输出(更优雅)
移除默认的「排除产品」字段,在「允许产品」字段之后重新输出自定义字段和「排除产品」字段:
// 移除默认的排除产品字段 add_filter('woocommerce_admin_field_text', 'remove_default_exclude_products_field', 10, 1); function remove_default_exclude_products_field($field) { if ($field['id'] === 'exclude_product_ids') { return false; } return $field; } // 在允许产品字段之后添加自定义字段和排除产品字段 add_action('woocommerce_coupon_options_after_products', 'insert_custom_field_before_exclude_products', 10, 1); function insert_custom_field_before_exclude_products($coupon) { // 输出自定义复选框 $apply_cart_condition_on_products = get_post_meta($coupon->get_id(), 'apply_cart_condition_for_customer_on_products', true); woocommerce_wp_checkbox( [ 'id' => 'apply_cart_condition_for_customer_on_products', 'label' => esc_html__( 'Product Cart Condition', 'hexcoupon' ), 'description' => esc_html__( 'Check this box to add a cart condition for the customer based on product.', 'hexcoupon' ), 'value' => $apply_cart_condition_on_products, ] ); // 重新输出排除产品字段 $exclude_product_ids = implode(',', array_map('absint', $coupon->get_excluded_product_ids('edit'))); woocommerce_wp_text_input([ 'id' => 'exclude_product_ids', 'label' => __( 'Exclude products', 'woocommerce' ), 'placeholder' => __( 'Search for a product…', 'woocommerce' ), 'description' => __( 'Products that this coupon will not apply to.', 'woocommerce' ), 'type' => 'hidden', 'class' => 'wc-product-search', 'custom_attributes' => [ 'data-placeholder' => __( 'Search for a product…', 'woocommerce' ), 'data-action' => 'woocommerce_json_search_products_and_variations', ], 'value' => $exclude_product_ids, ]); } // 保存自定义字段值 add_action('woocommerce_coupon_options_save', 'save_custom_coupon_field_value', 10, 1); function save_custom_coupon_field_value($post_id) { $field_value = isset($_POST['apply_cart_condition_for_customer_on_products']) ? 'yes' : 'no'; update_post_meta($post_id, 'apply_cart_condition_for_customer_on_products', $field_value); }
两种方法都能实现需求,方法一适合快速调整,方法二更符合WordPress/WooCommerce的钩子规范。
内容的提问来源于stack exchange,提问作者Shahadat Hossain
相关产品推荐
相关产品推荐

