WooCommerce 快速下单插件

插件能帮助商店管理员快速创建订单,支持现有客户和新客户,并可以轻松应用折扣。

  • 快速创建订单
  • 支持现有客户和新客户
  • 实时产品搜索和库存检查
  • 灵活的折扣选项(固定金额或百分比)
  • 实时总额计算
  • 响应式管理界面
  • 多语言支持
  • 完整的错误处理和验证
<?php
/**
* Plugin Name: BDFG Quick Order for WooCommerce
* Plugin URI: https://beiduofengou.net/2024/02/15/quick-order-for-woocommerce/
* Description: Create WooCommerce orders quickly and efficiently! Perfect for store managers who need to process orders rapidly with automatic discounts and customer management.
* Version: 2.1.0
* Author: beiduofengou
* Author URI: https://beiduofengou.net
* Text Domain: bdfg-quick-order
* Domain Path: /languages
* Requires at least: 5.8
* Requires PHP: 7.4
* WC requires at least: 5.0
* WC tested up to: 8.5
* License: GPL v2 or later
*
* @package BDFG_Quick_Order
* @author beiduofengou
* @link https://beiduofengou.net
*/

// Exit if accessed directly - safety first!
if (!defined('ABSPATH')) {
exit;
}

// 定义插件常量
define('BDFG_QUICK_ORDER_VERSION', '2.1.0');
define('BDFG_QUICK_ORDER_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('BDFG_QUICK_ORDER_PLUGIN_URL', plugin_dir_url(__FILE__));

// 自动加载类
spl_autoload_register(function ($class) {
$prefix = 'BDFG_Quick_Order\\';
$base_dir = BDFG_QUICK_ORDER_PLUGIN_DIR . 'includes/';

$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
return;
}

$relative_class = substr($class, $len);
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';

if (file_exists($file)) {
require $file;
}
});

if (!class_exists('BDFG_Quick_Order')) {
/**
* 主插件类
*
* @since 2.1.0
*/
class BDFG_Quick_Order {
/**
* 插件实例
*
* @var BDFG_Quick_Order
*/
private static $instance = null;

/**
* 插件设置
*
* @var array
*/
private $settings;

/**
* 获取插件实例
*
* @return BDFG_Quick_Order
*/
public static function instance() {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}

/**
* 构造函数 - 设置基本配置并初始化钩子
*/
private function __construct() {
$this->settings = get_option('bdfg_quick_order_settings', array());

// 基础钩子
add_action('plugins_loaded', array($this, 'init'));
add_action('admin_init', array($this, 'check_environment'));
add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'add_plugin_links'));

// 加载功能模块
$this->load_modules();
}

/**
* 初始化插件
*/
public function init() {
// 加载语言包
load_plugin_textdomain('bdfg-quick-order', false, dirname(plugin_basename(__FILE__)) . '/languages');

// WooCommerce 依赖检查
if (!class_exists('WooCommerce')) {
add_action('admin_notices', array($this, 'woocommerce_missing_notice'));
return;
}

// 初始化管理面板
if (is_admin()) {
require_once BDFG_QUICK_ORDER_PLUGIN_DIR . 'admin/class-admin.php';
new BDFG_Quick_Order\Admin();
}
}

/**
* 加载功能模块
*/
private function load_modules() {
require_once BDFG_QUICK_ORDER_PLUGIN_DIR . 'includes/class-order-creator.php';
require_once BDFG_QUICK_ORDER_PLUGIN_DIR . 'includes/class-customer-manager.php';
require_once BDFG_QUICK_ORDER_PLUGIN_DIR . 'includes/class-discount-handler.php';
}

/**
* 检查运行环境
*/
public function check_environment() {
// PHP 版本检查
if (version_compare(PHP_VERSION, '7.4', '<')) {
add_action('admin_notices', array($this, 'php_version_notice'));
}

// WordPress 版本检查
if (version_compare($GLOBALS['wp_version'], '5.8', '<')) {
add_action('admin_notices', array($this, 'wp_version_notice'));
}
}

/**
* WooCommerce 缺失提示
*/
public function woocommerce_missing_notice() {
?>
<div class="notice notice-error">
<p><?php _e('BDFG Quick Order requires WooCommerce to be installed and activated. Please install WooCommerce to continue using this plugin.', 'bdfg-quick-order'); ?></p>
</div>
<?php
}

/**
* PHP 版本提示
*/
public function php_version_notice() {
?>
<div class="notice notice-error">
<p><?php _e('BDFG Quick Order requires PHP 7.4 or higher. Please upgrade your PHP version to continue using this plugin.', 'bdfg-quick-order'); ?></p>
</div>
<?php
}

/**
* 添加插件链接
*/
public function add_plugin_links($links) {
$plugin_links = array(
'<a href="' . admin_url('admin.php?page=bdfg-quick-order') . '">' . __('Settings', 'bdfg-quick-order') . '</a>',
'<a href="https://beiduofengou.net/docs/quick-order">' . __('Documentation', 'bdfg-quick-order') . '</a>'
);
return array_merge($plugin_links, $links);
}

/**
* 获取插件设置
*/
public function get_settings($key = null) {
if ($key) {
return isset($this->settings[$key]) ? $this->settings[$key] : null;
}
return $this->settings;
}
}
}

// 初始化插件
function BDFG_Quick_Order() {
return BDFG_Quick_Order::instance();
}

// 启动插件
BDFG_Quick_Order();

includes/class-order-creator.php

<?php
namespace BDFG_Quick_Order;

if (!defined('ABSPATH')) {
exit;
}

/**
* 订单创建管理类
*
* @since 2.1.0
*/
class Order_Creator {
/**
* 创建新订单
*
* @param int $user_id
* @param array $products
* @param string $discount_type
* @param float $discount_value
* @return int
* @throws \Exception
*/
public function create_order($user_id, $products, $discount_type, $discount_value) {
// 验证用户
$user = get_user_by('id', $user_id);
if (!$user) {
throw new \Exception(__('Invalid user ID', 'bdfg-quick-order'));
}

try {
// 开始创建订单
$order = wc_create_order(array(
'customer_id' => $user_id,
'created_via' => 'bdfg_quick_order'
));

// 添加商品
foreach ($products as $product) {
$this->add_product_to_order($order, $product);
}

// 应用折扣
if ($discount_type && $discount_value) {
$this->apply_discount($order, $discount_type, $discount_value);
}

// 计算订单总额
$order->calculate_totals();

// 添加订单备注
$order->add_order_note(
sprintf(
__('Order created via BDFG Quick Order by %s', 'bdfg-quick-order'),
wp_get_current_user()->display_name
)
);

// 保存订单
$order->save();

do_action('bdfg_quick_order_created', $order->get_id(), $user_id);

return $order->get_id();

} catch (\Exception $e) {
// 记录错误日志
error_log(sprintf(
'[BDFG Quick Order] Error creating order for user %d: %s',
$user_id,
$e->getMessage()
));
throw $e;
}
}

/**
* 添加商品到订单
*
* @param \WC_Order $order
* @param array $product_data
*/
private function add_product_to_order($order, $product_data) {
$product = wc_get_product($product_data['id']);

if (!$product) {
throw new \Exception(
sprintf(__('Product #%d not found', 'bdfg-quick-order'), $product_data['id'])
);
}

// 检查库存
if (!$product->is_in_stock() || !$product->has_enough_stock($product_data['quantity'])) {
throw new \Exception(
sprintf(__('Insufficient stock for product #%d', 'bdfg-quick-order'), $product_data['id'])
);
}

$order->add_product($product, $product_data['quantity']);
}

/**
* 应用折扣到订单
*
* @param \WC_Order $order
* @param string $type
* @param float $value
*/
private function apply_discount($order, $type, $value) {
if ($type === 'flat') {
$discount = new \WC_Order_Item_Fee();
$discount->set_name(__('Quick Order Discount', 'bdfg-quick-order'));
$discount->set_amount(-$value);
$discount->set_total(-$value);
$order->add_item($discount);
} elseif ($type === 'percent') {
$subtotal = $order->get_subtotal();
$discount_amount = ($subtotal * $value) / 100;
$discount = new \WC_Order_Item_Fee();
$discount->set_name(sprintf(__('%d%% Quick Order Discount', 'bdfg-quick-order'), $value));
$discount->set_amount(-$discount_amount);
$discount->set_total(-$discount_amount);
$order->add_item($discount);
}
}
}

includes/class-customer-manager.php

<?php
namespace BDFG_Quick_Order;

if (!defined('ABSPATH')) {
exit;
}

