您目前正在查看 訂單匯出進階技巧,匯出訂單內手動新增的商品項目

訂單匯出進階技巧,匯出訂單內手動新增的商品項目

使用外掛

訂單匯出進階技巧,匯出訂單內手動新增的商品項目
手動新增的商品資料(FEE)

首先使用的訂單匯出外掛為 Advanced Order Export For WooCommerce

 

有時候我們會手動新增訂單用於紀錄部分線下交易或是其他的成交內容

這時候可能就不會使用網站上已經上架的商品,而是手動新增商品(fee)在訂單內

如果直接使用外掛匯出訂單資料的話,這些手動新增的內容並不會被匯出

商品名稱(Product Name)不是匯出空白欄位,就是直接被系統略過

那要如果才能匯出這些手動新增的資料呢?

訂單匯出進階技巧,匯出訂單內手動新增的商品項目
匯出的訂單只會有空白欄位

設定匯出方式

這時候可以在匯出訂單設定的下方 其他設定 > 自訂 PHP 代碼以修改輸出

並插入以下代碼

代碼參考網站,裡面的export Fee line as Product

訂單匯出進階技巧,匯出訂單內手動新增的商品項目
插入代碼
//export Fee line as Products
add_filter('woe_fetch_order_products', function ($products, $order, $labels, $format, $static_vals) {
    $i = count($products);
    foreach($order-> get_items('fee') as $item_id => $item) {
        $item_meta = $order->get_item_meta( $item_id );
        $fee_amount = $item_meta['_fee_amount'][0];
        $tax_amount = $item_meta['_line_tax'][0];
        $row = array();
        $i++;
        foreach($labels as $field => $label) {
            if ($field == 'line_id') {
                $row[$field] = $i;
            }
            elseif($field == 'name') {
                $row['name'] = $item["name"];
            }
            elseif($field == 'qty') {
                $row['qty'] = 1;
            }
            elseif($field == 'tax_rate') {
                $row[$field] = round($tax_amount/$fee_amount*100);
            }
            elseif($field == 'line_no_tax') {
                $row[$field] = $fee_amount;
            }
            elseif($field == 'line_tax') {
                $row[$field] = $tax_amount;
            }
            elseif($field == 'line_subtotal') {
                $row[$field] = $fee_amount;
            }
            elseif($field == 'line_total') {
                $row[$field] = $fee_amount;
            }
            elseif($field == 'line_total_plus_tax') {
                $row[$field] = $fee_amount + $tax_amount;
            }
            elseif($field == 'item_price') {
                $row[$field] = $fee_amount;
            }
            else {
                $row[$field] = $item[$field];
            }
        }
        $products[] = $row;
    }
    return $products;
}, 10, 5);

然後匯出的欄位使用 Product order items > 項目名稱(Item Name) 就可以囉

不要使用產品名稱(Product Name),產品名稱還是抓不到手動新增的項目

訂單匯出進階技巧,匯出訂單內手動新增的商品項目
成功匯出手動新增的商品名稱資料 使用Item Name