如何在编程创建WooCommerce订单时正确应用优惠券折扣?
问题:手动创建WooCommerce订单时优惠券折扣未体现在订单总额中
我在不使用购物车的情况下编程创建WooCommerce订单,其他功能正常,但应用优惠券后,折扣未体现在订单总额里。产品价格已显示折扣,但订单总价仍为原价。
当前代码
function create_order_with_pending_status_and_customer($email, $billing_link, $product_id, $quantity = 1, $coupon_code = "") { if (!class_exists('WC_Order')) { return 'Error: WooCommerce not loaded.'; } // Start session if not already started if (!session_id()) { session_start(); } // Check if an order ID exists in the session if (isset($_SESSION['order_id'])) { $order_id = $_SESSION['order_id']; $order = wc_get_order($order_id); // Validate the order if ($order && $order->get_status() === 'pending') { foreach ($order->get_items() as $item) { if ($item->get_product_id() == $product_id) { return $order_id; // Return existing order ID if product is already in the order } } } } // Get product details $product = wc_get_product($product_id); if (!$product || !$product->get_price()) { return 'Error: Product not found or invalid price.'; } // Get or create customer $user = get_user_by('email', $email); if ($user) { $customer_id = $user->ID; } else { $customer_id = wc_create_new_customer($email, '', wp_generate_password()); if (is_wp_error($customer_id)) { return 'Error creating customer: ' . $customer_id->get_error_message(); } } // Calculate total price $total_price = $product->get_price() * $quantity; // Create a new WooCommerce order $order = wc_create_order(); // Add product to the order $order->add_product($product, $quantity); // Set customer details $order->set_customer_id($customer_id); // Set billing details $billing_address = [ 'email' => $email, ]; $order->set_address($billing_address, 'billing'); // Add custom meta data $order->update_meta_data('billing_link', $billing_link); // Apply coupon code (if provided) if (!empty($coupon_code)) { $order->apply_coupon($coupon_code); } $order->calculate_totals(); // Set order status $order->set_status('pending'); // Set order total $order->set_total($total_price); // Save the order $order->save(); // Store the order ID in the session $_SESSION['order_id'] = $order->get_id(); return $order->get_id(); // Return the created Order ID }
问题现象
- 产品原价:
$24.99 - 折扣后产品单价:
$16.24 - 订单总额仍显示:
$24.99(预期为$16.24)
解决方案
问题根源是你手动调用$order->set_total($total_price);覆盖了calculate_totals()计算出的含折扣总价。calculate_totals()会自动处理优惠券折扣、税费等,手动设置总额会直接忽略这些计算结果。
修改步骤
- 删除代码中手动设置订单总额的行:
$order->set_total($total_price); - 保留
$order->calculate_totals();在应用优惠券之后执行,让WooCommerce自动计算最终总额。
修改后的完整函数
function create_order_with_pending_status_and_customer($email, $billing_link, $product_id, $quantity = 1, $coupon_code = "") { if (!class_exists('WC_Order')) { return 'Error: WooCommerce not loaded.'; } // Start session if not already started if (!session_id()) { session_start(); } // Check if an order ID exists in the session if (isset($_SESSION['order_id'])) { $order_id = $_SESSION['order_id']; $order = wc_get_order($order_id); // Validate the order if ($order && $order->get_status() === 'pending') { foreach ($order->get_items() as $item) { if ($item->get_product_id() == $product_id) { return $order_id; // Return existing order ID if product is already in the order } } } } // Get product details $product = wc_get_product($product_id); if (!$product || !$product->get_price()) { return 'Error: Product not found or invalid price.'; } // Get or create customer $user = get_user_by('email', $email); if ($user) { $customer_id = $user->ID; } else { $customer_id = wc_create_new_customer($email, '', wp_generate_password()); if (is_wp_error($customer_id)) { return 'Error creating customer: ' . $customer_id->get_error_message(); } } // Create a new WooCommerce order $order = wc_create_order(); // Add product to the order $order->add_product($product, $quantity); // Set customer details $order->set_customer_id($customer_id); // Set billing details $billing_address = [ 'email' => $email, ]; $order->set_address($billing_address, 'billing'); // Add custom meta data $order->update_meta_data('billing_link', $billing_link); // Apply coupon code (if provided) if (!empty($coupon_code)) { $order->apply_coupon($coupon_code); } // Calculate totals including coupon discounts $order->calculate_totals(); // Set order status $order->set_status('pending'); // Save the order $order->save(); // Store the order ID in the session $_SESSION['order_id'] = $order->get_id(); return $order->get_id(); // Return the created Order ID }
内容的提问来源于stack exchange,提问作者Robin Singh
相关产品推荐
相关产品推荐