/**
* 客户管理类
*
* @since 2.1.0
*/
class Customer_Manager {
/**
* 创建或获取客户
*
* @param array $customer_data
* @return int
*/
public function create_or_get_customer($customer_data) {
// 验证邮箱
if (empty($customer_data['email']) || !is_email($customer_data['email'])) {
throw new \Exception(__('Invalid email address', 'bdfg-quick-order'));
}

$email = sanitize_email($customer_data['email']);

// 检查现有用户
$existing_user = get_user_by('email', $email);
if ($existing_user) {
$this->update_customer_meta($existing_user->ID, $customer_data);
return $existing_user->ID;
}

// 创建新用户
return $this->create_new_customer($customer_data);
}

/**
* 创建新客户
*
* @param array $customer_data
* @return int
*/
private function create_new_customer($customer_data) {
$username = $this->generate_username(
$customer_data['first_name'],
$customer_data['last_name'],
$customer_data['email']
);

$password = wp_generate_password(12, true);

$user_id = wp_create_user($username, $password, $customer_data['email']);

if (is_wp_error($user_id)) {
throw new \Exception($user_id->get_error_message());
}

// 更新用户信息
wp_update_user(array(
'ID' => $user_id,
'first_name' => sanitize_text_field($customer_data['first_name']),
'last_name' => sanitize_text_field($customer_data['last_name']),
'role' => 'customer',
'show_admin_bar_front' => false
));

// 更新用户元数据
$this->update_customer_meta($user_id, $customer_data);

// 发送新用户通知
$this->send_welcome_email($user_id, $password);

do_action('bdfg_quick_order_customer_created', $user_id, $customer_data);

return $user_id;
}

/**
* 生成用户名
*
* @param string $first_name
* @param string $last_name
* @param string $email
* @return string
*/
private function generate_username($first_name, $last_name, $email) {
$username = sanitize_user(
strtolower($first_name . '.' . $last_name),
true
);

// 如果用户名已存在,添加随机数
if (username_exists($username)) {
$base_username = $username;
$i = 1;
do {
$username = $base_username . $i;
$i++;
} while (username_exists($username));
}

return $username;
}

/**
* 更新客户元数据
*
* @param int $user_id
* @param array $customer_data
*/
private function update_customer_meta($user_id, $customer_data) {
// 账单地址
if (!empty($customer_data['billing'])) {
foreach ($customer_data['billing'] as $key => $value) {
update_user_meta($user_id, 'billing_' . $key, sanitize_text_field($value));
}
}

// 送货地址
if (!empty($customer_data['shipping'])) {
foreach ($customer_data['shipping'] as $key => $value) {
update_user_meta($user_id, 'shipping_' . $key, sanitize_text_field($value));
}
}

// 电话号码
if (!empty($customer_data['phone'])) {
update_user_meta($user_id, 'billing_phone', sanitize_text_field($customer_data['phone']));
}

// 更新最后修改时间
update_user_meta($user_id, '_bdfg_last_modified', current_time('mysql'));
}

/**
* 发送欢迎邮件
*
* @param int $user_id
* @param string $password
*/
private function send_welcome_email($user_id, $password) {
$user = get_userdata($user_id);
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);

$message = sprintf(__('Welcome to %s!', 'bdfg-quick-order'), $blogname) . "\r\n\r\n";
$message .= sprintf(__('Your username: %s', 'bdfg-quick-order'), $user->user_login) . "\r\n";
$message .= sprintf(__('Your password: %s', 'bdfg-quick-order'), $password) . "\r\n\r\n";
$message .= __('You can login at: ', 'bdfg-quick-order') . wp_login_url() . "\r\n\r\n";
$message .= sprintf(__('If you have any questions, please contact %s.', 'bdfg-quick-order'), get_option('admin_email'));

wp_mail(
$user->user_email,
sprintf(__('[%s] Your account details', 'bdfg-quick-order'), $blogname),
$message
);
}
}

includes/class-discount-handler.php

<?php
namespace BDFG_Quick_Order;

if (!defined('ABSPATH')) {
exit;
}

