| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Admin\Order; |
| 4 |
|
| 5 |
defined("ABSPATH") || exit; |
| 6 |
|
| 7 |
class OrderMetaBox |
| 8 |
{ |
| 9 |
public function registerMetaBox() |
| 10 |
{ |
| 11 |
if (!current_user_can("manage_woocommerce")) return; |
| 12 |
|
| 13 |
// HPOS uses 'id', traditional CPT uses 'post' |
| 14 |
$orderId = isset($_GET["id"]) ? absint($_GET["id"]) : 0; |
| 15 |
if (!$orderId) $orderId = isset($_GET["post"]) ? absint($_GET["post"]) : 0; |
| 16 |
if (!$orderId) return; |
| 17 |
|
| 18 |
$order = wc_get_order($orderId); |
| 19 |
if (!$order) return; |
| 20 |
|
| 21 |
$allowedStatuses = [ |
| 22 |
"bslm-rejected", |
| 23 |
"bslm-preparation", |
| 24 |
"bslm-wait-vendor", |
| 25 |
"bslm-shipping", |
| 26 |
"bslm-completed", |
| 27 |
]; |
| 28 |
|
| 29 |
if (!in_array($order->get_status(), $allowedStatuses, true)) return; |
| 30 |
|
| 31 |
add_meta_box( |
| 32 |
"basalam_order_settings", |
| 33 |
"تنظی� |
| 34 |
ات باسلا� |
| 35 |
", |
| 36 |
[$this, "renderMetaBox"], |
| 37 |
["woocommerce_page_wc-orders", "shop_order"], |
| 38 |
"side", |
| 39 |
"high" |
| 40 |
); |
| 41 |
|
| 42 |
add_meta_box( |
| 43 |
'basalam_order_info', |
| 44 |
'اطلاعات سفارش باسلا� |
| 45 |
', |
| 46 |
[$this, 'renderBasalamOrderMetaBox'], |
| 47 |
["woocommerce_page_wc-orders", "shop_order"], |
| 48 |
"side", |
| 49 |
"high" |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
public function renderMetaBox($post_or_order) |
| 54 |
{ |
| 55 |
// HPOS passes WC_Order object, traditional CPT passes WP_Post object |
| 56 |
if (is_object($post_or_order) && method_exists($post_or_order, 'get_id')) { |
| 57 |
$orderId = $post_or_order->get_id(); |
| 58 |
} else $orderId = $post_or_order->ID; |
| 59 |
|
| 60 |
$order = wc_get_order($orderId); |
| 61 |
if (!$order) return; |
| 62 |
|
| 63 |
$orderStatus = $order->get_status(); |
| 64 |
|
| 65 |
require_once plugin_dir_path(__FILE__) . "OrderTrackingBox.php"; |
| 66 |
} |
| 67 |
|
| 68 |
public function renderBasalamOrderMetaBox($post_or_order) |
| 69 |
{ |
| 70 |
// HPOS passes WC_Order object, traditional CPT passes WP_Post object |
| 71 |
if (is_object($post_or_order) && method_exists($post_or_order, 'get_id')) { |
| 72 |
$orderId = $post_or_order->get_id(); |
| 73 |
} else $orderId = $post_or_order->ID; |
| 74 |
|
| 75 |
$order = wc_get_order($orderId); |
| 76 |
if (!$order) { |
| 77 |
echo '<p class="basalam-p">سفارش یافت نشد</p>'; |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
global $wpdb; |
| 82 |
$table_name = $wpdb->prefix . 'sync_basalam_payments'; |
| 83 |
|
| 84 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Custom plugin table; identifier from $wpdb->prefix, not user input. |
| 85 |
$invoice_id = $wpdb->get_var( |
| 86 |
$wpdb->prepare( |
| 87 |
"SELECT invoice_id FROM {$table_name} WHERE order_id = %d", |
| 88 |
$orderId |
| 89 |
) |
| 90 |
); |
| 91 |
|
| 92 |
if (!$invoice_id) { |
| 93 |
echo '<p class="basalam-p">اطلاعات سفارش باسلا� |
| 94 |
یافت نشد</p>'; |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
$fee_amount = $order->get_meta('_basalam_fee_amount', true); |
| 99 |
$balance_amount = $order->get_meta('_basalam_balance_amount', true); |
| 100 |
$purchase_count = $order->get_meta('_basalam_purchase_count', true); |
| 101 |
$hash_id = $order->get_meta('_sync_basalam_hash_id', true); |
| 102 |
|
| 103 |
$fee_formatted = $this->formatBasalamCurrency($fee_amount); |
| 104 |
$balance_formatted = $this->formatBasalamCurrency($balance_amount); |
| 105 |
|
| 106 |
require_once syncBasalamPlugin()->templatePath('orders/OrderMetaBox.php'); |
| 107 |
} |
| 108 |
|
| 109 |
private function formatBasalamCurrency($amount) |
| 110 |
{ |
| 111 |
if (empty($amount) || $amount == 0) return '0 تو� |
| 112 |
ان'; |
| 113 |
|
| 114 |
return number_format($amount) . ' تو� |
| 115 |
ان'; |
| 116 |
} |
| 117 |
} |
| 118 |
|