Allow for a position to be given with media imports.
Allow positions to be configured for image gallery. If position is left empty it will work as it has previously, allowing positions to be decided per product in Magento. If position has got a value, it will be set for the products during in import.
NOTE: it will only be set for default configuration, position could be overwritten if done at a store or store view level.
This PR looks promising and would fix an issue for one of my clients. Is it in use already?
It has not been merged yet so in that sense it is not yet in use. We use composer patches to add a remote diff of this PR so we can still use the functionality https://github.com/vaimo/composer-patches#usage-json-declaration https://patch-diff.githubusercontent.com/raw/akeneo/magento2-connector-community/pull/166.patch
I do hope it will be merged at some point 🤞
@indykoning I have updated the patch with the current akeneo version 101.3.2, see code below:
diff --git a/Block/Adminhtml/System/Config/Form/Field/Gallery.php b/Block/Adminhtml/System/Config/Form/Field/Gallery.php
index 3fd9608..4f275ed 100644
--- a/Block/Adminhtml/System/Config/Form/Field/Gallery.php
+++ b/Block/Adminhtml/System/Config/Form/Field/Gallery.php
@@ -24,6 +24,7 @@ class Gallery extends AbstractFieldArray
protected function _construct()
{
$this->addColumn('attribute', ['label' => __('Akeneo Attribute')]);
+ $this->addColumn('position', ['label' => __('Position')]);
$this->_addAfter = false;
$this->_addButtonLabel = __('Add');
diff --git a/Helper/Config.php b/Helper/Config.php
index d1a64da..0546b3b 100755
--- a/Helper/Config.php
+++ b/Helper/Config.php
@@ -666,7 +666,7 @@ public function getMediaImportImagesColumns()
*
* @return array
*/
- public function getMediaImportGalleryColumns()
+ public function getMediaImportGalleryColumns($raw = false)
{
/** @var array $images */
$images = [];
@@ -686,7 +686,15 @@ public function getMediaImportGalleryColumns()
if (!isset($image['attribute'])) {
continue;
}
- $images[] = $image['attribute'];
+
+ if ($raw) {
+ if(!isset($image['position'])) {
+ $image['position'] = 0;
+ }
+ $images[] = $image;
+ } else {
+ $images[] = $image['attribute'];
+ }
}
return $images;
diff --git a/Job/Product.php b/Job/Product.php
--- a/Job/Product.php
+++ b/Job/Product.php
@@ -3015,7 +3015,7 @@
/** @var string $tableName */
$tmpTable = $this->entitiesHelper->getTableName($this->getCode());
/** @var array $gallery */
- $gallery = $this->configHelper->getMediaImportGalleryColumns();
+ $gallery = $this->configHelper->getMediaImportGalleryColumns(true);
if (empty($gallery)) {
$this->setStatus(false);
@@ -3024,8 +3024,6 @@
return;
}
- $gallery = array_unique($gallery);
-
/** @var string $table */
$table = $this->entitiesHelper->getTable('catalog_product_entity');
/** @var string $columnIdentifier */
@@ -3037,11 +3035,11 @@
'sku' => 'identifier',
];
foreach ($gallery as $image) {
- if (!$connection->tableColumnExists($tmpTable, strtolower($image))) {
- $this->setMessage(__('Info: No value found in the current batch for the attribute %1', $image));
+ if (!$connection->tableColumnExists($tmpTable, $image['attribute'])) {
+ $this->setMessage(__('Warning: %1 attribute does not exist', $image['attribute']));
continue;
}
- $data[$image] = strtolower($image);
+ $data[$image['attribute']] = strtolower($image['attribute']);
}
/** @var bool $rowIdExists */
@@ -3076,16 +3074,16 @@
/** @var array $files */
$files = [];
foreach ($gallery as $image) {
- if (!isset($row[$image])) {
+ if (!isset($row[$image['attribute']])) {
continue;
}
- if (!$row[$image]) {
+ if (!$row[$image['attribute']]) {
continue;
}
/** @var array $media */
- $media = $this->akeneoClient->getProductMediaFileApi()->get($row[$image]);
+ $media = $this->akeneoClient->getProductMediaFileApi()->get($row[$image['attribute']]);
/** @var string $name */
$name = $this->entitiesHelper->formatMediaName(basename($media['code']));
/** @var string $filePath */
@@ -3095,7 +3093,7 @@
if (!$this->configHelper->mediaFileExists($filePath)) {
/** @var ResponseInterface $binary */
- $binary = $this->akeneoClient->getProductMediaFileApi()->download($row[$image]);
+ $binary = $this->akeneoClient->getProductMediaFileApi()->download($row[$image['attribute']]);
/** @var string $imageContent */
$imageContent = $binary->getBody()->getContents();
$this->configHelper->saveMediaFile($filePath, $imageContent);
@@ -3134,6 +3132,23 @@
];
$connection->insertOnDuplicate($galleryEntityTable, $data, array_keys($data));
+ if ($image['position']) {
+ $recordId = $connection->fetchOne(
+ $connection->select()
+ ->from($galleryValueTable, ['record_id'])
+ ->where('`value_id` = ?', $valueId)
+ ->where('`entity_id` = ?', $row[$columnIdentifier])
+ );
+
+ $data['position'] = $image['position'];
+
+ if ($recordId) {
+ $connection->update('catalog_product_entity_media_gallery_value', $data, '`record_id` = ' . $recordId);
+ } else {
+ $connection->insert('catalog_product_entity_media_gallery_value', $data);
+ }
+ }
+
// Get potential record_id from gallery value table
/** @var Select $select */
$select = $connection->select()->from($galleryValueTable)->where('value_id = ?', $valueId)->where(
@@ -3157,7 +3172,6 @@
'store_id' => 0,
$columnIdentifier => $row[$columnIdentifier],
'label' => '',
- 'position' => 0,
'disabled' => 0,
];
@@ -3170,7 +3184,7 @@
$columns = $this->configHelper->getMediaImportImagesColumns();
foreach ($columns as $column) {
- if ($column['column'] !== $image) {
+ if ($column['column'] !== $image['attribute']) {
continue;
}
/** @var array $data */
Can this PR be reviewed and merged? We have several clients with different types of issues due to this exact problem, and this PR seems to fix it very nicely.