Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@duncanmcclean
Last active January 22, 2022 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duncanmcclean/f95905c1d129659d509a94ea242ad0f8 to your computer and use it in GitHub Desktop.
Save duncanmcclean/f95905c1d129659d509a94ea242ad0f8 to your computer and use it in GitHub Desktop.
Basic Sales Exporter for Simple Commerce

This gist contains code of a basic sales exporter for Simple Commerce, here's how to add it into your app:

  1. Install the Laravel Excel package - it does the heavy lifting of creating the spreadsheet files..
composer require maatwebsite/excel
  1. Copy the SalesExport.php file into a app/Exports directory.

  2. Copy route from the web.php file and put it in your routes/web.php file.

  3. When a user is logged into your site, they can go to /sales-export and it'll download the CSV export.

<?php
namespace App\Exports;
use DoubleThreeDigital\SimpleCommerce\Facades\Order;
use DoubleThreeDigital\SimpleCommerce\Facades\Product;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Statamic\Facades\Entry;
class SalesExport implements FromCollection, WithMapping, WithHeadings
{
protected $metadataKeys = [];
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
return Entry::whereCollection('orders')
->filter(function ($entry) {
return $entry->get('is_paid') === true;
})
->flatMap(function ($order) {
$lineItems = Order::find($order->id())->lineItems();
return $lineItems->map(function ($lineItem) use ($order) {
$order = $order->fresh();
foreach ($lineItem as $key => $value) {
$order->data()->put($key, $value);
}
if (isset($lineItem['metadata'])) {
foreach ($lineItem['metadata'] as $key => $value) {
$this->metadataKeys[] = $key;
}
}
return $order;
});
});
}
public function map($order): array
{
$product = Product::find($order->get('product'));
$productVariant = null;
if ($product->purchasableType() === 'variants' && $order->has('variant')) {
$productVariant = $product->variant(is_array($order->get('variant')) ? $order->get('variant')['variant'] : $order->get('variant'));
}
$row = [
$order->get('title'),
$order->get('paid_date'),
optional($order->augmentedValue('customer')->value())->get('name'),
optional($order->augmentedValue('customer')->value())->get('email'),
$order->get('billing_name'),
$order->get('billing_address') ?? $order->get('billing_address_line1'),
$order->get('billing_address_line2'),
$order->get('billing_city'),
$order->get('billing_region'),
$order->get('billing_country'),
$order->get('billing_zip_code') ?? $order->get('billing_postal_code'),
$order->get('shipping_name'),
$order->get('shipping_address') ?? $order->get('shipping_address_line1'),
$order->get('shipping_address_line2'),
$order->get('shipping_city'),
$order->get('shipping_country'),
$order->get('shipping_zip_code') ?? $order->get('shipping_postal_code'),
$order->get('shipping_region'),
$order->get('product'),
$product->title(),
optional($productVariant)->name() ?? '',
$order->get('quantity'),
$order->get('total'),
];
$rowMetadata = collect($this->metadataKeys)
->map(function ($key) use ($order) {
if (isset($order->get('metadata')[$key])) {
return $order->get('metadata')[$key];
}
return '';
})
->toArray();
return array_merge($row, $rowMetadata);
}
public function headings(): array
{
return [
'Order Number',
'Paid Date',
'Customer Name',
'Customer Email',
'Billing Name',
'Billing Address Line 1',
'Billing Address Line 2',
'Billing City',
'Billing Region',
'Billing Country',
'Billing Postal Code',
'Shipping Name',
'Shipping Address Line 1',
'Shipping Address Line 2',
'Shipping City',
'Shipping Region',
'Shipping Country',
'Shipping Postal Code',
'Product ID',
'Product Name',
'Variant Name',
'Quantity',
'Total',
];
}
}
<?php
// ...
Route::get('/sales-export', function () {
return Excel::download(new SalesExport, 'sales.csv');
})->middleware('auth');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment