درج کد رهگیری پستی در بخش سفارشات کاربر و مدیر
با استفاده از این کد، امکان ثبت کد رهگیری مرسوله پستی برای هر سفارش در پنل مدیریت فراهم میشود و کد ثبتشده در صفحه سفارش کاربر نیز نمایش داده خواهد شد.
کاربر میتواند کد رهگیری را کپی کند و با استفاده از دکمه «رهگیری در سایت پست» مستقیماً وضعیت مرسوله خود را پیگیری کند.
<?php
add_action('add_meta_boxes', 'myt_register_tracking_box');
function myt_register_tracking_box() {
$screen = 'shop_order';
if (function_exists('wc_get_page_screen_id')) {
$screen = wc_get_page_screen_id('shop-order');
}
add_meta_box(
'myt_tracking_mb',
'اطلاعات ارسال پستی',
'myt_render_box_content',
$screen,
'side',
'high'
);
}
function myt_render_box_content($post_or_order_object) {
$order = ($post_or_order_object instanceof WC_Order) ? $post_or_order_object : wc_get_order($post_or_order_object->ID);
if (!$order) return;
wp_nonce_field('myt_save_action', 'myt_nonce');
$val = $order->get_meta('_myt_tracking_id', true);
?>
<div class="myt-field-group">
<label for="myt_tracking_id"><strong>کد رهگیری:</strong></label>
<input type="text" id="myt_tracking_id" name="myt_tracking_id"
value="<?php echo esc_attr($val); ?>"
style="width:100%; margin-top:5px;" placeholder="کد را وارد کنید..." />
<p style="color:#666; font-size:12px; margin-top:5px;">سپس دکمه بروزرسانی سفارش را بزنید.</p>
</div>
<?php
}
add_action('woocommerce_process_shop_order_meta', 'myt_save_tracking_data');
function myt_save_tracking_data($order_id) {
if (!isset($_POST['myt_nonce']) || !wp_verify_nonce($_POST['myt_nonce'], 'myt_save_action')) {
return;
}
$order = wc_get_order($order_id);
if (!$order) return;
if (isset($_POST['myt_tracking_id'])) {
$code = sanitize_text_field($_POST['myt_tracking_id']);
if (!empty($code)) {
$order->update_meta_data('_myt_tracking_id', $code);
} else {
$order->delete_meta_data('_myt_tracking_id');
}
$order->save();
}
}
add_action('woocommerce_order_details_before_order_table', 'myt_show_tracking_info');
function myt_show_tracking_info($order) {
if (is_numeric($order)) {
$order = wc_get_order($order);
}
if (!$order instanceof WC_Order) return;
$tracking_code = $order->get_meta('_myt_tracking_id', true);
if (empty($tracking_code)) return;
$post_url = 'https://tracking.post.ir/search.aspx?id=' . rawurlencode($tracking_code);
?>
<div class="myt-card-wrapper">
<div class="myt-card-header">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="myt-icon-truck"><rect x="1" y="3" width="15" height="13"></rect><polygon points="16 8 20 8 23 11 23 16 16 16 16 8"></polygon><circle cx="5.5" cy="18.5" r="2.5"></circle><circle cx="18.5" cy="18.5" r="2.5"></circle></svg>
<h3 class="myt-title">وضعیت مرسوله پستی</h3>
</div>
<div class="myt-card-body">
<p class="myt-desc">سفارش شما تحویل اداره پست شده است. کد رهگیری در زیر قابل مشاهده است:</p>
<div class="myt-actions-row">
<!-- دکمه کپی کد -->
<div class="myt-code-container">
<span class="myt-label">کد رهگیری:</span>
<span class="myt-code-number"><?php echo esc_html($tracking_code); ?></span>
<button type="button" class="myt-btn-copy" data-code="<?php echo esc_attr($tracking_code); ?>" aria-label="کپی کد رهگیری">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg>
<span class="myt-tooltip">کپی شد!</span>
</button>
</div>
<!-- دکمه لینک پست -->
<a href="<?php echo esc_url($post_url); ?>" target="_blank" class="myt-btn-track">
رهگیری در سایت پست
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="margin-right:5px;"><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"></path><polyline points="15 3 21 3 21 9"></polyline><line x1="10" y1="14" x2="21" y2="3"></line></svg>
</a>
</div>
</div>
</div>
<?php
}
add_action('wp_footer', 'myt_footer_scripts');
function myt_footer_scripts() {
if (!is_wc_endpoint_url('view-order') && !is_wc_endpoint_url('order-received')) {
return;
}
?>
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cstyle%3E%0A%20%20%20%20%20%20%20%20.myt-card-wrapper%20%7B%20background-color%3A%20%23fff%3B%20border%3A%201px%20solid%20%23e2e8f0%3B%20border-radius%3A%2012px%3B%20box-shadow%3A%200%204px%206px%20-1px%20rgba(0%2C%200%2C%200%2C%200.05)%2C%200%202px%204px%20-1px%20rgba(0%2C%200%2C%200%2C%200.03)%3B%20margin%3A%2030px%200%3B%20overflow%3A%20hidden%3B%20font-family%3A%20%22Vazirmatn%22%2C%20%22Vazir%22%2C%20Tahoma%2C%20sans-serif%20!important%3B%20direction%3A%20rtl%3B%20%7D%0A%20%20%20%20%20%20%20%20.myt-card-header%20%7B%20background-color%3A%20%23f8fafc%3B%20padding%3A%2015px%2020px%3B%20border-bottom%3A%201px%20solid%20%23e2e8f0%3B%20display%3A%20flex%3B%20align-items%3A%20center%3B%20gap%3A%2010px%3B%20%7D%0A%20%20%20%20%20%20%20%20.myt-icon-truck%20%7B%20color%3A%20%233b82f6%3B%20%7D%0A%20%20%20%20%20%20%20%20.myt-title%20%7B%20margin%3A%200%20!important%3B%20font-size%3A%2016px%20!important%3B%20font-weight%3A%20700%20!important%3B%20color%3A%20%231e293b%20!important%3B%20font-family%3A%20%22Vazirmatn%22%2C%20%22Vazir%22%2C%20sans-serif%20!important%3B%20%7D%0A%20%20%20%20%20%20%20%20.myt-card-body%20%7B%20padding%3A%2020px%3B%20%7D%0A%20%20%20%20%20%20%20%20.myt-desc%20%7B%20margin%3A%200%200%2020px%200%20!important%3B%20color%3A%20%2364748b%3B%20font-size%3A%2014px%20!important%3B%20line-height%3A%201.6%3B%20font-family%3A%20%22Vazirmatn%22%2C%20%22Vazir%22%2C%20sans-serif%20!important%3B%20%7D%0A%20%20%20%20%20%20%20%20.myt-actions-row%20%7B%20display%3A%20flex%3B%20flex-wrap%3A%20wrap%3B%20gap%3A%2015px%3B%20align-items%3A%20center%3B%20justify-content%3A%20space-between%3B%20%7D%0A%20%20%20%20%20%20%20%20.myt-code-container%20%7B%20display%3A%20flex%3B%20align-items%3A%20center%3B%20background%3A%20%23f1f5f9%3B%20padding%3A%208px%2012px%3B%20border-radius%3A%208px%3B%20%20border%3A%201px%20dashed%20%23cbd5e1%3B%20flex-grow%3A%201%3B%20min-width%3A%20250px%3B%20position%3A%20relative%3B%20%7D%0A%20%20%20%20%20%20%20%20.myt-label%20%7B%20font-weight%3A%20500%3B%20color%3A%20%23475569%3B%20font-size%3A%2013px%3B%20margin-left%3A%208px%3B%20%7D%0A%20%20%20%20%20%20%20%20.myt-code-number%20%7B%20font-family%3A%20%22Vazirmatn%22%2C%20monospace%20!important%3B%20font-weight%3A%20700%3B%20font-size%3A%2016px%3B%20color%3A%20%230f172a%3B%20letter-spacing%3A%201px%3B%20flex-grow%3A%201%3B%20text-align%3A%20center%3B%20%7D%0A%20%20%20%20%20%20%20%20.myt-btn-copy%20%7B%20background%3A%20transparent%3B%20border%3A%20none%3B%20cursor%3A%20pointer%3B%20color%3A%20%2364748b%3B%20padding%3A%205px%3B%20display%3A%20flex%3B%20align-items%3A%20center%3B%20justify-content%3A%20center%3B%20transition%3A%20all%200.2s%3B%20border-radius%3A%204px%3B%20%7D%0A%20%20%20%20%20%20%20%20.myt-btn-copy%3Ahover%20%7B%20color%3A%20%233b82f6%3B%20background-color%3A%20rgba(59%2C%20130%2C%20246%2C%200.1)%3B%20%7D%0A%20%20%20%20%20%20%20%20.myt-btn-track%20%7B%20display%3A%20inline-flex%3B%20align-items%3A%20center%3B%20justify-content%3A%20center%3B%20background-color%3A%20%233b82f6%3B%20color%3A%20%23fff%20!important%3B%20padding%3A%2010px%2020px%3B%20border-radius%3A%208px%3B%20font-size%3A%2014px%20!important%3B%20font-weight%3A%20500%3B%20text-decoration%3A%20none%20!important%3B%20transition%3A%20background%200.3s%3B%20white-space%3A%20nowrap%3B%20font-family%3A%20%22Vazirmatn%22%2C%20%22Vazir%22%2C%20sans-serif%20!important%3B%20border%3A%201px%20solid%20%233b82f6%3B%20%7D%0A%20%20%20%20%20%20%20%20.myt-btn-track%3Ahover%20%7B%20background-color%3A%20%232563eb%3B%20%7D%0A%20%20%20%20%20%20%20%20.myt-tooltip%20%7B%20position%3A%20absolute%3B%20background-color%3A%20%231e293b%3B%20color%3A%20%23fff%3B%20padding%3A%204px%208px%3B%20border-radius%3A%204px%3B%20font-size%3A%2011px%3B%20top%3A%20-30px%3B%20left%3A%2050%25%3B%20transform%3A%20translateX(-50%25)%3B%20opacity%3A%200%3B%20visibility%3A%20hidden%3B%20transition%3A%20opacity%200.2s%3B%20pointer-events%3A%20none%3B%20white-space%3A%20nowrap%3B%20%7D%0A%20%20%20%20%20%20%20%20.myt-tooltip%3A%3Aafter%20%7B%20content%3A%20''%3B%20position%3A%20absolute%3B%20top%3A%20100%25%3B%20left%3A%2050%25%3B%20margin-left%3A%20-4px%3B%20border-width%3A%204px%3B%20border-style%3A%20solid%3B%20border-color%3A%20%231e293b%20transparent%20transparent%20transparent%3B%20%7D%20.myt-btn-copy.copied%20.myt-tooltip%20%7B%20opacity%3A%201%3B%20visibility%3A%20visible%3B%20top%3A%20-35px%3B%20%7D%0A%20%20%20%20%20%20%20%20%40media%20(max-width%3A%20600px)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20.myt-actions-row%20%7B%20flex-direction%3A%20column%3B%20align-items%3A%20stretch%3B%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20.myt-code-container%20%7B%20width%3A%20100%25%3B%20justify-content%3A%20space-between%3B%20box-sizing%3A%20border-box%3B%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20.myt-btn-track%20%7B%20width%3A%20100%25%3B%20box-sizing%3A%20border-box%3B%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20.myt-code-number%20%7B%20text-align%3A%20right%3B%20font-size%3A%2014px%3B%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%3C%2Fstyle%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object mce-object-style" width="20" height="20" alt="<style>" />
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%3E%0A%20%20%20%20document.addEventListener('DOMContentLoaded'%2C%20function()%20%7B%0A%20%20%20%20%20%20%20%20const%20copyBtns%20%3D%20document.querySelectorAll('.myt-btn-copy')%3B%0A%20%20%20%20%20%20%20%20copyBtns.forEach(btn%20%3D%3E%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20btn.addEventListener('click'%2C%20function(e)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20e.preventDefault()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20const%20code%20%3D%20this.getAttribute('data-code')%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(navigator.clipboard%20%26%26%20navigator.clipboard.writeText)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20navigator.clipboard.writeText(code).then(()%20%3D%3E%20showTooltip(this))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20const%20textArea%20%3D%20document.createElement(%22textarea%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20textArea.value%20%3D%20code%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20document.body.appendChild(textArea)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20textArea.select()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20try%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20document.execCommand('copy')%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20showTooltip(this)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%20catch%20(err)%20%7B%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20document.body.removeChild(textArea)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%20%20%20%20function%20showTooltip(btn)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20btn.classList.add('copied')%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20setTimeout(()%20%3D%3E%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20btn.classList.remove('copied')%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%202000)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D)%3B%0A%20%20%20%20%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object mce-object-script" width="20" height="20" alt="<script>" />
<?php
}