如何在PHP中创建WooCommerce订单?WordPress众筹插件开发需求
嘿,我帮你把这段WooCommerce订单代码适配到你的众筹网站插件里,结合你需要的「特定项目+自定义捐款金额」的核心需求,整理出一套可用的方案:
适配众筹场景的WooCommerce订单创建方案
你的原始代码是基础的订单创建逻辑,但针对众筹场景(用户自主选捐款额、关联特定项目),我们需要把它改成可动态传参的灵活函数,同时适配WooCommerce的最新规范。
1. 完善后的订单创建核心函数
这个函数支持传入众筹项目ID、用户自定义捐款额、用户信息,完全贴合你的需求:
function create_crowdfunding_order($project_id, $donation_amount, $user_info) { // 初始化WooCommerce订单对象(兼容新版WooCommerce) $order = wc_create_order(); if (!$order) { return new WP_Error('order_create_failed', '无法创建捐款订单'); } // 将指定众筹项目(WooCommerce产品)添加到订单 $project_product = wc_get_product($project_id); if (!$project_product) { return new WP_Error('invalid_project', '指定的众筹项目不存在'); } $order->add_product($project_product, 1); // 数量固定为1,金额用用户自定义捐款额 // 覆盖产品默认价格,设置为用户捐款金额 $order->set_total(floatval($donation_amount)); // 设置账单地址(如果众筹不需要实物回报,可跳过发货地址) $address = array( 'first_name' => sanitize_text_field($user_info['first_name']), 'last_name' => sanitize_text_field($user_info['last_name']), 'email' => sanitize_email($user_info['email']), 'phone' => sanitize_text_field($user_info['phone']), 'address_1' => sanitize_text_field($user_info['address_1']), 'address_2' => sanitize_text_field($user_info['address_2'] ?? ''), 'city' => sanitize_text_field($user_info['city']), 'state' => sanitize_text_field($user_info['state']), 'postcode' => sanitize_text_field($user_info['postcode']), 'country' => sanitize_text_field($user_info['country']) ); $order->set_address($address, 'billing'); // 如果有实物回报,再设置发货地址;没有的话可以注释掉 // $order->set_address($address, 'shipping'); // 设置订单状态:根据你的需求选,比如processing(处理中)或completed(已完成) $order->update_status('processing', '众筹捐款订单已创建', true); // 保存订单 $order->save(); // 返回订单ID,方便后续跳转或通知用户 return $order->get_id(); }
2. 如何调用这个函数
当用户提交捐款表单后,你可以这样获取参数并调用函数:
// 示例:从表单或用户登录信息中获取数据 $target_project_id = 123; // 你要指定的众筹项目ID(对应WooCommerce产品ID) $user_donation = 88.00; // 用户输入的捐款金额 $current_user = wp_get_current_user(); // 如果用户已登录,直接取用户信息;未登录则从表单获取 $user_info = array( 'first_name' => $current_user->first_name ?: sanitize_text_field($_POST['first_name']), 'last_name' => $current_user->last_name ?: sanitize_text_field($_POST['last_name']), 'email' => $current_user->user_email ?: sanitize_email($_POST['email']), 'phone' => sanitize_text_field($_POST['phone']), 'address_1' => sanitize_text_field($_POST['address_1']), 'city' => sanitize_text_field($_POST['city']), 'state' => sanitize_text_field($_POST['state']), 'postcode' => sanitize_text_field($_POST['postcode']), 'country' => sanitize_text_field($_POST['country']) ); // 调用创建订单函数 $order_id = create_crowdfunding_order($target_project_id, $user_donation, $user_info); // 处理结果 if (!is_wp_error($order_id)) { // 订单创建成功,比如跳转到订单详情页 wp_redirect(get_permalink(get_option('woocommerce_myaccount_page_id')) . 'view-order/' . $order_id . '/'); exit; } else { // 订单创建失败,提示用户 echo '抱歉,捐款订单创建失败:' . $order_id->get_error_message(); }
3. 关键注意事项
- 项目设置:把你的众筹项目设置为WooCommerce的虚拟产品(不需要发货),如果没有实物回报,可同时勾选“可下载”,避免生成不必要的物流流程。
- 金额验证:调用函数前一定要验证
$user_donation是合法的正数,且不低于你设置的项目最低捐款额(比如用if ($user_donation < 10) { ... }拦截)。 - 版本兼容:如果你的WooCommerce是3.0以上版本,推荐用
wc_create_order()和wc_get_product(),旧版的$woocommerce->order->create()已经被弃用。 - 用户登录优化:如果用户已登录,直接用
wp_get_current_user()获取信息,减少用户填写表单的步骤,提升体验。
内容的提问来源于stack exchange,提问作者Vitalie Miron
相关产品推荐
相关产品推荐

