Esempio n. 1
0
 function wc_subs_exporter_create_csv($export)
 {
     global $wc_subs_exporter;
     if (!$export->status) {
         return false;
     }
     if (empty($export->subscriptions)) {
         return false;
     }
     $subscription_statuses = wc_subs_exporter_get_subscription_statuses();
     $fields = array(__('Order Id', 'wc-subs-exporter'), __('Order Status', 'wc-subs-exporter'), __('Subscription Status', 'wc-subs-exporter'), __('Subscription Description', 'wc-subs-exporter'), __('Subscription Start Date', 'wc-subs-exporter'), __('Subscription Expiration Date', 'wc-subs-exporter'), __('Subscription Last Payment', 'wc-subs-exporter'), __('Email', 'wc-subs-exporter'), __('Billing First Name', 'wc-subs-exporter'), __('Billing Last Name', 'wc-subs-exporter'), __('Billing Address 1', 'wc-subs-exporter'), __('Billing Address 2', 'wc-subs-exporter'), __('Billing City', 'wc-subs-exporter'), __('Billing State', 'wc-subs-exporter'), __('Billing Zip', 'wc-subs-exporter'), __('Billing Country', 'wc-subs-exporter'), __('Shipping First Name', 'wc-subs-exporter'), __('Shipping Last Name', 'wc-subs-exporter'), __('Shipping Address 1', 'wc-subs-exporter'), __('Shipping Address 2', 'wc-subs-exporter'), __('Shipping City', 'wc-subs-exporter'), __('Shipping State', 'wc-subs-exporter'), __('Shipping Zip', 'wc-subs-exporter'), __('Shipping Country', 'wc-subs-exporter'), __('Product SKU', 'wc-subs-exporter'), __('Product Description', 'wc-subs-exporter'), __('Quantity', 'wc-subs-exporter'), __('Date Ordered', 'wc-subs-exporter'), __('Coupon Code Used', 'wc-subs-exporter'));
     $csv = '';
     if ($export->bom) {
         $csv .= chr(239) . chr(187) . chr(191) . '';
     }
     foreach ($fields as $field) {
         $csv .= wc_subs_exporter_escape_csv_value($field, $export->delimiter, $export->escape_formatting) . $export->delimiter;
     }
     $csv = substr($csv, 0, -strlen($export->delimiter)) . "\n";
     $processed_rows = 0;
     foreach ($export->subscriptions as $user => $user_subscriptions) {
         foreach ($user_subscriptions as $key => $subscription) {
             $order = new WC_Order($subscription['order_id']);
             $order_item = WC_Subscriptions_Order::get_item_by_product_id($order, $subscription['product_id']);
             $product = $order->get_product_from_item($order_item);
             if (empty($product)) {
                 continue;
             }
             $product_sku = $product->get_sku();
             $product_title = WC_Subscriptions_Order::get_item_name($subscription['order_id'], $subscription['product_id']);
             if (isset($product->variation_data)) {
                 $product_description = woocommerce_get_formatted_variation($product->variation_data, true);
             } else {
                 $product_description = $product_title;
             }
             $coupon_code_used = '';
             $coupons = $order->get_items(array('coupon'));
             foreach ($coupons as $item_id => $item) {
                 $coupon_code_used .= $item['name'] . ' ';
             }
             $start_date = substr($subscription['start_date'], 0, 10);
             if ($subscription['expiry_date']) {
                 $end_date = substr($subscription['expiry_date'], 0, 10);
             } else {
                 $end_date = '0000-00-00';
             }
             if ($order->user_id > 0) {
                 $user_info = get_userdata($order->user_id);
             }
             if (array_key_exists('completed_payments', $subscription) && is_array($subscription['completed_payments'])) {
                 $recent_payment = substr(end($subscription['completed_payments']), 0, 10);
             } else {
                 $recent_payment = '';
             }
             $order_date = substr($order->completed_date, 0, 10);
             $csv .= wc_subs_exporter_escape_csv_value($order->id, $export->delimiter, $export->escape_formatting) . $export->delimiter;
             $csv .= wc_subs_exporter_escape_csv_value($order->status, $export->delimiter, $export->escape_formatting) . $export->delimiter;
             $csv .= wc_subs_exporter_escape_csv_value($subscription_statuses[$subscription['status']], $export->delimiter, $export->escape_formatting) . $export->delimiter;
             $csv .= wc_subs_exporter_escape_csv_value($product_title, $export->delimiter, $export->escape_formatting) . $export->delimiter;
             $csv .= wc_subs_exporter_escape_csv_value($start_date, $export->delimiter, $export->escape_formatting) . $export->delimiter;
             $csv .= wc_subs_exporter_escape_csv_value($end_date, $export->delimiter, $export->escape_formatting) . $export->delimiter;
             $csv .= wc_subs_exporter_escape_csv_value($recent_payment, $export->delimiter, $export->escape_formatting) . $export->delimiter;
             $csv .= wc_subs_exporter_escape_csv_value($user_info->user_email, $export->delimiter, $export->escape_formatting) . $export->delimiter;
             $csv .= wc_subs_exporter_escape_csv_value($order->billing_first_name, $export->delimiter, $export->escape_formatting) . $export->delimiter;
             $csv .= wc_subs_exporter_escape_csv_value($order->billing_last_name, $export->delimiter, $export->escape_formatting) . $export->delimiter;
             $csv .= wc_subs_exporter_escape_csv_value($order->billing_address_1, $export->delimiter, $export->escape_formatting) . $export->delimiter;
             $csv .= wc_subs_exporter_escape_csv_value($order->billing_address_2, $export->delimiter, $export->escape_formatting) . $export->delimiter;
             $csv .= wc_subs_exporter_escape_csv_value($order->billing_city, $export->delimiter, $export->escape_formatting) . $export->delimiter;
             $csv .= wc_subs_exporter_escape_csv_value($order->billing_state, $export->delimiter, $export->escape_formatting) . $export->delimiter;
             $csv .= wc_subs_exporter_escape_csv_value($order->billing_postcode, $export->delimiter, $export->escape_formatting) . $export->delimiter;
             $csv .= wc_subs_exporter_escape_csv_value($order->billing_country, $export->delimiter, $export->escape_formatting) . $export->delimiter;
             $csv .= wc_subs_exporter_escape_csv_value($order->shipping_first_name, $export->delimiter, $export->escape_formatting) . $export->delimiter;
             $csv .= wc_subs_exporter_escape_csv_value($order->shipping_last_name, $export->delimiter, $export->escape_formatting) . $export->delimiter;
             $csv .= wc_subs_exporter_escape_csv_value($order->shipping_address_1, $export->delimiter, $export->escape_formatting) . $export->delimiter;
             $csv .= wc_subs_exporter_escape_csv_value($order->shipping_address_2, $export->delimiter, $export->escape_formatting) . $export->delimiter;
             $csv .= wc_subs_exporter_escape_csv_value($order->shipping_city, $export->delimiter, $export->escape_formatting) . $export->delimiter;
             $csv .= wc_subs_exporter_escape_csv_value($order->shipping_state, $export->delimiter, $export->escape_formatting) . $export->delimiter;
             $csv .= wc_subs_exporter_escape_csv_value($order->shipping_postcode, $export->delimiter, $export->escape_formatting) . $export->delimiter;
             $csv .= wc_subs_exporter_escape_csv_value($order->shipping_country, $export->delimiter, $export->escape_formatting) . $export->delimiter;
             $csv .= wc_subs_exporter_escape_csv_value($product_sku, $export->delimiter, $export->escape_formatting) . $export->delimiter;
             $csv .= wc_subs_exporter_escape_csv_value($product_description, $export->delimiter, $export->escape_formatting) . $export->delimiter;
             $csv .= wc_subs_exporter_escape_csv_value(1, $export->delimiter, $export->escape_formatting) . $export->delimiter;
             $csv .= wc_subs_exporter_escape_csv_value($order_date, $export->delimiter, $export->escape_formatting) . $export->delimiter;
             $csv .= wc_subs_exporter_escape_csv_value(trim($coupon_code_used), $export->delimiter, $export->escape_formatting) . $export->delimiter;
             $csv .= "\n";
         }
     }
     if (!$csv) {
         return false;
     } else {
         if (isset($wc_subs_exporter['debug']) && $wc_subs_exporter['debug']) {
             $wc_subs_exporter['debug_log'] = $csv;
         } else {
             return $csv;
         }
     }
 }
<?php

if (isset($_POST['from_date'])) {
    $from_date = $_POST['from_date'];
} else {
    $from_date = '';
}
if (isset($_POST['to_date'])) {
    $to_date = $_POST['to_date'];
} else {
    $to_date = '';
}
// initialize subscription status count
$subscription_statuses = wc_subs_exporter_get_subscription_statuses();
foreach ($subscription_statuses as $key => $status) {
    $subscription_count[$key] = WC_Subscriptions::get_subscription_count(array('subscription_status' => $key));
}
?>
<p>
  <?php 
_e('Make a selection below to export entries.', 'wc-subs-exporter');
?>
<br />
  <?php 
_e('When you click the export button below, Subscription Exporter will create a CSV file for you to save to your computer.', 'wc-subs-exporter');
?>
</p>
<form method="post" action="<?php 
echo add_query_arg(array('failed' => null, 'empty' => null));
?>
" id="postform">