I created a custom image role drop down.
I am trying to save its value into ‘catalog_product_entity_media_gallery_value’ table by using the observer event ‘catalog_product_save_after’:
<?php
namespace HotcoffeeImageCustomRoleObserver;
use MagentoFrameworkEventObserverInterface;
class Productsaveafter implements ObserverInterface
{
protected $request;
protected $resource;
/**
*
* @param MagentoFrameworkAppRequestInterface $request
* @param MagentoFrameworkAppResourceConnection $resource
*/
public function __construct(
MagentoFrameworkAppRequestInterface $request, MagentoFrameworkAppResourceConnection $resource
) {
$this->request = $request;
$this->resource = $resource;
}
public function execute(MagentoFrameworkEventObserver $observer) {
// $galleryPart = array();
$data = $this->request->getPostValue();
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$messageManager = $objectManager->create('MagentoFrameworkMessageManagerInterface');
$array_element = 0;
if (isset($data['product']['media_gallery']['images'])) {
$images = $data['product']['media_gallery']['images'];
foreach ($images as $value) {
if ($value['value_id'] == "") {
$array_element++;
}
}
$productCollection = $objectManager->create('MagentoCatalogModelResourceModelProductCollectionFactory');
$collection = $productCollection->create();
$collection->getSelect()->join(
array('value_entity' => $collection->getTable('catalog_product_entity_media_gallery_value')),
'e.entity_id = value_entity.entity_id',
array('value_id')
);
$collection->getSelect()->join(
array('value_data' => $collection->getTable('catalog_product_entity_media_gallery')),
'value_entity.value_id = value_data.value_id',
array('value')
);
$product = $observer->getProduct();
if(empty($data['id'])){
$id = $product->getId();
}else{
$id = $data['id'];
}
$product = $collection->addFieldToFilter('entity_id', ['eq' => $id]);
$collection_updated = array_slice($product->getData(), "-" . $array_element, $array_element, true);
$new_image_id = [];
foreach ($collection_updated as $key => $value) {
$new_image_id[] = $value['value_id'];
}
try
{
$getGalleryPart = [];
$resource = $objectManager->get('MagentoFrameworkAppResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('catalog_product_entity_media_gallery_value');
foreach ($images as $image) {
print_r($image['GalleryPart_part']);
//if ($image['removed'] != "") {
// if(!empty($image['removed'])){
// $sql = "DELETE FROM " . $tableName . " WHERE value_id =" . $image['value_id'];
// $connection->query($sql);
// }
// if ($image['value_id'] == "") {
// if (!isset($image['store_id']) || $image['store_id'] == "") {
// $store_id = '0';
// } else {
// $store_id = $image['store_id'];
// }
// $getGalleryPart[] = $image['GalleryPart_part'];
// } else {
// $sqlGalleryPart = "update " . $tableName . " set GalleryPart_part = " . $image['GalleryPart_part'] . " where value_id = " . $image['value_id'];
// $connection->query($sqlGalleryPart);
// }
}
exit;
if ($array_element > 0) {
$count = 0;
foreach ($collection_updated as $key1 => $value1) {
$collection_updated[$key1]['new_GalleryPart_part'] = $getGalleryPart[$count];
$count++;
$sqlGalleryPart = "update " . $tableName . " set GalleryPart_part = " . $collection_updated[$key1]['new_GalleryPart_part'] . " where value_id = " . $collection_updated[$key1]['value_id'];
$connection->query($sqlGalleryPart);
}
}
} catch (MagentoFrameworkExceptionLocalizedException $e) {
$messageManager->addError($e->getMessage());
} catch (RuntimeException $e) {
$messageManager->addError($e->getMessage());
} catch (Exception $e) {
$messageManager->addError($e->getMessage());
}
}
// if (isset($data['product']['media_gallery']['images'])) {
// $images = $data['product']['media_gallery']['images'];
// foreach ($images as $image) {
// if (isset($image['GalleryPart_part']) && $image['GalleryPart_part'] == 1) {
// $galleryPart[$image['value_id']] = 1;
// } else {
// $galleryPart[$image['value_id']] = 0;
// }
// }
// // print_r($images);exit;
// $connection = $this->resource->getConnection();
// $tableName = 'catalog_product_entity_media_gallery_value'; //gives table name with prefix
// $product = $observer->getProduct();
// $mediaGallery = $product->getMediaGallery();
// if (isset($mediaGallery['images'])) {
// foreach ($mediaGallery['images'] as $image) {
// if (isset($galleryPart[$image['value_id']])) {
// //Update Data into table
// $sql = "Update " . $tableName . " Set GalleryPart_part = " . $galleryPart[$image['value_id']] . " where value_id = " . $image['value_id'];
// $connection->query($sql);
// }
// }
// }
// }
}
}
It is not saving the selected dropdown value in ‘GalleryPart_part’ column. Any help would be appreciated.