File: D:/HostingSpaces/SBogers10/shop.komma.nl/app/Products/Product/Transfer/ProductCsvExportService.php
<?php
namespace App\Products\Product\Transfer;
use Komma\KMS\Transfer\AbstractCsvExportService;
use Komma\KMS\Globalization\Languages\Models\Language;
use App\Discounts\Actions\ModifyPriceAction;
use App\Discounts\Conditions\QuantityDiscountCondition;
use App\Discounts\DiscountServiceInterface;
use App\Products\Product\Product;
/**
* Class ProductCsvExportService
*
* Is directed by a ImportExportService and can export Product related data to a multidimensional csv array.
* The first level keys in the array represent rows by arrays. The keys from those arrays represent columns and their values,
* The column values.
*
* @package App\Products\Product
*/
class ProductCsvExportService extends AbstractCsvExportService
{
use ProductColumnMapsTrait;
/** @var DiscountServiceInterface $discountService */
private $discountService;
public function __construct()
{
$this->discountService = app(DiscountServiceInterface::class);
}
/**
* Returns an array of arrays that represent products.
* Each row represents an item in the form of an array. In the case of products an example could be:
* [
* ['1', 'my awesome product', 'product description']
* ['1', 'my awesome second product', 'product description 2']
* ]
*
* @return array
*/
function export():array
{
$languageIso2s = ['nl', 'en', 'de']; //The order of the iso2's also determines their order in the csv. From left to right.
$languages = Language::whereIn('iso_2', $languageIso2s)->get()->sortBy(function($language) use ($languageIso2s) {
return array_search($language->iso_2, $languageIso2s);
});
$products = Product::with(['translations'])->get();
$productsCsv = $products->map(function(Product $product) use($languages) {
//Product fields
$productDataCollection = collect([$product->active, $product->price, (string) $product->stock_keeping_unit]);
//Discount
$discountDataCollection = collect();
$discount = $this->discountService->getQuantityDiscountForASpecificDiscountable($product);
if($discount)
{
/** @var QuantityDiscountCondition $quantityDiscountCondition */
$quantityDiscountCondition = $discount->getDiscountCondition();
/** @var ModifyPriceAction $modifyPriceAction */
$modifyPriceAction = $discount->getDiscountAction();
$discountDataCollection->push($quantityDiscountCondition->getQuantity().'/'.$modifyPriceAction->getModificationValue());
} else {
$discountDataCollection->push('');
}
//Product translation fields
$productTranslationDataCollection = $languages->map(function (Language $language) use($product) {
$productTranslation = $product->translations->where('language_id', $language->id)->first();
if($productTranslation) {
return [$productTranslation->name, $productTranslation->description, $productTranslation->meta_description];
} else {
return ['', '', ''];
}
})->collapse();
$row = $productDataCollection->merge($discountDataCollection)->merge($productTranslationDataCollection);
return $row;
})->toArray();
return $productsCsv;
}
}