/**
* 折扣处理类
*
* @since 2.1.0
*/
class Discount_Handler {
/**
* 折扣类型
*/
const DISCOUNT_TYPE_FLAT = 'flat';
const DISCOUNT_TYPE_PERCENT = 'percent';

/**
* 验证折扣设置
*
* @param string $type
* @param float $value
* @return bool
*/
public function validate_discount($type, $value) {
if (!in_array($type, [self::DISCOUNT_TYPE_FLAT, self::DISCOUNT_TYPE_PERCENT])) {
return false;
}

if ($type === self::DISCOUNT_TYPE_PERCENT && ($value <= 0 || $value > 100)) {
return false;
}

if ($type === self::DISCOUNT_TYPE_FLAT && $value <= 0) {
return false;
}

return true;
}

/**
* 计算折扣金额
*
* @param float $subtotal
* @param string $type
* @param float $value
* @return float
*/
public function calculate_discount($subtotal, $type, $value) {
if (!$this->validate_discount($type, $value)) {
return 0;
}

if ($type === self::DISCOUNT_TYPE_FLAT) {
return min($value, $subtotal);
}

if ($type === self::DISCOUNT_TYPE_PERCENT) {
return ($subtotal * $value) / 100;
}

return 0;
}

/**
* 获取折扣描述
*
* @param string $type
* @param float $value
* @return string
*/
public function get_discount_description($type, $value) {
if ($type === self::DISCOUNT_TYPE_FLAT) {
return sprintf(
__('Flat discount of %s', 'bdfg-quick-order'),
wc_price($value)
);
}

if ($type === self::DISCOUNT_TYPE_PERCENT) {
return sprintf(
__('Percentage discount of %d%%', 'bdfg-quick-order'),
$value
);
}

return '';
}
}

admin/class-admin.php

<?php
namespace BDFG_Quick_Order;

if (!defined('ABSPATH')) {
exit;
}

/**
* 后台管理类
*
* @since 2.1.0
*/
class Admin {
/**
* 初始化管理界面
*/
public function __construct() {
add_action('admin_menu', array($this, 'add_menu_page'));
add_action('admin_enqueue_scripts', array($this, 'enqueue_assets'));
add_action('wp_ajax_bdfg_quick_order_create', array($this, 'handle_ajax_create_order'));
add_action('wp_ajax_bdfg_quick_order_search_products', array($this, 'handle_ajax_search_products'));
add_action('wp_ajax_bdfg_quick_order_search_customers', array($this, 'handle_ajax_search_customers'));
}

/**
* 添加菜单页面
*/
public function add_menu_page() {
add_submenu_page(
'woocommerce',
__('BDFG Quick Order', 'bdfg-quick-order'),
__('Quick Order', 'bdfg-quick-order'),
'manage_woocommerce',
'bdfg-quick-order',
array($this, 'render_page')
);
}

/**
* 加载资源文件
*/
public function enqueue_assets($hook) {
if ('woocommerce_page_bdfg-quick-order' !== $hook) {
return;
}

// CSS
wp_enqueue_style(
'bdfg-quick-order-admin',
BDFG_QUICK_ORDER_PLUGIN_URL . 'assets/css/admin.css',
array(),
BDFG_QUICK_ORDER_VERSION
);

// JavaScript
wp_enqueue_script(
'bdfg-quick-order-admin',
BDFG_QUICK_ORDER_PLUGIN_URL . 'assets/js/admin.js',
array('jquery', 'select2'),
BDFG_QUICK_ORDER_VERSION,
true
);

// 本地化脚本
wp_localize_script(
'bdfg-quick-order-admin',
'bdfgQuickOrder',
array(
'ajaxUrl' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('bdfg_quick_order_nonce'),
'i18n' => array(
'errorTitle' => __('Error', 'bdfg-quick-order'),
'successTitle' => __('Success', 'bdfg-quick-order'),
'orderCreated' => __('Order created successfully!', 'bdfg-quick-order'),
'searchProducts' => __('Search products...', 'bdfg-quick-order'),
'searchCustomers' => __('Search customers...', 'bdfg-quick-order'),
'loading' => __('Processing...', 'bdfg-quick-order'),
)
)
);
}

/**
* 渲染管理页面
*/
public function render_page() {
include BDFG_QUICK_ORDER_PLUGIN_DIR . 'admin/views/quick-order.php';
}

/**
* 处理创建订单的AJAX请求
*/
public function handle_ajax_create_order() {
check_ajax_referer('bdfg_quick_order_nonce', 'nonce');

if (!current_user_can('manage_woocommerce')) {
wp_send_json_error(__('Permission denied', 'bdfg-quick-order'));
}

try {
$order_creator = new Order_Creator();
$customer_manager = new Customer_Manager();

// 处理客户数据
$customer_id = $customer_manager->create_or_get_customer($_POST['customer']);

// 创建订单
$order_id = $order_creator->create_order(
$customer_id,
$_POST['products'],
$_POST['discount_type'],
$_POST['discount_value']
);

wp_send_json_success(array(
'order_id' => $order_id,
'redirect_url' => admin_url('post.php?post=' . $order_id . '&action=edit')
));

} catch (\Exception $e) {
wp_send_json_error($e->getMessage());
}
}

/**
* 处理搜索产品的AJAX请求
*/
public function handle_ajax_search_products() {
check_ajax_referer('bdfg_quick_order_nonce', 'nonce');

$search_term = isset($_GET['term']) ? sanitize_text_field($_GET['term']) : '';
$products = wc_get_products(array(
'status' => 'publish',
'limit' => 10,
's' => $search_term,
));

$results = array();
foreach ($products as $product) {
$results[] = array(
'id' => $product->get_id(),
'text' => $product->get_name() . ' (#' . $product->get_id() . ')',
'price' => $product->get_price(),
'stock' => $product->get_stock_quantity()
);
}

wp_send_json($results);
}

/**
* 处理搜索客户的AJAX请求
*/
public function handle_ajax_search_customers() {
check_ajax_referer('bdfg_quick_order_nonce', 'nonce');

$search_term = isset($_GET['term']) ? sanitize_text_field($_GET['term']) : '';

$customers = get_users(array(
'search' => '*' . $search_term . '*',
'search_columns' => array('user_login', 'user_email', 'display_name'),
'role' => 'customer',
'number' => 10
));

$results = array();
foreach ($customers as $customer) {
$results[] = array(
'id' => $customer->ID,
'text' => sprintf(
'%s (%s)',
$customer->display_name,
$customer->user_email
)
);
}

wp_send_json($results);
}
}

