Skip to content

Instantly share code, notes, and snippets.

@sunscreem
Created July 19, 2024 10:39
Show Gist options
  • Save sunscreem/6b24da52073e4b17f57a9ee73af35f9a to your computer and use it in GitHub Desktop.
Save sunscreem/6b24da52073e4b17f57a9ee73af35f9a to your computer and use it in GitHub Desktop.
<?php
namespace modules\productexporter\elements\exporters;
use Craft;
use craft\base\ElementExporter;
use craft\elements\db\ElementQueryInterface;
/**
* Products Exporter element exporter
*/
class ProductsExport extends ElementExporter
{
public static function displayName(): string
{
return 'J&C Custom Export';
}
function export(ElementQueryInterface $query): mixed
{
$data = [];
foreach ($query->each() as $product) {
$data[] = [
'SKU' => $product->defaultSku ?? '',
'Title' => $product->title ?? '',
'Description' => strip_tags($product->description),
'Price' => $product->defaultPrice ?? '',
'Stock' => $product->hasUnlimitedStock ? 'Unlimited' : $product->getTotalStock() ?? '',
'SalePrice' => $product->jcSalePrice ?? '',
'Enabled' => $product->enabled ? 'Enabled' : 'Disabled',
'Status' => ucfirst($product->status),
'Weight' => $product->defaultWeight ?? '',
'FinanceAvailable' => $product->financeAvailable ? 'Yes' : 'No',
'CanBeReserved' => $product->canBeReserved ? 'Yes' : 'No',
'CanBeEngraved' => $product->canBeEngraved ? 'Yes' : 'No',
'RingSizeRequired' => $product->ringSizeRequired ? 'Yes' : 'No',
'Clearance' => $product->clearance ? 'Yes' : 'No',
'URL' => $product->getUrl(),
];
}
return $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment