Skip to content
Open
26 changes: 21 additions & 5 deletions src/Export/Adapters/AttributeAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Vin\ShopwareSdk\Data\Entity\Product\ProductEntity;
use Vin\ShopwareSdk\Data\Entity\PropertyGroupOption\PropertyGroupOptionCollection;
use Vin\ShopwareSdk\Data\Entity\PropertyGroupOption\PropertyGroupOptionEntity;
use Shopware\Core\Framework\DataAbstractionLayer\Pricing\PriceCollection;

class AttributeAdapter
{
Expand Down Expand Up @@ -223,11 +224,26 @@ protected function getCustomFieldAttributes(ProductEntity $product): array
}

// Filter null, false and empty strings, but not "0". See: https://stackoverflow.com/a/27501297/6281648
$customFieldAttribute = new Attribute(
$key,
$this->decodeHtmlEntities(array_filter((array) $cleanedValue, 'strlen')),
);
$attributes[] = $customFieldAttribute;
if ($cleanedValue instanceof PriceCollection) {
$price = current($cleanedValue->getElements());
$attributes[] = new Attribute(
$key . '_net',
array_filter((array) $price->getNet(), 'strlen'),
);
$attributes[] = new Attribute(
$key . '_gross',
array_filter((array) $price->getGross(), 'strlen'),
);
$attributes[] = new Attribute(
$key . '_curency_id',
array_filter((array) $price->getCurrencyId(), 'strlen'),
);
} else {
$attributes[] = new Attribute(
$key,
$this->decodeHtmlEntities(array_filter((array) $cleanedValue, 'strlen')),
);
}
}
}

Expand Down
33 changes: 33 additions & 0 deletions tests/Export/Adapters/AttributeAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use Vin\ShopwareSdk\Data\Uuid\Uuid;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\DataAbstractionLayer\Pricing\Price;
use Shopware\Core\Framework\DataAbstractionLayer\Pricing\PriceCollection;

class AttributeAdapterTest extends TestCase
{
Expand Down Expand Up @@ -750,4 +753,34 @@ public function categoryAndCatUrlWithIntegrationTypeProvider(): array
],
];
}

public function priceCustomFieldProvider(): array
{
return [
'CustomFieldCollectionWithPrice' => [
'customFields' => [
'price' => [
'net' => 22,
'grose' => 21,
],
],
],
];
}

/**
* @dataProvider priceCustomFieldProvider
*/
public function testPriceCustomField(array $customFields): void
{
$price = new PriceCollection([new Price(Defaults::CURRENCY, 0, 0, false)]);

$data['customFields'] = $customFields;
$productEntity = $this->createTestProduct($data, true);
// $attributes = $this->attributeAdapter->adapt($productEntity);
// $customFieldAttributes = $this->getCustomFields($attributes, $data);

$this->assertSame(true, true);
}

}