fgc-modify-export-csv.php 4.58 KB
<?php

/*
  Plugin Name: Modify export csv
  Plugin URI: http://fgc.com
  Description: Modify export csv
  Version: 1.0
  Author: Phuong An <phuongantt.na@gmail.com>
  Author URI: http://fgc.com
 */

// Add and modify column headers
function fgc_modify_headers_data($column_headers) {
    $custom_column_headers = array(
        'OrderQuantity' => 'TransactionNumber',
        'CustomerName' => 'CustomerName',
        'order_id' => 'ReferenceNumber',
        'order_number' => 'PurchaseOrderNumber',
        'ShipperAccount' => 'ShipperAccount',
        'ShipCarrier' => 'ShipCarrier',
        'ShipService' => 'ShipService',
        'ShipBilling' => 'ShipBilling',
        'ShipAccount' => 'ShipAccount',
        'ShipToName' => 'ShipToName',
        'ShipToCompany' => 'ShipToCompany',
        'shipping_address_1' => 'ShipToAddress1',
        'shipping_address_2' => 'ShipToAddress2',
        'shipping_city' => 'ShipToCity',
        'shipping_state' => 'ShipToState',
        'shipping_postcode' => 'ShipToZip',
        'shipping_country' => 'ShipToCountry',
        'ShipToPhone' => 'ShipToPhone',
        'ShipToFax' => 'ShipToFax',
        'billing_email' => 'ShipToEmail',
        'customer_note' => 'CarrierNotes',
        'ShipToDeptNumber' => 'ShipToDeptNumber',
        'RetailerID' => 'RetailerID',
        'TotalCartons' => 'TotalCartons',
        'TotalPallets' => 'TotalPallets',
        'TotalWeight' => 'TotalWeight',
        'TotalVolume' => 'TotalVolume',
        'BOLNum' => 'BOLNum',
        'TrackingNum' => 'TrackingNum',
        'TrailerNum' => 'TrailerNum',
        'SealNum' => 'SealNum',
        'ShipDate' => 'ShipDate',
        'billing_company' => 'BillToCompany',
        'billing_address_1' => 'BillToAddress1',
        'billing_address_2' => 'BillToAddress2',
        'billing_city' => 'BillToCity',
        'billing_state' => 'BillToState',
        'billing_postcode' => 'BillToZip',
        'billing_country' => 'BillToCountry',
        'billing_phone' => 'BillToPhone',
        'PickupDate' => 'PickupDate',
        'RequiresDeliveryConf' => 'RequiresDeliveryConf',
        'RequiresReturnReceipt' => 'RequiresReturnReceipt',
    );
    foreach ($column_headers as $key => $value) {
        if (!array_key_exists($key, $custom_column_headers)) {
            $custom_column_headers[$key] = $value;
        }
    }
    return $custom_column_headers;
}

add_filter('wc_customer_order_csv_export_order_headers', 'fgc_modify_headers_data');

// set the data for each for custom columns
function fgc_modify_rows_data($order_data, $order) {
    foreach ($order->get_items() as $key => $value) {
        if ($value['variation_id'] > 0) {
            foreach ($value['item_meta_array'] as $_key => $_value) {
                if ($_value->key == 'drinks') {
                    $pack = (int) $_value->value / 6;
                }
            }
        } else {
            $pack = (int) str_replace(' drinks', '', $value['name']) / 6;
        }
    }
    $order_data['OrderQuantity'] = $pack;
    $order_data['CustomerName'] = "REIZE ENERGY DRINK";
//    $order_data['CustomerName'] = $order->billing_first_name . ' ' . $order->billing_last_name;
    $order_data['ShipToName'] = $order->shipping_first_name . ' ' . $order->shipping_last_name;
    $order_data['ShipToCompany'] = $order->shipping_first_name . ' ' . $order->shipping_last_name;
    $order_data['billing_email'] = $order->billing_email . ';contact@uncleanduncle.com';
    if (get_post_meta($order->id, 'authorise', true) == 1) {
        $customNote = $order->customer_note;
        if ($customNote === '') {
            $order_data['customer_note'] = 'ATL - Authorised to Leave';
        } else {
            $order_data['customer_note'] = $customNote . ' - ATL - Authorised to Leave';
        }
    }
    $order_data['ShipToPhone'] = $order_data['billing_phone'];
    return $order_data;
}

add_filter('wc_customer_order_csv_export_order_row', 'fgc_modify_rows_data', 10, 2);

function fgc_mark_12_drinks_as_csv_exported($order_id) {
    $order = new WC_Order($order_id);
    $drinks = 0;
    foreach ($order->get_items() as $key => $value) {
        if ($value['variation_id'] > 0) {
            foreach ($value['item_meta_array'] as $_key => $_value) {
                if ($_value->key == 'drinks') {
                    $drinks = (int) $_value->value;
                }
            }
        } else {
            $drinks = (int) str_replace(' drinks', '', $value['name']);
        }
    }
    if ($drinks == 12) {
        update_post_meta($order_id, '_wc_customer_order_csv_export_is_exported', 1);
    }
}

add_action('woocommerce_payment_complete', 'fgc_mark_12_drinks_as_csv_exported', 10);