Here is what I did to add a field in the system export form, and override the below mentioned files in order to return only attribute value for that particular selected store for each product.
- Override MagentoImportExportBlockAdminhtmlExportEditForm to add extra field
protected function _prepareForm()
{
parent::_prepareForm();
$form = $this->getForm();
/** @var Fieldset $fieldset */
$fieldset = $form->getElement('base_fieldset');
// Add store selection field
$fieldset->addField(
'store_id',
'select',
[
'name' => 'store_id',
'label' => __('Store View'),
'title' => __('Store View'),
'values' => $this->_getStoreOptions(),
'note' => __('Choose the store view to export data from'),
]
);
return $this;
}
protected function _getStoreOptions()
{
$options = [['label' => __('-- Please Select --'), 'value' => '']];
$stores = $this->_storeManager->getStores();
foreach ($stores as $store) {
$options[] = [
'label' => $store->getName(),
'value' => $store->getId()
];
}
return $options;
}
- view/adminhtml/templates/export/form/before.phtml
getFile = function() {
if ($('entity') && $F('entity')) {
var form = $('export_filter_form');
var oldAction = form.action;
var url = oldAction + ((oldAction.slice(-1) != '/') ? '/' : '') + 'entity/' + $F('entity')
+ '/file_format/' + $F('file_format');
if ($F('fields_enclosure')) {
url += '/fields_enclosure/' + $F('fields_enclosure');
}
// Addedd store_id to url param
if ($F('store_id')) {
url += '/store_id/' + $F('store_id');
}
form.action = url;
form.submit();
form.action = oldAction;
} else {
alert({
content: '{$block->escapeHtml(__('Invalid data'))}'
});
}
};
- Controller/Adminhtml/Export/Export.php – got the selected store value here.
public function execute()
{
if ($this->getRequest()->getPost(ExportModel::FILTER_ELEMENT_GROUP)) {
try {
$params = $this->getRequestParameters();
if (!array_key_exists('skip_attr', $params)) {
$params['skip_attr'] = [];
}
/** @var ExportInfoFactory $dataObject */
$dataObject = $this->exportInfoFactory->create(
$params['file_format'],
$params['entity'],
$params['store_id'],
$params['export_filter'],
$params['skip_attr'],
$this->localeResolver->getLocale()
);
$this->messagePublisher->publish('import_export.export', $dataObject);
$this->messageManager->addSuccessMessage(
__(
'Message is added to queue, wait to get your file soon.'
. ' Make sure your cron job is running to export the file'
)
);
} catch (Exception $e) {
$this->_objectManager->get(PsrLogLoggerInterface::class)->critical($e);
$this->messageManager->addErrorMessage(__('Please correct the data sent value.'));
}
} else {
$this->messageManager->addErrorMessage(__('Please correct the data sent value.'));
}
/** @var MagentoBackendModelViewResultRedirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setPath('adminhtml/*/index');
return $resultRedirect;
}
- vendor/magento/module-import-export/Model/Export/Entity/ExportInfoFactory.php
public function create($fileFormat, $entity, $store_id, $exportFilter, $skipAttr = [], ?string $locale = null)
{
$writer = $this->getWriter($fileFormat);
$entityAdapter = $this->getEntityAdapter(
$entity,
$store_id,
$fileFormat,
$exportFilter,
$skipAttr,
$writer->getContentType()
);
$fileName = $this->generateFileName($entity, $entityAdapter, $writer->getFileExtension());
/** @var ExportInfoInterface $exportInfo */
$exportInfo = $this->objectManager->create(ExportInfoInterface::class);
$exportInfo->setExportFilter($this->serializer->serialize($exportFilter));
$exportInfo->setSkipAttr($skipAttr);
$exportInfo->setFileName($fileName);
$exportInfo->setEntity($entity);
$exportInfo->setFileFormat($fileFormat);
$exportInfo->setContentType($writer->getContentType());
if ($locale) {
$exportInfo->setLocale($locale);
}
if ($store_id) {
$exportInfo->setStoreId($store_id);
}
return $exportInfo;
}
private function getEntityAdapter($entity, $store_id, $fileFormat, $exportFilter, $skipAttr, $contentType)
{
$entities = $this->exportConfig->getEntities();
if (isset($entities[$entity])) {
try {
$entityAdapter = $this->entityFactory->create($entities[$entity]['model']);
} catch (Exception $e) {
$this->logger->critical($e);
throw new MagentoFrameworkExceptionLocalizedException(
__('Please enter a correct entity model.')
);
}
if (!$entityAdapter instanceof MagentoImportExportModelExportEntityAbstractEntity &&
!$entityAdapter instanceof MagentoImportExportModelExportAbstractEntity
) {
throw new MagentoFrameworkExceptionLocalizedException(
__(
'The entity adapter object must be an instance of %1 or %2.',
MagentoImportExportModelExportEntityAbstractEntity::class,
MagentoImportExportModelExportAbstractEntity::class
)
);
}
// check for entity codes integrity
if ($entity != $entityAdapter->getEntityTypeCode()) {
throw new MagentoFrameworkExceptionLocalizedException(
__('The input entity code is not equal to entity adapter code.')
);
}
} else {
throw new MagentoFrameworkExceptionLocalizedException(__('Please enter a correct entity.'));
}
$entityAdapter->setParameters(
[
'fileFormat' => $fileFormat,
'entity' => $entity,
'storeId' => $store_id,
'exportFilter' => $exportFilter,
'skipAttr' => $skipAttr,
'contentType' => $contentType,
]
);
return $entityAdapter;
}
- MagentoCatalogImportExportModelExportProduct
public function setParameters(array $parameters)
{
// Capture the parameters
parent::setParameters($parameters);
// Set the store ID if it exists in the parameters
if (isset($parameters['storeId'])) {
$this->storeId = $parameters['storeId'];
}
return $this;
}
/**
* Get entity collection
*
* @param bool $resetCollection
* @return MagentoFrameworkDataCollectionAbstractDb
*/
protected function _getEntityCollection($resetCollection = false)
{
// if ($resetCollection || empty($this->_entityCollection)) {
// $this->_entityCollection = $this->_entityCollectionFactory->create();
// }
// Apply store filter if store ID is set
if ($this->storeId) {
$this->_entityCollection->setStoreId($this->storeId);
}
return $this->_entityCollection;
}
But its not working. no change in the exported file listing values for all of the stores. Any help is appreciated.