[DI][doc_attribute] Export more attribute properties - boxalino/rtux-integration-magento2 GitHub Wiki
The presented use-case is for adding more product attribute information to the doc_attribute export.
Details
The easiest way to extend what is being exported for each attribute, is to create your own service based of Boxalino\DataIntegration\Service\Document\Attribute\EavAttribute
(the property handler) and Boxalino\DataIntegration\Model\DataProvider\Document\Attribute\EavAttribute
(the model that gets data for the property handler => the data provider).
Steps
- Create 2 files to replace the property handler (#1) and the data provider(#2)
- Create a resource model(#3) to be used in your data provider (#2) (it will be used to get data for each attribute & new property)
- To avoid further integration compatibilities, your services #1 & #2 should extend from the original services
- Service #1 (property handler) will rewrite the
getValues
function - Service #2 (data provider) will update the
__construct()
to add a new resource model (#3) and create functions to access the data for each attribute code & property to add to export
- In your integration layer di.xml, replace:
- the use of
Boxalino\DataIntegration\Service\Document\Attribute\EavAttribute
with service #1 - the use of
Boxalino\DataIntegration\Model\DataProvider\Document\Attribute\EavAttribute
with service #2
- Notify Boxalino which properties are exported and how they should be considered for further integrations
Code Samples
Property Handler
<?php declare(strict_types=1);
namespace <your-integration-layer>\Service\Document\Attribute;
use Boxalino\DataIntegrationDoc\Doc\DocSchemaInterface;
use Boxalino\DataIntegration\Service\Document\Attribute\EavAttribute;
class ExtendedEavAttribute extends EavAttribute
{
/**
* Structure: [property-name => [$schema, $schema], property-name => [], [..]]
*
* @return array
*/
public function getValues() : array
{
$content = parent::getValues();
$dataProvider = $this->getDataProvider();
foreach($content as $attributeCode => &$schema)
{
foreach($this->_getExtraProperties() as $propertyName)
{
$values = $dataProvider->getPropertyByAttributeCode($attributeCode, $propertyName);
if(empty($values))
{
continue;
}
$schema[DocSchemaInterface::FIELD_STRING][] = $this->schemaGetter()->getStringAttributeSchema([], $propertyName)->toArray();
}
}
return $content;
}
protected function _getExtraProperties() : array
{
return ["prop1", "prop2"];
}
}
Data Provider
<?php declare(strict_types=1);
namespace <your-integration-layer>\Model\DataProvider\Document\Attribute;
use Boxalino\DataIntegration\Model\DataProvider\Document\Attribute\EavAttribute;
use Boxalino\DataIntegration\Model\ResourceModel\Document\Attribute\EavAttribute as DataProviderResourceModel;
use <your-integration-layer>\Model\ResourceModel\Document\Attribute\AmastyEavAttribute as ExtendedDataProviderResourceModel;
class ExtendedEavAttribute extends EavAttribute
{
private $resourceModel;
private $attributeList;
protected $extendedResourceModel;
protected $extendedAttributeProperties = [];
public function __construct(
ExtendedDataProviderResourceModel $extendedResourceModel,
DataProviderResourceModel $resource
) {
$this->extendedResourceModel = $extendedResourceModel;
$this->resourceModel = $resource;
$this->attributeList = new \ArrayObject();
$this->extendedAttributeProperties = [];
parent::__construct($resource);
}
/**
* You can extend this function in order to preload all desired information from your extendedResourceModel just once
* (instead of calling the model each time to fetch information)
* @return void
*/
public function resolve() : void
{
parent::resolve();
}
public function getPropertyByAttributeCode(string $attributeCode, string $propertyName) : array
{
if(isset($this->extendedAttributeProperties[$attributeCode]))
{
if(isset($this->extendedAttributeProperties[$attributeCode][$propertyName]))
{
return is_array($this->extendedAttributeProperties[$attributeCode][$propertyName]) ? $this->extendedAttributeProperties[$attributeCode][$propertyName] : explode(",", $this->extendedAttributeProperties[$attributeCode][$propertyName]);
}
}
return [];
}
}