ساخت فاکتور در ووکامرس

ایجاد فاکتور چاپی اختصاصی برای هر فروشنده در ووکامرس، با نمایش اطلاعات فروشنده و خریدار، جزئیات محصولات، مبالغ سفارش و پشتیبانی از فروشندگان YITH Multi Vendor.

فاکتور فروشنده ووکامرس

این کد یک فاکتور فروش اختصاصی برای سفارش‌های ووکامرس ایجاد می‌کند و امکان چاپ آن را از صفحه مدیریت سفارش فراهم می‌سازد.

مهم‌ترین امکانات این سیستم:

اضافه شدن دکمه «چاپ فاکتور» در صفحه مدیریت سفارش.
نمایش اطلاعات فروشنده شامل نام، تلفن، آدرس، کدپستی و وب‌سایت.
دریافت خودکار اطلاعات فروشنده از محصول سفارش داده‌شده.
پشتیبانی از فروشندگان YITH Multi Vendor.
نمایش اطلاعات خریدار، شماره تماس، آدرس و کدپستی.
نمایش لیست محصولات همراه با تصویر، شناسه کالا، نام، وزن، قیمت، تخفیف و تعداد.
نمایش مبلغ کل، تخفیف، هزینه حمل‌ونقل و مبلغ نهایی پرداخت.
تبدیل اعداد و مبالغ فاکتور به اعداد فارسی.
نمایش فاکتور در قالب A5 افقی و مناسب برای چاپ.
نمایش یادداشت مشتری و توضیحات مربوط به مسئولیت فروشنده.
درج بخش مخصوص مهر و امضای فروشنده و مشتری.
استفاده از فونت iranyekan برای نمایش بهتر متن فارسی.

با کلیک روی دکمه چاپ فاکتور، فاکتور در یک صفحه جدید باز شده و پنجره چاپ مرورگر به‌صورت خودکار اجرا می‌شود.


<?php
// ========== تابع تبدیل اعداد به فارسی ==========
function farsi_number($input) {
    $persian = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
    $english = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
    return str_replace($english, $persian, (string)$input);
}

add_action('woocommerce_admin_order_data_after_order_details','add_invoice_button_near_label');
function add_invoice_button_near_label($order){
    if(!$order) return;
    $order_id = $order->get_id();
    $url = admin_url('admin-ajax.php?action=print_invoice_admin&order_id='.$order_id);
    echo '<div style="position:absolute; left:140px; top:20px;">
        <a href="'.$url.'" target="_blank" style=" background:#46b450; color:#fff; padding:10px 15px; border-radius:5px; text-decoration:none; ">
        چاپ فاکتور
        </a>
    </div>';
}
function invoice_price_fa($price){
    $formatted = number_format((float)$price, 0, '.', ',');
    return farsi_number($formatted);
}
function get_farsi_state_name($country_code, $state_code){
    $states = WC()->countries->get_states($country_code);
    return isset($states[$state_code]) ? $states[$state_code] : $state_code;
}
function my_get_order_vendor_data($order){
    $vendor_id = 0;
    $items = $order->get_items();
    foreach($items as $item){
        $product_id = $item->get_product_id();
        $product = wc_get_product($product_id);
        if($product){
            $vendor_id = $product->get_post_data()->post_author;
            if(!empty($vendor_id)){
                break;
            }
        }
    }

    if($vendor_id){
        $sender_name = get_user_meta($vendor_id, 'first_name', true) . ' ' . get_user_meta($vendor_id, 'last_name', true);
        $sender_phone = get_user_meta($vendor_id, 'billing_phone', true);
        $sender_address = get_user_meta($vendor_id, 'billing_address_1', true);
        $sender_city = get_user_meta($vendor_id, 'billing_city', true);
        $sender_state_code = get_user_meta($vendor_id, 'billing_state', true);
        $sender_postcode = get_user_meta($vendor_id, 'billing_postcode', true);
        $sender_email = get_the_author_meta('user_email', $vendor_id);
        $sender_website = str_replace(['http://','https://'], '', get_the_author_meta('user_url', $vendor_id));
    } else {
        // fallback به مدیرکل
        $admins = get_users(['role'=>'administrator','number'=>1]);
        if(!empty($admins)){
            $admin = $admins[0];
            $admin_id = $admin->ID;
            $sender_name = $admin->display_name;
            $sender_phone = get_user_meta($admin_id,'billing_phone',true);
            $sender_address = get_user_meta($admin_id,'billing_address_1',true);
            $sender_city = get_user_meta($admin_id,'billing_city',true);
            $sender_state_code = get_user_meta($admin_id,'billing_state',true);
            $sender_postcode = get_user_meta($admin_id,'billing_postcode',true);
            $sender_email = $admin->user_email;
            $sender_website = str_replace(['http://','https://'], '', get_bloginfo('url'));
        } else {
            return [
                'user_id'    => 0,
                'name'       => get_bloginfo('name'),
                'phone'      => get_option('woocommerce_store_phone', ''),
                'address'    => get_option('woocommerce_store_address', ''),
                'city'       => '',
                'state'      => '',
                'postcode'   => get_option('woocommerce_store_postcode', ''),
                'email'      => get_option('admin_email'),
                'website'    => str_replace(['http://','https://'], '', get_bloginfo('url')),
            ];
        }
    }

    $sender_states = WC()->countries->get_states('IR');
    $sender_state = isset($sender_states[$sender_state_code]) ? $sender_states[$sender_state_code] : $sender_state_code;

    return [
        'user_id'    => $vendor_id,
        'name'       => $sender_name,
        'phone'      => $sender_phone,
        'address'    => $sender_address,
        'city'       => $sender_city,
        'state'      => $sender_state,
        'postcode'   => $sender_postcode,
        'email'      => $sender_email,
        'website'    => $sender_website,
    ];
}

