WordPress(WooCommerce)定制虚拟可下载产品需求:字段验证、邮箱限购
WordPress(WooCommerce)虚拟可下载产品完整实现方案
以下是针对需求的分步技术实现方案,所有代码可添加至主题functions.php文件或自定义插件中:
1. 自定义购买表单与支付流程
产品页添加必填字段
在虚拟产品的"加入购物车"按钮前插入姓名、手机号、邮箱字段,并将字段值存入Session传递至结账环节:
// 产品页添加自定义购买字段 add_action('woocommerce_before_add_to_cart_button', 'render_custom_checkout_fields'); function render_custom_checkout_fields() { global $product; if (!$product->is_virtual()) return; ?> <div class="custom-checkout-group"> <p class="form-row form-row-wide"> <label for="buyer_name">姓名 <span class="required">*</span></label> <input type="text" class="input-text" name="buyer_name" id="buyer_name" required> </p> <p class="form-row form-row-wide"> <label for="buyer_phone">手机号 <span class="required">*</span></label> <input type="tel" class="input-text" name="buyer_phone" id="buyer_phone" required> </p> <p class="form-row form-row-wide"> <label for="buyer_email">邮箱 <span class="required">*</span></label> <input type="email" class="input-text" name="buyer_email" id="buyer_email" required> </p> </div> <?php } // 将字段值存入Session add_action('woocommerce_add_to_cart', 'save_custom_fields_to_session'); function save_custom_fields_to_session() { if (isset($_POST['buyer_name'], $_POST['buyer_phone'], $_POST['buyer_email'])) { WC()->session->set('buyer_name', sanitize_text_field($_POST['buyer_name'])); WC()->session->set('buyer_phone', sanitize_text_field($_POST['buyer_phone'])); WC()->session->set('buyer_email', sanitize_email($_POST['buyer_email'])); } } // 将字段关联至订单元数据 add_action('woocommerce_checkout_create_order', 'attach_custom_fields_to_order', 10, 2); function attach_custom_fields_to_order($order, $data) { if ($name = WC()->session->get('buyer_name')) { $order->update_meta_data('_buyer_name', $name); } if ($phone = WC()->session->get('buyer_phone')) { $order->update_meta_data('_buyer_phone', $phone); } if ($email = WC()->session->get('buyer_email')) { $order->update_meta_data('_buyer_email', $email); $order->set_billing_email($email); // 覆盖订单默认邮箱 } }
2. 邮箱限购校验
在加入购物车时校验该邮箱是否已购买当前产品,已购买则跳转至error.php:
add_filter('woocommerce_add_to_cart_validation', 'validate_email_purchase_restriction', 10, 3); function validate_email_purchase_restriction($passed, $product_id, $quantity) { if (!isset($_POST['buyer_email'])) return $passed; $buyer_email = sanitize_email($_POST['buyer_email']); // 查询该邮箱是否有已完成的同产品订单 $existing_orders = get_posts([ 'meta_key' => '_buyer_email', 'meta_value' => $buyer_email, 'post_type' => 'shop_order', 'post_status' => ['wc-completed'], 'meta_query' => [ ['key' => '_product_id', 'value' => $product_id, 'compare' => '='] ] ]); if (!empty($existing_orders)) { wp_redirect(get_site_url() . '/error.php'); exit; } return $passed; }
3. 支付成功后生成定制下载文件
订单完成时自动生成包含用户信息和个性化编码的文本文件,并在感谢页展示下载链接:
// 订单完成时生成定制文件 add_action('woocommerce_order_status_completed', 'generate_custom_download_file', 10, 1); function generate_custom_download_file($order_id) { $order = wc_get_order($order_id); $name = $order->get_meta('_buyer_name'); $phone = $order->get_meta('_buyer_phone'); $email = $order->get_meta('_buyer_email'); $order_number = $order->get_order_number(); $product = $order->get_items()[0]->get_product(); $custom_code = get_post_meta($product->get_id(), '_custom_product_code', true); // 生成文件内容 $file_content = "亲爱的{$name},感谢您的购买\n您的手机号为{$phone},邮箱为{$email},订单号为{$order_number},点击下方链接即可下载文件:\n" . get_site_url() . "/download?code={$custom_code}"; // 保存文件至媒体库 $upload_dir = wp_upload_dir(); $file_path = $upload_dir['path'] . "/order_{$order_id}_custom.txt"; file_put_contents($file_path, $file_content); $file_url = $upload_dir['url'] . "/order_{$order_id}_custom.txt"; $order->update_meta_data('_custom_download_url', $file_url); $order->save(); } // 在感谢页展示下载链接 add_action('woocommerce_thankyou', 'display_custom_download_button', 10, 1); function display_custom_download_button($order_id) { $order = wc_get_order($order_id); $download_url = $order->get_meta('_custom_download_url'); if ($download_url) { echo '<div class="custom-download-box"> <h3>您的专属下载链接</h3> <p><a href="' . esc_url($download_url) . '" class="button alt">立即下载文件</a></p> </div>'; } }
4. 独立数据库表存储订单详情
创建独立表存储订单核心信息,避免与WooCommerce默认订单表耦合:
// 激活时创建自定义订单表 register_activation_hook(__FILE__, 'create_custom_order_table'); function create_custom_order_table() { global $wpdb; $table_name = $wpdb->prefix . 'custom_orders'; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE $table_name ( id mediumint(9) NOT NULL AUTO_INCREMENT, order_id bigint(20) NOT NULL, order_number varchar(20) NOT NULL, purchase_date datetime NOT NULL, total_amount decimal(10,2) NOT NULL, buyer_name varchar(100) NOT NULL, buyer_phone varchar(20) NOT NULL, buyer_email varchar(100) NOT NULL, product_id bigint(20) NOT NULL, product_name varchar(200) NOT NULL, PRIMARY KEY (id), UNIQUE KEY order_id (order_id) ) $charset_collate;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } // 订单完成时插入数据至自定义表 add_action('woocommerce_order_status_completed', 'insert_order_to_custom_table', 10, 1); function insert_order_to_custom_table($order_id) { global $wpdb; $order = wc_get_order($order_id); $item = $order->get_items()[0]; $product = $item->get_product(); $wpdb->insert( $wpdb->prefix . 'custom_orders', [ 'order_id' => $order_id, 'order_number' => $order->get_order_number(), 'purchase_date' => $order->get_date_created()->date('Y-m-d H:i:s'), 'total_amount' => $order->get_total(), 'buyer_name' => $order->get_meta('_buyer_name'), 'buyer_phone' => $order->get_meta('_buyer_phone'), 'buyer_email' => $order->get_meta('_buyer_email'), 'product_id' => $product->get_id(), 'product_name' => $product->get_name() ] ); }
5. 禁用注册与冗余字段
后台设置
进入WooCommerce > 设置 > 账户与隐私,取消勾选"允许客户在结账时创建账户"。
代码隐藏地址字段
// 移除结账页冗余地址字段 add_filter('woocommerce_checkout_fields', 'remove_unnecessary_checkout_fields'); function remove_unnecessary_checkout_fields($fields) { unset($fields['billing']['billing_address_1']); unset($fields['billing']['billing_address_2']); unset($fields['billing']['billing_city']); unset($fields['billing']['billing_postcode']); unset($fields['billing']['billing_country']); unset($fields['billing']['billing_state']); unset($fields['shipping']); // 虚拟产品无需配送字段 return $fields; } // 强制关闭结账注册要求 add_filter('woocommerce_checkout_registration_required', '__return_false');
6. 开源加密货币支付插件推荐
- Bitcoin and Altcoin Payments:支持BTC、ETH等主流加密货币,开源免费,支持手动/自动确认,集成简单。
- WooCommerce Cryptocurrency Payment Gateway:轻量级插件,直接对接钱包地址,无需第三方支付处理器,完全开源。
7. 多产品个性化编码配置
在产品编辑页添加个性化编码字段,用于区分不同产品的定制文件:
// 产品编辑页添加编码字段 add_action('woocommerce_product_options_general_product_data', 'add_product_custom_code_field'); function add_product_custom_code_field() { woocommerce_wp_text_input([ 'id' => '_custom_product_code', 'label' => '个性化编码', 'placeholder' => '输入产品专属编码', 'desc_tip' => true, 'description' => '该编码将嵌入定制下载文件中' ]); } // 保存产品编码至元数据 add_action('woocommerce_process_product_meta', 'save_product_custom_code_field'); function save_product_custom_code_field($post_id) { if (isset($_POST['_custom_product_code'])) { update_post_meta($post_id, '_custom_product_code', sanitize_text_field($_POST['_custom_product_code'])); } }
内容的提问来源于stack exchange,提问作者Salvadora
相关产品推荐
相关产品推荐

