Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/code/core/Mage/Catalog/Model/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -1464,6 +1464,16 @@ public function duplicate()
->setSkipImagesOnDuplicate($newProduct->getSkipImagesOnDuplicate())
->duplicate($this->getId(), $newProduct->getId());

$attributes = $this->getTypeInstance(true)->getSetAttributes($this);
// duplicate media after $this->getResource()->duplicate()
if (!$newProduct->getSkipImagesOnDuplicate() && isset($attributes['media_gallery'])) {
/** @var Mage_Catalog_Model_Resource_Eav_Attribute $mediaGalleryAttribute */
$mediaGalleryAttribute = $attributes['media_gallery'];
/** @var Mage_Catalog_Model_Product_Attribute_Backend_Media $backend */
$backend = $mediaGalleryAttribute->getBackend();
$backend->duplicate($newProduct);
}

// TODO - duplicate product on all stores of the websites it is associated with
/*if ($storeIds = $this->getWebsiteIds()) {
foreach ($storeIds as $storeId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public function validate($object)
*/
public function beforeSave($object)
{
if ($object->getIsDuplicate() == true) {
return;
}

$attrCode = $this->getAttribute()->getAttributeCode();
$value = $object->getData($attrCode);
if (!is_array($value) || !isset($value['images'])) {
Expand All @@ -120,34 +124,17 @@ public function beforeSave($object)
$clearImages = [];
$newImages = [];
$existImages = [];
if ($object->getIsDuplicate() != true) {
foreach ($value['images'] as &$image) {
if (!empty($image['removed'])) {
$clearImages[] = $image['file'];
} elseif (!isset($image['value_id'])) {
$newFile = $this->_moveImageFromTmp($image['file']);
$image['new_file'] = $newFile;
$newImages[$image['file']] = $image;
$this->_renamedImages[$image['file']] = $newFile;
$image['file'] = $newFile;
} else {
$existImages[$image['file']] = $image;
}
}
} else {
// For duplicating we need copy original images.
$duplicate = [];
foreach ($value['images'] as &$image) {
if (!isset($image['value_id'])) {
continue;
}

$newFile = $this->_copyImage($image['file']);
$newImages[$image['file']] = [
'new_file' => $newFile,
'label' => $image['label'],
];
$duplicate[$image['value_id']] = $newFile;
foreach ($value['images'] as &$image) {
if (!empty($image['removed'])) {
$clearImages[] = $image['file'];
} elseif (!isset($image['value_id'])) {
$newFile = $this->_moveImageFromTmp($image['file']);
$image['new_file'] = $newFile;
$newImages[$image['file']] = $image;
$this->_renamedImages[$image['file']] = $newFile;
$image['file'] = $newFile;
} else {
$existImages[$image['file']] = $image;
}
}

Expand Down Expand Up @@ -200,7 +187,6 @@ public function getRenamedImage($file)
public function afterSave($object)
{
if ($object->getIsDuplicate() == true) {
$this->duplicate($object);
return;
}

Expand Down Expand Up @@ -700,9 +686,17 @@ public function duplicate($object)
return $this;
}

$newImagesMap = [];
foreach ($mediaGalleryData['images'] as &$image) {
$newImagesMap[$image['file']] = [
'file' => $this->_copyImage($image['file']),
'old_value_id' => $image['value_id'],
];
}

$this->_getResource()->duplicate(
$this,
$mediaGalleryData['duplicate'] ?? [],
$newImagesMap,
$object->getOriginalId(),
$object->getId(),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public function duplicate($object, $newFiles, $originalProductId, $newProductId)
$data = [
'attribute_id' => $object->getAttribute()->getId(),
'entity_id' => $newProductId,
'value' => $newFiles[$row['value_id']] ?? $row['value'],
'value' => $newFiles[$row['value']]['file'],
];

$valueIdMap[$row['value_id']] = $this->insertGallery($data);
Expand All @@ -200,6 +200,33 @@ public function duplicate($object, $newFiles, $originalProductId, $newProductId)
$this->insertGalleryValueInStore($row);
}

// Duplicate product store values
$mediaAttributes = Mage::getModel('catalog/product')->getMediaAttributes();
$attributeIds = array_map(fn($attr) => $attr->getAttributeId(), $mediaAttributes);
$tableName = $this->getTable(['catalog/product', 'varchar']);
$select = $this->_getReadAdapter()->select()
->from($tableName, [
'entity_type_id',
'attribute_id',
'store_id',
'value',
])
->where('entity_id = ?', $originalProductId)
->where('attribute_id IN (?)', $attributeIds);

$attrRows = [];
foreach ($this->_getReadAdapter()->fetchAll($select) as $row) {
$attrRows[] = [
'entity_type_id' => $row['entity_type_id'],
'attribute_id' => $row['attribute_id'],
'store_id' => $row['store_id'],
'entity_id' => $newProductId,
'value' => $newFiles[$row['value']]['file'],
];
}

$this->_getWriteAdapter()->insertOnDuplicate($tableName, $attrRows, ['value']);

return $this;
}

Expand Down
Loading