Magento 2 Event Observers: Capturing Category Saves
As a Magento developer, you often need to interact with core events to perform custom logic during key processes like category saves. In this post, weâll explore how to capture both the creation and update of categories using event observers in Magento 2.
Understanding Magento 2 Events for Categories
Magento 2 provides several events related to category operations that you can tap into:
-
catalog_category_prepare_saveThis event triggers before a category is saved, allowing you to access the category object during creation or update. It’s ideal for updating existing categories with new data. -
catalog_category_save_afterSimilar to the above, but fires after the category has been successfully saved. Useful for performing actions based on the updated state of the category. -
catalog_category_prepare_product_saveThis event is more specific and triggers before a product is associated with a category (during product save).
Capturing Both Category Creation and Update
In your scenario, you’ve correctly setup an observer to catch the catalog_category_prepare_save event. However, as you noted, this only works for updating existing categories. To also capture new category creation:
- Create another observer for the
catalog_category_save_afterevent. - In this new observer’s
execute()method, check if the category has a new ID (indicating it’s being created).
Let me show you how to update your setup:
Step 1: Add the New Event Observer
Update your events.xml:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="catalog_category_prepare_save">
<observer name="categoryattributes" instance="Namespace\Module\Observer\Categoryattributes"/>
</event>
<!-- New event observer for category save after -->
<event name="catalog_category_save_after">
<observer name="categorycreated" instance="Namespace\Module\Observer\CategoryCreated"/>
</event>
</config>
Step 2: Create the New Observer Class
Create a CategoryCreated.php file in your observer directory:
<?php
namespace Namespace\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Catalog\Model\CategoryFactory;
class CategoryCreated implements ObserverInterface
{
private $category = null;
private $categoryFactory;
public function __construct(CategoryFactory $categoryFactory)
{
$this->categoryFactory = $categoryFactory;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$this->category = $observer->getEvent()->getCategory();
// Check if the category is new
if ($this->category->getId() === null) {
// Category is being created, perform your custom logic here
// For example:
$newCategory = $this->categoryFactory->create();
$newCategory->setName('New Category');
$newCategory->save();
}
}
}
Step 3: Update Your existing Observer
Your Categoryattributes.php observer can remain as is. It will now be triggered only when an existing category is being updated.
Conclusion
By using a combination of catalog_category_prepare_save and catalog_category_save_after, you can capture both the creation and update of categories in Magento 2.
Remember, it’s crucial to follow best practices like proper class naming, constructor injection for dependencies, and keeping your code organized. For more advanced use cases or detailed examples, consider checking out the official Magento Developer Documentation at https://developer.adobe.com/experience-cloud/magento/.
Happy coding! Let me know if you have any further questions about Magento 2 event observers or customization techniques.