Magento2 Events Observers
Hey Magento folks, this article is based on Magento2 1.0.0 beta version. I will be briefly explaining the event observers in Magento2. Developers who have worked on Magento 1.x series are well aware the importance of events-observers in terms of developing custom functionality, and a way to avoid core code modification.
In Magento2 , it is nearly the same concept, just the aesthetics of using it has changed. So lets dive into it without any further ado.
Contrary to Magento 1.x, all event defintions fo into a seperate xml file i.e events.xml
So in your module folder, there could be three events file.
- etc/events.xml
- etc/frontend/events.xml
- etc/adminhtml/events.xml
So as you might have guessed first file is for global scope and then frontend , adminhtml respectively.
So late’s say, i need to observer event, checkout_cart_add_product_complete
i will create an events.xml
* @param \Magento\Framework\Event\Observer $observer
* @return void
*
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
//execute your code here
}
Okie, so going deep into, how the events are initialize
//file \Magento\Checkout\Controller\Cart\Add.php -113
$this->_eventManager->dispatch(
‘checkout_cart_add_product_complete’,
[‘product’ => $product, ‘request’ => $this->getRequest(), ‘response’ => $this->getResponse()]
);
Where event manager is a object of \Magento\Framework\Event\ManagerInterface
So in same way we could initialize and fire our own custom events.
You can find list of events fired throughout Magento2 here
Building Magento 2 Extension – lets begin…
Building Magento 2 Extension – lets begin…
Building Magento 2 Extension – ExtendedConfig
Magento 2 branches, composer packages and sample data
Magento create admin user programmatically
Hello all,
Here is custom script to create admin user programmatically.
<?php
# Create New admin User programmatically.
require_once(‘./app/Mage.php’);
umask(0);
Mage::app();
try {
$user = Mage::getModel(‘admin/user’)
->setData(array(
‘username’ => ‘admin1’,
‘firstname’ => ‘Admin’,
‘lastname’ => ‘Admin’,
’email’ => ‘santosh@test.com’,
‘password’ =>’admin123′,
‘is_active’ => 1
))->save();
} catch (Exception $e) {
echo $e->getMessage();
exit;
}
//Assign Role Id
try {
$user->setRoleIds(array(1)) //Administrator role id is 1 ,Here you can assign other roles ids
->setRoleUserId($user->getUserId())
->saveRelations();
} catch (Exception $e) {
echo $e->getMessage();
exit;
}
echo “User created successfully”;
?>
Put the above content in a magento root folder in a file and then browse the file.
You are done with creating new admin user.
Chears
Programmatically Adding Attributes and Attribute Sets (Example)
Programmatically Adding Attributes and Attribute Sets (Example) ======
Comprehensive error-checking has been omitted.
Get Product attribute’s select option Id or Lable or Value in Magento
If you have a select dropdown for any product attribute, to get the value from label or vice versa is always needed in order to display or get value for further processing.Get product attribute’s value from label, label from value easily in Magento.
Suppose, you have an product attribute called “color” in Magento. You have the label (e.g. Purple), and you want to find it’s value. The below code will help you get the value for it.
$_product = Mage::getModel('catalog/product');
$attr = $_product->getResource()->getAttribute("color");
if ($attr->usesSource()) {
echo $color_id = $attr->getSource()->getOptionId("Purple");
}
Now suppose, you have the value (let’s say 10, for Purple) for your attribute, but want to get the label for it. Below code will help you to achive it.
$_product = Mage::getModel('catalog/product');
$attr = $_product->getResource()->getAttribute("color");
if ($attr->usesSource()) {
echo $color_label = $attr->getSource()->getOptionText("10");
}
Reminder of the Value of Deployment Automation
Some time back I came across an article “5 things Amazon taught me about deployment automation.” It does not say anything radically new, but gives some good examples of why deployment automation (including quality automation) is good. Oh, and this is *nothing* about how Amazon does deployment automation – they just use the end-user Amazon experience as parallels to deployment automation.
“When you make [deployment] cheap an easy … you change behavior in radical ways.
- …There’s a surprising difference between having a web page where someone can easily request a deployment and automatically deploying to test as the result of a new build or a nightly schedule. On event/schedule gets has a bigger impact over self-service… this helps turn CI into CD
- It’s about effort, not speed. [Making testing easy is more important than making testing fast … most of the time]
- When deployments are a big, scary…
View original post 128 more words