Magento event to check if customer have subscribed to newsletter
Check if the customer has subscribed to the newsletter from registration page or checkout page by using event observer. You may need to take some action programatically if customer subscribes to newsletter, below code will help you exactly in that.
Code to put in your config.xml
1 2 3 4 5 6 7 8 | <newsletter_subscriber_save_after>
<observers>
<namespace_module_model_observer>
<class>Namespace_Module_Model_Observer</class>
<method>subscribedToNewsletter</method>
</namespace_module_model_observer>
</observers>
</newsletter_subscriber_save_after> |
Code to put in your Observer.php file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class Namespace_Module_Model_Observer {
public function subscribedToNewsletter(Varien_Event_Observer $observer)
{
$event = $observer->getEvent();
$subscriber = $event->getDataObject();
$data = $subscriber->getData();
$email = $data['subscriber_email'];
$statusChange = $subscriber->getIsStatusChanged();
if ($data['subscriber_status'] == "1" && $statusChange == true) {
//code to handle if customer is just subscribed...
}
}
} |
Magento event observer for customer registration success
If you are looking for how to execute some code when customer successfully sign up in your website, you can use below code to check if the registration was successful. Note, this will NOT check if customer was registered from checkout page, if you are looking for that please go to this post.
In your xml file:
1 2 3 4 5 6 7 8 9 10 11 | <events>
<customer_register_success>
<observers>
<namespace_module_customer_register_success>
<type>singleton</type>
<class>Namespace_Module_Model_Observer</class>
<method>customerRegisterSuccess</method>
</namespace_module_customer_register_success>
</observers>
</customer_register_success>
</events> |
And in your Observer.php file:
1 2 3 4 5 6 7 8 9 10 | class Namespace_Module_Model_Observer {
public function customerRegisterSuccess(Varien_Event_Observer $observer) {
$event = $observer->getEvent();
$customer = $event->getCustomer();
$email = $customer->getEmail();
if($email) {
//code to handle if customer is successfully registered
}
}
} |
Magento check if customer registered in checkout page
If you want to check if the customer is guest, registered or just register to the site when they place the order, below script will help you identify that in success.phtml file.
You can find success / order confirmation phtml file at:
app/design/frontend/[package]/[theme]/template/checkout/success.phtml
Just at the end of this file put below lines of code:
1 2 3 4 5 6 7 8 9 10 11 12 | <?php $order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
$quoteId = $order->getQuoteId();
$quote = Mage::getModel('sales/quote')->load($quoteId);
$method = $quote->getCheckoutMethod(true);
$customer_email = $order->getCustomerEmail();
if ($method == 'register'){ ?>
//code to handle if customer just registered to your site
<?php } elseif($method == 'guest') {?>
//code to handle if customer is guest
<?php } else { ?>
//code to handle for logged in customer
<?php } ?> |
In the same file, success.phtml, you can request for order number, customer email, customer id, subtotal, grandtotal, order ID just created etc. like this:
1 2 3 4 5 6 7 8 | $_customerId = Mage::getSingleton('customer/session')->getCustomerId();
$lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getSingleton('sales/order');
$order->load($lastOrderId);
$_totalData =$order->getData();
$_sub = $_totalData['subtotal'];
$_orderEmail = $_totalData['customer_email'];
$_orderNumber = $_totalData['increment_id']; |
Magento add attribute to customer
In Magento to add an attribute to customer is not an option in the admin panel like it does have for Product attribute. So you have to end up writing the script that will add your custom attribute in customer’s EAV tables.
Below code will insert your custom customer attribute in Magento system. You can even specify whil creating the attribute whether that attribute should appear in the forms (like register/signup) or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | $installer = $this;
$installer->startSetup();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$entityTypeId = $setup->getEntityTypeId('customer');
$attributeSetId = $setup->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
$setup->addAttribute('customer', 'your_attribute_code_here', array(
'input' => 'text', //or select or whatever you like
'type' => 'int', //or varchar or anything you want it
'label' => 'Attribute description goes here',
'visible' => 1,
'required' => 0, //mandatory? then 1
'user_defined' => 1,
));
$setup->addAttributeToGroup(
$entityTypeId,
$attributeSetId,
$attributeGroupId,
'your_attribute_code_here',
'100'
);
$oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'your_attribute_code_here');
$oAttribute->setData('used_in_forms', array('adminhtml_customer'));
$oAttribute->save();
$setup->endSetup(); |
Here we have used our custom attribute’s:
– input type as text, but you can have it anything you like to appear in the form. It can be textarea, date, select or anything you want.
– data type as int, but you can have it anything from text, datetime, varchar or decimal. Remember customer is EAV in Magento, so it needs this information to store all the future values of this attribute in customer_entity_int (customer_entity_*) table.
Magento join EAV collection with Flat table
Joining tables in Magento when it comes to EAV with Flat table is quite complicated. Consider you want to join sales_flat_order table with customer EAV tables to get Customer’s firstname and lastname, it becomes difficult as customer’s name comes from customer_entity_varchar table.
Below code will join sales order flat table with customer EAV to get customer’s full name in the collection along with all the order details.
1 2 3 4 5 6 7 8 9 10 11 12 13 | $coll = Mage::getModel('sales/order')->getCollection();
$fn = Mage::getModel('eav/entity_attribute')->loadByCode('1', 'firstname');
$ln = Mage::getModel('eav/entity_attribute')->loadByCode('1', 'lastname');
$coll->getSelect()
->join(array('ce1' => 'customer_entity_varchar'), 'ce1.entity_id=main_table.customer_id', array('firstname' => 'value'))
->where('ce1.attribute_id='.$fn->getAttributeId())
->join(array('ce2' => 'customer_entity_varchar'), 'ce2.entity_id=main_table.customer_id', array('lastname' => 'value'))
->where('ce2.attribute_id='.$ln->getAttributeId())
->columns(new Zend_Db_Expr("CONCAT(`ce1`.`value`, ' ',`ce2`.`value`) AS fullname"));
print_r($coll->getData()); |
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- ChatGPT (1)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (2)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OpenAI (1)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)