...

کد رهگیری سفارشات ووکامرس

طبق آموزش ریلز اینستلگرام
افزونه Code snippets رو نصب کنید و کد بالا رو داخلش قرار بدید

از بخش سفارشات ووکامرس میتونید برای هر سفارش کد رهگیری ثبت کنید

و به راحتی داخل پنل کاربری مشتری قرار میگیره

// 1. ثبت باکس متا (با پشتیبانی از سیستم جدید ووکامرس) 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'     ); }  // 2. نمایش فیلد ورودی در پنل ادمین 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 }  // 3. ذخیره‌سازی دیتا (سازگار با HPOS) 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();     } }  // 4. نمایش در صفحه جزییات سفارش (طراحی جدید) add_action('woocommerce_order_details_after_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 }  // 5. استایل‌ها و اسکریپت‌ها (دیزاین مدرن + فونت وزیر) 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;     }     ?>     <style>         /* Container Style */         .myt-card-wrapper {             background-color: #fff;             border: 1px solid #e2e8f0;             border-radius: 12px;             box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05), 0 2px 4px -1px rgba(0, 0, 0, 0.03);             margin: 30px 0;             overflow: hidden;             font-family: "yekanbakh", "Vazir", Tahoma, sans-serif !important;             direction: rtl;         }          /* Header */         .myt-card-header {             background-color: #f8fafc;             padding: 15px 20px;             border-bottom: 1px solid #e2e8f0;             display: flex;             align-items: center;             gap: 10px;         }         .myt-icon-truck {             color: #FB6F92; /* Blue Color */         }         .myt-title {             margin: 0 !important;             font-size: 16px !important;             font-weight: 700 !important;             color: #1e293b !important;             font-family: "yekanbakh", "Vazir", sans-serif !important;         }          /* Body */         .myt-card-body {             padding: 20px;         }         .myt-desc {             margin: 0 0 20px 0 !important;             color: #64748b;             font-size: 14px !important;             line-height: 1.6;             font-family: "yekanbakh", "Vazir", sans-serif !important;         }          /* Actions Area */         .myt-actions-row {             display: flex;             flex-wrap: wrap;             gap: 15px;             align-items: center;             justify-content: space-between;         }          /* Code Box */         .myt-code-container {             display: flex;             align-items: center;             background: #f1f5f9;             padding: 8px 12px;             border-radius: 8px;             border: 1px dashed #cbd5e1;             flex-grow: 1;             min-width: 250px;             position: relative;         }         .myt-label {             font-weight: 500;             color: #475569;             font-size: 13px;             margin-left: 8px;         }         .myt-code-number {             font-family: "yekanbakh", monospace !important;             font-weight: 700;             font-size: 16px;             color: #0f172a;             letter-spacing: 1px;             flex-grow: 1;             text-align: center;         }                  /* Copy Button */         .myt-btn-copy {             background: transparent;             border: none;             cursor: pointer;             color: #64748b;             padding: 5px;             display: flex;             align-items: center;             justify-content: center;             transition: all 0.2s;             border-radius: 4px;         }         .myt-btn-copy:hover {             color: #3b82f6;             background-color: rgba(59, 130, 246, 0.1);         }          /* Track Button */         .myt-btn-track {             display: inline-flex;             align-items: center;             justify-content: center;             background-color: #FB6F92;             color: #fff !important;             padding: 10px 20px;             border-radius: 8px;             font-size: 14px !important;             font-weight: 500;             text-decoration: none !important;             transition: background 0.3s;             white-space: nowrap;             font-family: "yekanbakh", "Vazir", sans-serif !important;             border: 1px solid #FB6F92;         }         .myt-btn-track:hover {             background-color: #FB6F92;         }          /* Tooltip */         .myt-tooltip {             position: absolute;             background-color: #1e293b;             color: #fff;             padding: 4px 8px;             border-radius: 4px;             font-size: 11px;             top: -30px;             left: 50%;             transform: translateX(-50%);             opacity: 0;             visibility: hidden;             transition: opacity 0.2s;             pointer-events: none;             white-space: nowrap;         }         .myt-tooltip::after {             content: '';             position: absolute;             top: 100%;             left: 50%;             margin-left: -4px;             border-width: 4px;             border-style: solid;             border-color: #1e293b transparent transparent transparent;         }         .myt-btn-copy.copied .myt-tooltip {             opacity: 1;             visibility: visible;             top: -35px;         }          /* Responsive Design */         @media (max-width: 600px) {             .myt-actions-row {                 flex-direction: column;                 align-items: stretch;             }             .myt-code-container {                 width: 100%;                 justify-content: space-between;                 box-sizing: border-box;             }             .myt-btn-track {                 width: 100%;                 box-sizing: border-box;             }             .myt-code-number {                 text-align: right;                 font-size: 14px;             }         }     </style>     <script>     document.addEventListener('DOMContentLoaded', function() {         // Find all copy buttons         const copyBtns = document.querySelectorAll('.myt-btn-copy');                  copyBtns.forEach(btn => {             btn.addEventListener('click', function(e) {                 e.preventDefault();                 const code = this.getAttribute('data-code');                                  // Copy logic                 if (navigator.clipboard && navigator.clipboard.writeText) {                     navigator.clipboard.writeText(code).then(() => showTooltip(this));                 } else {                     // Fallback for older browsers                     const textArea = document.createElement("textarea");                     textArea.value = code;                     document.body.appendChild(textArea);                     textArea.select();                     try {                         document.execCommand('copy');                         showTooltip(this);                     } catch (err) {}                     document.body.removeChild(textArea);                 }             });         });          function showTooltip(btn) {             btn.classList.add('copied');             setTimeout(() => {                 btn.classList.remove('copied');             }, 2000);         }     });     </script>     <?php }
مشاهده بیشتر