Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace HarrigoProductUpdateObserverCatalog;
- class ProductSaveBefore implements MagentoFrameworkEventObserverInterface
- {
- /**
- * Execute observer
- *
- * @param MagentoFrameworkEventObserver $observer
- * @return void
- */
- public function execute(
- MagentoFrameworkEventObserver $observer
- ) {
- $_product = $observer->getProduct(); // get product object
- //get title
- $title = $_product->getName();
- //get the images
- $existingMediaGalleryEntries = $_product->getMediaGalleryEntries();
- foreach ($existingMediaGalleryEntries as $key => $entry) {
- //set new title for each image
- $entry->setLabel($title);
- }
- //set the new labels for product
- $_product->setMediaGalleryEntries($existingMediaGalleryEntries);
- }
- }
- <?xml version="1.0" ?>
- <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
- <event name="catalog_product_save_before">
- <observer instance="HarrigoProductUpdateObserverCatalogProductSaveBefore" name="harrigo_productupdate_observer_catalog_productsavebefore_catalog_product_save_before"/>
- </event>
- </config>
Add Comment
Please, Sign In to add comment