assets/js/admin.js

(function($) {
'use strict';

// BDFG Quick Order Admin
var BDFGQuickOrder = {
init: function() {
this.initializeSelects();
this.initializeProductTable();
this.initializeDiscountFields();
this.initializeForm();
},

initializeSelects: function() {
// 客户选择
$('#customer_select').select2({
ajax: {
url: ajaxurl,
dataType: 'json',
delay: 250,
data: function(params) {
return {
term: params.term,
action: 'bdfg_quick_order_search_customers',
nonce: bdfgQuickOrder.nonce
};
},
processResults: function(data) {
return {
results: data
};
},
cache: true
},
minimumInputLength: 2,
placeholder: bdfgQuickOrder.i18n.searchCustomers
});

// 产品选择
$('#product_select').select2({
ajax: {
url: ajaxurl,
dataType: 'json',
delay: 250,
data: function(params) {
return {
term: params.term,
action: 'bdfg_quick_order_search_products',
nonce: bdfgQuickOrder.nonce
};
},
processResults: function(data) {
return {
results: data
};
},
cache: true
},
minimumInputLength: 2,
placeholder: bdfgQuickOrder.i18n.searchProducts
});
},

initializeProductTable: function() {
var self = this;

// 添加产品按钮
$('#add_product').on('click', function(e) {
e.preventDefault();
var productData = $('#product_select').select2('data')[0];
var quantity = parseInt($('#product_quantity').val(), 10);

if (!productData || quantity <= 0) {
return;
}

self.addProductRow(productData, quantity);
$('#product_select').val(null).trigger('change');
$('#product_quantity').val(1);
});

// 删除产品
$('#product_table').on('click', '.remove-product', function(e) {
e.preventDefault();
$(this).closest('tr').remove();
self.updateTotals();
});

// 数量变化
$('#product_table').on('change', '.product-quantity', function() {
self.updateTotals();
});
},

addProductRow: function(productData, quantity) {
var row = $('<tr>')
.append($('<td>').text(productData.text))
.append($('<td>').text(productData.price))
.append(
$('<td>').append(
$('<input>')
.attr('type', 'number')
.addClass('product-quantity')
.val(quantity)
.attr('min', 1)
.attr('max', productData.stock)
)
)
.append(
$('<td>').append(
$('<button>')
.addClass('button remove-product')
.text('×')
)
)
.append(
$('<input>')
.attr('type', 'hidden')
.addClass('product-id')
.val(productData.id)
);

$('#product_table tbody').append(row);
this.updateTotals();
},

initializeDiscountFields: function() {
var self = this;

$('input[name="discount_type"]').on('change', function() {
var type = $(this).val();
var valueField = $('#discount_value');

if (type === 'percent') {
valueField.attr('max', 100);
} else {
valueField.removeAttr('max');
}

self.updateTotals();
});

$('#discount_value').on('change', function() {
self.updateTotals();
});
},

updateTotals: function() {
var subtotal = 0;
var discount = 0;

// 计算小计
$('#product_table tbody tr').each(function() {
var price = parseFloat($(this).find('td:eq(1)').text());
var quantity = parseInt($(this).find('.product-quantity').val(), 10);
subtotal += price * quantity;
});

// 计算折扣
var discountType = $('input[name="discount_type"]:checked').val();
var discountValue = parseFloat($('#discount_value').val()) || 0;

if (discountType === 'percent') {
discount = (subtotal * discountValue) / 100;
} else if (discountType === 'flat') {
discount = discountValue;
}

// 更新显示
$('#subtotal').text(this.formatPrice(subtotal));
$('#discount_amount').text(this.formatPrice(discount));
$('#total').text(this.formatPrice(subtotal - discount));
},

formatPrice: function(price) {
return price.toFixed(2);
},

initializeForm: function() {
var self = this;

$('#quick_order_form').on('submit', function(e) {
e.preventDefault();
self.submitForm();
});
},

submitForm: function() {
var self = this;
var $form = $('#quick_order_form');
var $submitButton = $form.find('button[type="submit"]');

// 收集产品数据
var products = [];
$('#product_table tbody tr').each(function() {
products.push({
id: $(this).find('.product-id').val(),
quantity: $(this).find('.product-quantity').val()
});
});

if (products.length === 0) {
alert(bdfgQuickOrder.i18n.errorNoProducts);
return;
}

// 禁用提交按钮
$submitButton.prop('disabled', true)
.html('<span class="spinner is-active"></span> ' + bdfgQuickOrder.i18n.loading);

// 发送AJAX请求
$.ajax({
url: ajaxurl,
type: 'POST',
data: {
action: 'bdfg_quick_order_create',
nonce: bdfgQuickOrder.nonce,
customer: {
id: $('#customer_select').val(),
email: $('#customer_email').val(),
first_name: $('#customer_first_name').val(),
last_name: $('#customer_last_name').val(),
phone: $('#customer_phone').val(),
billing: {
address_1: $('#billing_address_1').val(),
address_2: $('#billing_address_2').val(),
city: $('#billing_city').val(),
state: $('#billing_state').val(),
postcode: $('#billing_postcode').val(),
country: $('#billing_country').val()
}
},
products: products,
discount_type: $('input[name="discount_type"]:checked').val(),
discount_value: $('#discount_value').val()
},
success: function(response) {
if (response.success) {
window.location.href = response.data.redirect_url;
} else {
alert(response.data || bdfgQuickOrder.i18n.errorGeneric);
$submitButton.prop('disabled', false)
.text(bdfgQuickOrder.i18n.submitOrder);
}
},
error: function() {
alert(bdfgQuickOrder.i18n.errorGeneric);
$submitButton.prop('disabled', false)
.text(bdfgQuickOrder.i18n.submitOrder);
}
});
}
};

// 页面加载完成后初始化
$(document).ready(function() {
BDFGQuickOrder.init();
});

})(jQuery);

