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.

  1. etc/events.xml
  2. etc/frontend/events.xml
  3. 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

<?xml version=“1.0” encoding=“UTF-8”?>
<config xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:noNamespaceSchemaLocation=“../../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd”>
    <event name=“checkout_cart_add_product_complete”>
        <observer name=“techbuzz_custom_checkout_cart_add_product_complete” instance=“Techbuzz\Custom\Observer\CompleteObserver”  />
    </event>
</config>
ok, so here the in events tag, name is given as the event name. In the observer tag we have
1. name  = name of the observer – which should be unique
2. instance = class name which will contain this observer
So, in my module i.e. Techbuzz\Custom\Observer
<?php
namespace ‘Techbuzz\Custom\Observer’
use Magento\Framework\Event\ObserverInterface;
class CompleteObserver implements ObserverInterface
{
/**
* @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

Magento create admin user programmatically

Santosh's Blog

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

View original post

Get Product attribute’s select option Id or Lable or Value in Magento

Pradeep Sanku

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");
}

View original post

Reminder of the Value of Deployment Automation

Alan Kent's Blog

robot-toySome 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.

  1. …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
  2. It’s about effort, not speed. [Making testing easy is more important than making testing fast … most of the time]
  3. When deployments are a big, scary…

View original post 128 more words