Skip to content

Commit 8fb9514

Browse files
Select how created/updated date is exported
1 parent 5006526 commit 8fb9514

5 files changed

Lines changed: 69 additions & 0 deletions

File tree

Model/Config.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class Config
4242
public const PATH_SKIP_CHILD_BY_COMPOSITE_TYPE = 'tweakwise/export/skip_child_by_composite_type';
4343
public const CALCULATE_COMPOSITE_PRICES = 'tweakwise/export/calculate_composite_prices';
4444
public const PATH_GROUPED_EXPORT_ENABLED = 'tweakwise/export/grouped_export_enabled';
45+
public const PATH_DATE_FIELD = 'tweakwise/export/date_field';
4546

4647
/**
4748
* Default feed filename
@@ -328,4 +329,20 @@ public function calculateCombinedPrices(Store $store): bool
328329
{
329330
return (bool) $this->config->isSetFlag(self::CALCULATE_COMPOSITE_PRICES, ScopeInterface::SCOPE_STORE, $store);
330331
}
332+
333+
/**
334+
* @return string
335+
*/
336+
public function getDateField(): string
337+
{
338+
return (string) $this->config->getValue(self::PATH_DATE_FIELD);
339+
}
340+
341+
/**
342+
* @return array
343+
*/
344+
public function getDateAttributes(): array
345+
{
346+
return ['created_at', 'updated_at'];
347+
}
331348
}

Model/Config/Source/DateField.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Tweakwise\Magento2TweakwiseExport\Model\Config\Source;
4+
5+
use Magento\Framework\Option\ArrayInterface;
6+
7+
class DateField implements ArrayInterface
8+
{
9+
/**
10+
* {@inheritdoc}
11+
*/
12+
public function toOptionArray()
13+
{
14+
$dateFields = [
15+
'all' => 'All Dates',
16+
'min' => 'Min Date',
17+
'max' => 'Max Date',
18+
];
19+
20+
return $dateFields;
21+
}
22+
}

Model/Write/Products/ExportEntity.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Store\Model\Store;
1313
use Tweakwise\Magento2TweakwiseExport\Model\Config;
1414
use Magento\Catalog\Model\Product\Type;
15+
use function in_array;
1516

1617
/**
1718
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
@@ -280,12 +281,31 @@ public function getCategories(): array
280281
return $this->categories;
281282
}
282283

284+
public function addDate(string $attribute, string $value): void
285+
{
286+
if ($this->config->getDateField() === 'min') {
287+
if (!isset($this->attributes[$attribute]) || strtotime($value) < strtotime(current($this->attributes[$attribute]))) {
288+
$this->attributes[$attribute] = [$value];
289+
return;
290+
}
291+
} elseif ($this->config->getDateField() === 'max') {
292+
if (!isset($this->attributes[$attribute]) || strtotime($value) > strtotime(current($this->attributes[$attribute]))) {
293+
$this->attributes[$attribute] = [$value];
294+
}
295+
}
296+
}
297+
283298
/**
284299
* @param string $attribute
285300
* @param mixed $value
286301
*/
287302
public function addAttribute(string $attribute, $value): void
288303
{
304+
if ($this->config->getDateField() !== 'all' && in_array($attribute, $this->config->getDateAttributes(), true)) {
305+
$this->addDate($attribute, (string) $value);
306+
return;
307+
}
308+
289309
if (!isset($this->attributes[$attribute])) {
290310
$this->attributes[$attribute] = [];
291311
}

etc/adminhtml/system.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@
109109
</depends>
110110
</field>
111111

112+
<field id="date_field" translate="label,comment" type="select" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">
113+
<label>Date fields (created at/ updated at)</label>
114+
<comment>Choose which dates to include: All exports every date (including child records), Min exports only the earliest, and Max exports only the latest.</comment>
115+
<source_model>Tweakwise\Magento2TweakwiseExport\Model\Config\Source\DateField</source_model>
116+
<depends>
117+
<field id="enabled">1</field>
118+
</depends>
119+
</field>
120+
112121
<field id="price_field" translate="label,comment" type="select" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">
113122
<label>Price field</label>
114123
<comment>Select which field is used as "price" in Tweakwise, selection will select each price field in order and return the first nonzero value. Prices are exported from price index table, note that catalogrule prices are available in field "min_price" so it could be that this value is lowed then for example "final_price". If your configurable / bundle products do not have a price attribute filled this will be zero.</comment>

etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<exclude_child_attributes>url_key,small_image,small_image_label,thumbnail_label,status,tax_class_id,type_id,required_options,visibility</exclude_child_attributes>
2525
<skip_child_by_composite_type/>
2626
<price_field>min_price,final_price,price,max_price</price_field>
27+
<date_field>all</date_field>
2728
<batch_size_categories>5000</batch_size_categories>
2829
<batch_size_products>5000</batch_size_products>
2930
<batch_size_products_children>5000</batch_size_products_children>

0 commit comments

Comments
 (0)