admin/views/quick-order.php

<?php
if (!defined('ABSPATH')) {
exit;
}
?>
<div class="wrap bdfg-quick-order">
<h1 class="wp-heading-inline">
<?php _e('BDFG Quick Order', 'bdfg-quick-order'); ?>
</h1>

<hr class="wp-header-end">

<form id="quick_order_form" method="post">
<div class="bdfg-quick-order-grid">
<!-- 客户信息部分 -->
<div class="bdfg-quick-order-section">
<h2><?php _e('Customer Information', 'bdfg-quick-order'); ?></h2>
<table class="form-table">
<tr>
<th>
<label for="customer_select"><?php _e('Existing Customer', 'bdfg-quick-order'); ?></label>
</th>
<td>
<select id="customer_select" class="bdfg-select2" style="width: 100%;">
<option></option>
</select>
</td>
</tr>
<tr class="new-customer-fields">
<th>
<label for="customer_email"><?php _e('Email', 'bdfg-quick-order'); ?></label>
</th>
<td>
<input type="email" id="customer_email" name="customer_email" class="regular-text" required>
</td>
</tr>
<tr class="new-customer-fields">
<th>
<label for="customer_first_name"><?php _e('First Name', 'bdfg-quick-order'); ?></label>
</th>
<td>
<input type="text" id="customer_first_name" name="customer_first_name" class="regular-text" required>
</td>
</tr>
<tr class="new-customer-fields">
<th>
<label for="customer_last_name"><?php _e('Last Name', 'bdfg-quick-order'); ?></label>
</th>
<td>
<input type="text" id="customer_last_name" name="customer_last_name" class="regular-text" required>
</td>
</tr>
<tr class="new-customer-fields">
<th>
<label for="customer_phone"><?php _e('Phone', 'bdfg-quick-order'); ?></label>
</th>
<td>
<input type="tel" id="customer_phone" name="customer_phone" class="regular-text">
</td>
</tr>
</table>
</div>