add_action('wp_ajax_print_invoice_admin','generate_invoice_admin');
function generate_invoice_admin(){
    $order_id = isset($_GET['order_id']) ? intval($_GET['order_id']) : 0;
    $order = wc_get_order($order_id);
    if(!$order) die('سفارش یافت نشد.');
    $site_title = get_bloginfo('name');
    $site_url = str_replace(['http://','https://'], '', get_bloginfo('url'));
    $logo = get_site_icon_url();
    $site_description = get_bloginfo('description');
    $vendor = my_get_order_vendor_data($order);
    $sender_name = $vendor['name'];
    $sender_phone = $vendor['phone'];
    $sender_address = $vendor['address'];
    $sender_city = $vendor['city'];
    $sender_state = $vendor['state'];
    $sender_postcode = $vendor['postcode'];
    $country = $order->get_shipping_country() ?: $order->get_billing_country();
    $states = WC()->countries->get_states($country);
    $customer_name = trim($order->get_shipping_first_name().' '.$order->get_shipping_last_name());
    if(!$customer_name) $customer_name = trim($order->get_billing_first_name().' '.$order->get_billing_last_name());
    
    // دریافت شماره موبایل خریدار از user meta
    $customer_phone = $order->get_billing_phone();
    if(!$customer_phone){
        $customer_id = $order->get_customer_id();
        if($customer_id){
            $customer_phone = get_user_meta($customer_id, 'billing_phone', true);
            if(!$customer_phone){
                $customer_phone = get_user_meta($customer_id, 'phone', true);
            }
        }
    }
    
    $customer_address = trim($order->get_shipping_address_1().' '.$order->get_shipping_address_2());
    if(!$customer_address) $customer_address = trim($order->get_billing_address_1().' '.$order->get_billing_address_2());
    $customer_city = $order->get_shipping_city() ?: $order->get_billing_city();
    $customer_state_code = $order->get_shipping_state() ?: $order->get_billing_state();
    $customer_state = isset($states[$customer_state_code]) ? $states[$customer_state_code] : $customer_state_code;
    $customer_postcode = $order->get_shipping_postcode() ?: $order->get_billing_postcode();
    $subtotal = $order->get_subtotal();
    $shipping = $order->get_shipping_total();
    $discount = $order->get_discount_total();
    $total = $order->get_total();
    ?>
    <!DOCTYPE html>
    <html lang="fa" dir="rtl">
    <head>
        <meta charset="UTF-8">
        <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cstyle%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%40font-face%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20font-family%3A%20'iranyekan'%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20src%3A%20url(%22%2Fwp-content%2Fthemes%2Fhello-child%2Ffont%2Firanyekanwebregular.woff%22)%20format(%22woff%22)%3B%0A%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%20body%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20font-family%3A'iranyekan'%2CTahoma%2CArial%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20font-size%3A11px%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20color%3A%23333%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20margin%3A0%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20padding%3A0%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20background-color%3A%23eee%3B%0A%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.invoice-box%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20width%3A210mm%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20height%3A148mm%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20margin%3A10px%20auto%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20padding%3A10px%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20background%3A%23fff%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20border%3A1px%20solid%20%23ccc%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20box-sizing%3Aborder-box%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20position%3Arelative%3B%0A%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%40media%20print%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%40page%7Bsize%3AA5%20landscape%3B%20margin%3A0%3B%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20body%7Bbackground%3A%23fff%3B%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.invoice-box%7Bborder%3Anone%3B%20margin%3A0%3B%20width%3A100%25%3B%20height%3A100%25%3B%7D%0A%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.header-table%7Bwidth%3A100%25%3B%20border-bottom%3A2px%20solid%20%2346b450%3B%20margin-bottom%3A5px%3B%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20.header-table%20td%7Bvertical-align%3Atop%3B%20padding%3A5px%3B%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20.store-info-right%7Btext-align%3Aright%3B%20width%3A35%25%3B%20line-height%3A1.7%3B%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20.logo-area%7Btext-align%3Acenter%3B%20width%3A20%25%3B%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20.order-meta-left%7Btext-align%3Aleft%3B%20width%3A35%25%3B%20font-size%3A10px%3B%20line-height%3A1.7%3B%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20.info-section%7Bbackground%3A%23f2f9f2%3B%20border%3A1px%20solid%20%23d0e6d0%3B%20border-radius%3A5px%3B%20margin-bottom%3A8px%3B%20padding%3A8px%3B%20line-height%3A1.8%3B%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20.items-table%7Bwidth%3A100%25%3B%20border-collapse%3Acollapse%3B%20margin-bottom%3A10px%3B%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20.items-table%20th%7Bbackground%3A%23e8f3e8%3B%20border%3A1px%20solid%20%23c8dbc8%3B%20padding%3A5px%3B%20font-size%3A10px%3B%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20.items-table%20td%7Bborder%3A1px%20solid%20%23c8dbc8%3B%20padding%3A4px%3B%20text-align%3Acenter%3B%20font-size%3A10px%3B%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20.bottom-section%7Bdisplay%3Aflex%3B%20justify-content%3Aspace-between%3B%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20.totals-table%7Bwidth%3A38%25%3B%20border-collapse%3Acollapse%3B%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20.totals-table%20td%7Bborder%3A1px%20solid%20%23ddd%3B%20padding%3A5px%3B%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20.final-price%7Bbackground%3A%23eef7ee%20!important%3B%20font-weight%3Abold%3B%20font-size%3A12px%3B%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20.footer-sigs%7Bdisplay%3Aflex%3B%20justify-content%3Aspace-around%3B%20margin-top%3A20px%3B%20font-weight%3Abold%3B%7D%0A%20%20%20%20%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="&lt;style&gt;" />
    </head>
    <body>
    <div class="invoice-box">
        <table class="header-table">
            <tr>
                <td class="store-info-right">
                    <strong>فروشنده: <?php echo esc_html($sender_name ?: $site_title); ?></strong><br>
                    <?php if($vendor['website']): ?>وب‌سایت: <?php echo esc_html(str_replace(['http://','https://'], '', $vendor['website'])); ?><br><?php endif; ?>
                    <?php if($sender_phone): ?>تلفن تماس: <?php echo esc_html(farsi_number($sender_phone)); ?><br><?php endif; ?>
                    وب‌سایت: 
                        <?php
                        $vendor_link = '';
                        foreach ($order->get_items() as $item) {
                            $product = $item->get_product();
                            if (!$product) { continue; }
                            $terms = get_the_terms($product->get_id(), 'yith_shop_vendor');
                            if ($terms && !is_wp_error($terms)) {
                                $vendor_term = array_shift($terms);
                                $link = get_term_link($vendor_term);
                                if (!is_wp_error($link)) {
                                    $vendor_link = $link;
                                }
                                break;
                            }
                        }
                        if (!empty($vendor_link)) {
                            echo esc_html(
                                preg_replace('#^https?://#', '', untrailingslashit($vendor_link))
                            );
                        }
                        ?>                  
                </td>
                <td class="logo-area">
                    <?php if($logo): ?><img src="<?php echo esc_url($logo); ?>" style="max-height:45px;"><br><?php endif; ?>
                    
                    <strong>
                                            <?php
                            if ( ! empty( $vendor['user_id'] ) ) {
                                echo esc_html( get_the_author_meta( 'display_name', $vendor['user_id'] ) );
                            } else {
                                echo esc_html( get_bloginfo( 'name' ) );
                            }
                        ?>
                    </strong>
                </td>
                <td class="order-meta-left">
                    تاریخ چاپ: <?php echo farsi_number(date_i18n('Y/m/d')); ?><br>
                    شناسه سفارش: <?php echo farsi_number($order_id); ?><br>
                    وضعیت: <?php echo esc_html(wc_get_order_status_name($order->get_status())); ?>
                </td>
            </tr>
        </table>
        <div class="info-section">
            <strong>خریدار:</strong> آقای/خانم <?php echo esc_html($customer_name); ?>
            <strong>تلفن:</strong> <?php echo esc_html(farsi_number($customer_phone)); ?>
            <strong>نشانی:</strong>
            استان <?php echo esc_html($customer_state); ?>،
            شهر <?php echo esc_html($customer_city); ?>،
            <?php echo esc_html($customer_address); ?>،
            کدپستی <?php echo esc_html(farsi_number($customer_postcode)); ?>
        </div>
        <table class="items-table">
            <thead>
                <tr>
                    <th>#</th>
                    <th>شناسه کالا</th>
                    <th>تصویر</th>
                    <th>نام محصول</th>
                    <th>وزن</th>
                    <th>قیمت واحد (تومان)</th>
                    <th>تخفیف</th>
                    <th>تعداد</th>
                    <th>جمع کل (تومان)</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $count = 1;
                foreach($order->get_items() as $item):
                    $product = $item->get_product();
                    $sku = $product ? $product->get_sku() : '-';
                    $weight = '';

                    if($product){
                        $weight = $product->get_attribute('pa_weight');
                        if(!$weight && $product->is_type('variation')){
                            $parent = wc_get_product($product->get_parent_id());
                            $weight = $parent ? $parent->get_attribute('pa_weight') : '';
                        }
                    }
                ?>
                <tr>
                    <td><?php echo farsi_number($count++); ?></td>
                    <td><?php echo esc_html(farsi_number($sku)); ?></td>
                    <td style="display:flex; align-items:center; justify-content:center;"><?php echo $product ? $product->get_image([30,30]) : ''; ?></td>
                    <td style="text-align:right;"><?php echo esc_html($item->get_name()); ?></td>
                    <td><?php echo $weight ? esc_html(farsi_number($weight)) : '-'; ?></td>
                    <td><?php echo invoice_price_fa($order->get_item_subtotal($item)); ?></td>
                    <td><?php echo invoice_price_fa($order->get_item_subtotal($item) - $item->get_total()); ?></td>
                    <td><?php echo farsi_number($item->get_quantity()); ?></td>
                    <td><?php echo invoice_price_fa($item->get_total()); ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        <div class="bottom-section">
            <div style="width:58%; font-size:10px;">
                <strong>آدرس فروشنده:</strong>
                استان <?php echo esc_html($sender_state); ?>،
                شهر <?php echo esc_html($sender_city); ?>،
                <?php echo esc_html($sender_address); ?>،
                کدپستی <?php echo esc_html(farsi_number($sender_postcode)); ?>
                <br>
                <strong>یادداشت مشتری:</strong> <?php echo esc_html($order->get_customer_note() ?: 'توضیحاتی ثبت نشده است.'); ?><br>
                <p style="color:#666;">
        		* تمامی مبالغ درج شده در این فاکتور به واحد پول رسمی (تومان) می‌باشد. <br>
                * تمامی مسئولیت حقوقی و مالی این فاکتور بر عهده <strong>فروشنده آقای/خانم <?php echo esc_html($sender_name ?: $site_title); ?></strong> میباشد. <br>
        		</p>
        	</div>
            <table class="totals-table">
                <tr>
                    <td style="background:#f9f9f9;">مبلغ کل اقلام</td>
                    <td><?php echo invoice_price_fa($subtotal); ?></td>
                </tr>
                <?php if($discount > 0): ?>
                <tr>
                    <td style="background:#f9f9f9;">تخفیف نهایی</td>
                    <td><?php echo invoice_price_fa($discount); ?>-</td>
                </tr>
                <?php endif; ?>
                <tr>
                    <td style="background:#f9f9f9;">هزینه حمل و نقل</td>
                    <td><?php echo $shipping > 0 ? invoice_price_fa($shipping) : farsi_number('0'); ?></td>
                </tr>
                <tr class="final-price">
                    <td style="background:#e8f3e8;">مبلغ نهایی پرداخت</td>
                    <td><?php echo invoice_price_fa($total); ?> تومان</td>
                </tr>
            </table>
        </div>
        <div class="footer-sigs">
            <div>مهر و امضای فروشنده</div>
            <div>مهر و امضای مشتری</div>
        </div>
    </div>
    <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%3Ewindow.print()%3B%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object mce-object-script" width="20" height="20" alt="&lt;script&gt;" />
    </body>
    </html>
    <?php
    exit;
}

روش اول: فایل functions.php

اگر از قالب فرزند (Child Theme) استفاده می‌کنید، می‌توانید کد را در فایل: functions.php قالب فرزند قرار دهید.

توصیه می‌شود کد را مستقیماً در فایل functions.php قالب اصلی قرار ندهید؛ زیرا با به‌روزرسانی قالب ممکن است تغییرات شما از بین برود.

روش دوم: استفاده از افزونه Code Snippets

روش ساده‌تر این است که از افزونه‌های مدیریت Snippet استفاده کنید.

در این روش، کد را به‌عنوان یک Snippet جدید اضافه کرده و آن را فعال می‌کنید.

مزیت این روش این است که برای اضافه کردن کدهای PHP نیازی به ویرایش مستقیم فایل‌های قالب ندارید.

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *