Skip to content

Instantly share code, notes, and snippets.

@sunscreem
Created July 19, 2024 10:39
Show Gist options
  • Select an option

  • Save sunscreem/6b24da52073e4b17f57a9ee73af35f9a to your computer and use it in GitHub Desktop.

Select an option

Save sunscreem/6b24da52073e4b17f57a9ee73af35f9a to your computer and use it in GitHub Desktop.

Revisions

  1. sunscreem created this gist Jul 19, 2024.
    46 changes: 46 additions & 0 deletions ProductsExport.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    <?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;
    }

    }