<!-- 订单产品部分 -->
<div class="bdfg-quick-order-section">
<h2><?php _e('Order Products', 'bdfg-quick-order'); ?></h2>
<div class="bdfg-product-selector">
<select id="product_select" style="width: 60%;">
<option></option>
</select>
<input type="number" id="product_quantity" value="1" min="1" style="width: 80px;">
<button type="button" id="add_product" class="button"><?php _e('Add Product', 'bdfg-quick-order'); ?></button>
</div>

<table id="product_table" class="widefat">
<thead>
<tr>
<th><?php _e('Product', 'bdfg-quick-order'); ?></th>
<th><?php _e('Price', 'bdfg-quick-order'); ?></th>
<th><?php _e('Quantity', 'bdfg-quick-order'); ?></th>
<th><?php _e('Actions', 'bdfg-quick-order'); ?></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>

<!-- 折扣部分 -->
<div class="bdfg-quick-order-section">
<h2><?php _e('Discount', 'bdfg-quick-order'); ?></h2>
<div class="bdfg-discount-options">
<label>
<input type="radio" name="discount_type" value="flat" checked>
<?php _e('Flat Amount', 'bdfg-quick-order'); ?>
</label>
<label>
<input type="radio" name="discount_type" value="percent">
<?php _e('Percentage', 'bdfg-quick-order'); ?>
</label>
<input type="number" id="discount_value" name="discount_value" value="0" min="0" step="0.01">
</div>
</div>

<!-- 订单总计部分 -->
<div class="bdfg-quick-order-section">
<h2><?php _e('Order Totals', 'bdfg-quick-order'); ?></h2>
<table class="bdfg-order-totals">
<tr>
<th><?php _e('Subtotal:', 'bdfg-quick-order'); ?></th>
<td><?php echo get_woocommerce_currency_symbol(); ?> <span id="subtotal">0.00</span></td>
</tr>
<tr>
<th><?php _e('Discount:', 'bdfg-quick-order'); ?></th>
<td><?php echo get_woocommerce_currency_symbol(); ?> <span id="discount_amount">0.00</span></td>
</tr>
<tr class="order-total">
<th><?php _e('Total:', 'bdfg-quick-order'); ?></th>
<td><?php echo get_woocommerce_currency_symbol(); ?> <span id="total">0.00</span></td>
</tr>
</table>
</div>
</div>

<div class="bdfg-quick-order-actions">
<button type="submit" class="button button-primary button-large">
<?php _e('Create Order', 'bdfg-quick-order'); ?>
</button>
</div>
</form>
</div>

assets/css/admin.css

/* BDFG Quick Order Admin Styles */
.bdfg-quick-order {
margin: 20px 0;
}

.bdfg-quick-order-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 20px;
margin-bottom: 20px;
}

.bdfg-quick-order-section {
background: #fff;
padding: 20px;
border: 1px solid #ccd0d4;
border-radius: 4px;
}

.bdfg-quick-order-section h2 {
margin-top: 0;
padding-bottom: 12px;
border-bottom: 1px solid #eee;
color: #23282d;
font-size: 1.3em;
}

.bdfg-product-selector {
margin-bottom: 20px;
display: flex;
gap: 10px;
align-items: center;
}

#product_table {
border-collapse: collapse;
margin-top: 10px;
width: 100%;
}

#product_table th {
background: #f8f9fa;
text-align: left;
padding: 8px;
}

#product_table td {
padding: 8px;
border-bottom: 1px solid #eee;
}

.bdfg-discount-options {
display: flex;
gap: 15px;
align-items: center;
margin-bottom: 20px;
}

.bdfg-discount-options label {
margin-right: 15px;
}

.bdfg-order-totals {
width: 100%;
margin-top: 15px;
}

.bdfg-order-totals th {
text-align: left;
padding: 8px 0;
}

.bdfg-order-totals td {
text-align: right;
padding: 8px 0;
}

.bdfg-order-totals .order-total {
font-weight: bold;
font-size: 1.1em;
border-top: 2px solid #eee;
}

.bdfg-quick-order-actions {
margin-top: 20px;
padding: 20px;
background: #fff;
border: 1px solid #ccd0d4;
border-radius: 4px;
text-align: right;
}

.remove-product {
color: #cc1818;
background: none;
border: none;
padding: 0 5px;
cursor: pointer;
font-size: 18px;
}

.remove-product:hover {
color: #dc3232;
}

/* 响应式调整 */
@media screen and (max-width: 782px) {
.bdfg-quick-order-grid {
grid-template-columns: 1fr;
}

.bdfg-product-selector {
flex-direction: column;
align-items: stretch;
}

.bdfg-product-selector select,
.bdfg-product-selector input {
width: 100% !important;
margin-bottom: 10px;
}
}

/* Select2 优化 */
.select2-container--default .select2-selection--single {
border-color: #8c8f94;
border-radius: 4px;
}

.select2-container .select2-selection--single {
height: 30px;
}

.select2-container--default .select2-selection--single .select2-selection__rendered {
line-height: 30px;
}

.select2-container--default .select2-selection--single .select2-selection__arrow {
height: 28px;
}

languages/bdfg-quick-order-zh_CN.po

msgid ""
msgstr ""
"Project-Id-Version: BDFG Quick Order for WooCommerce 2.1.0\n"
"Report-Msgid-Bugs-To: https://beiduofengou.net/support\n"
"POT-Creation-Date: 2024-02-15 09:57:58+00:00\n"
"PO-Revision-Date: 2024-02-15 09:57:58+00:00\n"
"Last-Translator: beiduofengou <[email protected]>\n"
"Language-Team: BDFG <[email protected]>\n"
"Language: zh_CN\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: BDFG PO Generator 1.0\n"
"X-Poedit-KeywordsList: __;_e;_n:1,2;_x:1,2c;_ex:1,2c;_nx:4c,1,2;esc_attr__;"
"esc_attr_e;esc_attr_x:1,2c;esc_html__;esc_html_e;esc_html_x:1,2c;_n_noop:1,2;"
"_nx_noop:3c,1,2;__ngettext_noop:1,2\n"
"X-Poedit-Basepath: ..\n"
"X-Poedit-SearchPath-0: .\n"
"X-Poedit-SearchPathExcluded-0: node_modules\n"
"X-Poedit-SearchPathExcluded-1: vendor\n"

msgid "BDFG Quick Order"
msgstr "BDFG 快速下单"

msgid "Quick Order"
msgstr "快速下单"

msgid "Customer Information"
msgstr "客户信息"

msgid "Existing Customer"
msgstr "现有客户"

msgid "Email"
msgstr "电子邮箱"

msgid "First Name"
msgstr "名字"

msgid "Last Name"
msgstr "姓氏"

msgid "Phone"
msgstr "电话"

msgid "Order Products"
msgstr "订单产品"

msgid "Add Product"
msgstr "添加产品"

msgid "Product"
msgstr "产品"

msgid "Price"
msgstr "价格"

msgid "Quantity"
msgstr "数量"

msgid "Actions"
msgstr "操作"

msgid "Discount"
msgstr "折扣"

msgid "Flat Amount"
msgstr "固定金额"

msgid "Percentage"
msgstr "百分比"

msgid "Order Totals"
msgstr "订单总计"

msgid "Subtotal:"
msgstr "小计:"

msgid "Discount:"
msgstr "折扣:"

msgid "Total:"
msgstr "总计:"

msgid "Create Order"
msgstr "创建订单"

msgid "Order created successfully"
msgstr "订单创建成功"

msgid "An error occurred"
msgstr "发生错误"

msgid "Permission denied"
msgstr "权限不足"

msgid "Missing required customer information"
msgstr "缺少必要的客户信息"

msgid "Product #%d not found"
msgstr "未找到产品 #%d"

msgid "Order created via BDFG Quick Order"
msgstr "通过 BDFG 快速下单创建的订单"

msgid "BDFG Quick Order requires WooCommerce to be installed and activated."
msgstr "BDFG 快速下单需要安装并激活 WooCommerce。"

msgid "Search products..."
msgstr "搜索产品..."

msgid "Search customers..."
msgstr "搜索客户..."

msgid "Processing..."
msgstr "处理中..."

msgid "Error"
msgstr "错误"

msgid "Success"
msgstr "成功"

msgid "Welcome to %s!"
msgstr "欢迎来到 %s!"

msgid "Your username: %s"
msgstr "您的用户名:%s"

msgid "Your password: %s"
msgstr "您的密码:%s"

msgid "You can login at: "
msgstr "您可以在此登录:"

msgid "If you have any questions, please contact %s."
msgstr "如有任何问题,请联系 %s。"

使用方法

 

  1. 在 WooCommerce 菜单下找到”Quick Order”选项
  2. 选择现有客户或填写新客户信息
  3. 搜索并添加产品到订单
  4. 设置折扣(如需要)
  5. 点击”创建订单”完成操作

Leave a Comment