-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdship.php
More file actions
243 lines (232 loc) · 11.1 KB
/
Copy pathdship.php
File metadata and controls
243 lines (232 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
/*
Plugin Name: Automated Dropshipping System
Description: Automates dropshipping order placement and inventory management.
Version: 1.0
Author: BLACKBOXAI
*/
// Exit if accessed directly
if ( !defined('ABSPATH') ) exit;
// Hook to add admin menu
add_action('admin_menu', 'ads_add_admin_menu');
function ads_add_admin_menu() {
add_menu_page('Dropshipping System', 'Dropshipping System', 'manage_options', 'ads_main_menu', 'ads_main_page', 'dashicons-cart', 6);
}
// Register settings for suppliers, products, and API keys etc
add_action('admin_init', 'ads_settings_init');
function ads_settings_init(){
// Suppliers option group (for simplicity, store as serialized array)
register_setting('ads_group', 'ads_suppliers');
register_setting('ads_group', 'ads_products');
register_setting('ads_group', 'ads_orders');
}
// Admin page UI
function ads_main_page(){
// Handle form submissions for adding supplier, product, placing manual orders
if (isset($_POST['ads_action'])){
$action = sanitize_text_field($_POST['ads_action']);
if($action === 'add_supplier'){
if (!isset($_POST['ads_add_supplier_nonce']) || !wp_verify_nonce($_POST['ads_add_supplier_nonce'], 'ads_add_supplier_action')) {
wp_die('Security check failed');
}
$suppliers = get_option('ads_suppliers', []);
$new_supplier = [
'id' => uniqid(),
'name' => sanitize_text_field($_POST['supplier_name']),
'api_url' => esc_url_raw($_POST['supplier_api']),
'api_key' => sanitize_text_field($_POST['supplier_key'])
];
$suppliers[] = $new_supplier;
update_option('ads_suppliers', $suppliers);
echo '<div class="updated"><p>Supplier added!</p></div>';
} elseif($action === 'add_product'){
if (!isset($_POST['ads_add_product_nonce']) || !wp_verify_nonce($_POST['ads_add_product_nonce'], 'ads_add_product_action')) {
wp_die('Security check failed');
}
$products = get_option('ads_products', []);
$suppliers = get_option('ads_suppliers', []);
$supplier_id = sanitize_text_field($_POST['product_supplier']);
// Find supplier name
$supplier_name = '';
foreach($suppliers as $s){
if($s['id'] === $supplier_id){
$supplier_name = $s['name'];
break;
}
}
if(!$supplier_name){
echo '<div class="error"><p>Invalid supplier selected</p></div>';
} else {
$new_product = [
'id' => uniqid(),
'name' => sanitize_text_field($_POST['product_name']),
'supplier_id' => $supplier_id,
'supplier_name' => $supplier_name,
'price' => floatval($_POST['product_price']),
'stock' => intval($_POST['product_stock'])
];
$products[] = $new_product;
update_option('ads_products',$products);
echo '<div class="updated"><p>Product added!</p></div>';
}
} elseif($action === 'place_order'){
if (!isset($_POST['ads_place_order_nonce']) || !wp_verify_nonce($_POST['ads_place_order_nonce'], 'ads_place_order_action')) {
wp_die('Security check failed');
}
// Simulate order placement to supplier's API.
$products = get_option('ads_products', []);
$orders = get_option('ads_orders', []);
$order_product_id = sanitize_text_field($_POST['order_product']);
$order_quantity = intval($_POST['order_quantity']);
$ordered_product = null;
foreach($products as &$p){
if($p['id'] === $order_product_id){
$ordered_product = &$p;
break;
}
}
if(!$ordered_product){
echo '<div class="error"><p>Product not found</p></div>';
} elseif($ordered_product['stock'] < $order_quantity){
echo '<div class="error"><p>Insufficient stock for this product!</p></div>';
} else {
// Simulate API order placement by supplier (this would be a real API call)
// For demonstration, just reduce stock and save order details
$ordered_product['stock'] -= $order_quantity;
update_option('ads_products', $products);
$new_order = [
'id' => uniqid(),
'product_name' => $ordered_product['name'],
'quantity' => $order_quantity,
'status' => 'Order Placed',
'timestamp' => current_time('mysql')
];
$orders[] = $new_order;
update_option('ads_orders', $orders);
echo '<div class="updated"><p>Order placed successfully!</p></div>';
}
}
}
// End of form handling
// Load current data
$suppliers = get_option('ads_suppliers', []);
$products = get_option('ads_products', []);
$orders = get_option('ads_orders', []);
?>
<div class="wrap">
<h1>Dropshipping System</h1>
<h2>Add Supplier</h2>
<form method="post">
<?php wp_nonce_field('ads_add_supplier_action', 'ads_add_supplier_nonce'); ?>
<input type="hidden" name="ads_action" value="add_supplier" />
<table class="form-table">
<tr><th><label for="supplier_name">Supplier Name</label></th>
<td><input name="supplier_name" type="text" id="supplier_name" class="regular-text" required></td>
</tr>
<tr><th><label for="supplier_api">Supplier API URL</label></th>
<td><input name="supplier_api" type="url" id="supplier_api" class="regular-text" required></td>
</tr>
<tr><th><label for="supplier_key">Supplier API Key</label></th>
<td><input name="supplier_key" type="text" id="supplier_key" class="regular-text"></td>
</tr>
</table>
<?php submit_button('Add Supplier'); ?>
</form>
<hr>
<h2>Add Product</h2>
<form method="post">
<?php wp_nonce_field('ads_add_product_action', 'ads_add_product_nonce'); ?>
<input type="hidden" name="ads_action" value="add_product" />
<table class="form-table">
<tr><th><label for="product_name">Product Name</label></th>
<td><input name="product_name" type="text" id="product_name" class="regular-text" required></td>
</tr>
<tr><th><label for="product_supplier">Supplier</label></th>
<td>
<select name="product_supplier" id="product_supplier" required>
<?php foreach($suppliers as $s): ?>
<option value="<?php echo esc_attr($s['id']); ?>"><?php echo esc_html($s['name']); ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<tr><th><label for="product_price">Price</label></th>
<td><input name="product_price" type="number" id="product_price" step="0.01" min="0" required></td>
</tr>
<tr><th><label for="product_stock">Stock</label></th>
<td><input name="product_stock" type="number" id="product_stock" min="0" required></td>
</tr>
</table>
<?php submit_button('Add Product'); ?>
</form>
<hr>
<h2>Place Order</h2>
<form method="post">
<?php wp_nonce_field('ads_place_order_action', 'ads_place_order_nonce'); ?>
<input type="hidden" name="ads_action" value="place_order" />
<table class="form-table">
<tr><th><label for="order_product">Product</label></th>
<td>
<select name="order_product" id="order_product" required>
<?php foreach($products as $p): ?>
<option value="<?php echo esc_attr($p['id']); ?>"><?php echo esc_html($p['name']) . ' (Stock: ' . intval($p['stock']) . ')'; ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<tr><th><label for="order_quantity">Quantity</label></th>
<td><input name="order_quantity" type="number" id="order_quantity" min="1" required></td>
</tr>
</table>
<?php submit_button('Place Order'); ?>
</form>
<hr>
<h2>Current Products</h2>
<?php if(empty($products)): ?>
<p>No products available.</p>
<?php else: ?>
<table class="widefat">
<thead><tr><th>Name</th><th>Supplier</th><th>Price</th><th>Stock</th></tr></thead>
<tbody>
<?php foreach($products as $p): ?>
<tr>
<td><?php echo esc_html($p['name']); ?></td>
<td><?php echo esc_html($p['supplier_name']); ?></td>
<td><?php echo number_format($p['price'], 2); ?></td>
<td><?php echo intval($p['stock']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<hr>
<h2>Orders</h2>
<?php if(empty($orders)): ?>
<p>No orders placed yet.</p>
<?php else: ?>
<table class="widefat">
<thead><tr><th>Order ID</th><th>Product</th><th>Quantity</th><th>Status</th><th>Date</th></tr></thead>
<tbody>
<?php foreach($orders as $o): ?>
<tr>
<td><?php echo esc_html($o['id']); ?></td>
<td><?php echo esc_html($o['product_name']); ?></td>
<td><?php echo intval($o['quantity']); ?></td>
<td><?php echo esc_html($o['status']); ?></td>
<td><?php echo esc_html($o['timestamp']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
<style>
/* Make admin styling slightly nicer */
.wrap h1 { margin-bottom: 1.5em; }
.form-table th { width: 150px; }
.widefat { width: 100%; margin-top: 1em; border-collapse: collapse;}
.widefat th, .widefat td { border: 1px solid #ddd; padding: 8px; text-align: left;}
.widefat th { background: #f4f4f4; }
</style>
<?php
}