Automatically sending emails in Magento - php

I'm helping someone set up a Magento shop which was bought from another company. The shop allows users to upload items and users can message each other on the site. It all works very nicely, except that a user doesn't receive an email notification when he receives a new message.
How would I go about implementing the sending of an automatic email each time a user receives a new message?
Thanks

You could add an event observer. You have code somewhere that takes care of the messages sent by the users. I'm sorry I cannot be more specific but it sounds like the site has a custom made module so I couldn't tell you where to look.
Once you find the function where the messaging magic happens, in there, you could dispatch your own event like so
Mage::dispatchEvent('user_message_sent_after');
Then, in the custom module, you look for the config.xml file (it should be in the etc/ folder). There you add something like
<frontend>
<events>
<user_message_sent_after>
<observers>
<your_name_for_this_tag>
<type>singleton</type>
<class>Packagename_Modulename_Model_Observer</class>
<method>sendEmailNotification</method>
</your_name_for_this_tag>
</observers>
</user_message_sent_after>
</events>
</frontend>
If there is an events tag already, you add to it rather than making a second one. 'user_message_sent_after' is not a default Magento event. I made up the name, you could use any other name, as long as it's equal to the name you used in your dispatchEvent call.
Define an email template. In the app\locale\yourlanguage_yourcountrycode\template\email\ folder, add a file called message_notification
Hi {{var name}},<br>
{{var sender}} has sent you a message!<br>
Bye
Next, you finally add the actual observer. Find the model folder in your custom module and add a file called Observer.php. In this file you add something like
<?php
class Packagename_Modulename_Model_Observer {
public function sendEmailNotification($observer) {
// add emailing functionality
}
Old but still very useful and basic explanation on how to send email is here: http://inchoo.net/ecommerce/magento/magento-custom-emails/

Related

Use PDO request in Magento 1.9 in the event "customer_register_success"

I'm a french student (sorry for my bad English) and I have a problem with one thing in Magento. I would like to insert a customer, when he created his account, in a custom table named 'membre' in my database (of Magento).
So I created an Observer and the methods for, and when I test, I have an error HTTP 500 and in the logs file, I have few pieces of information.
But when I refresh the page the customer is registered but in my database I have nothing....
In my local module I have these files :
/www/app/code/local/Magentix/Gestion/etc/config.xml
config.xml file
/www/app/code/local/Magentix/Gestion/Model/Observer.php
Observer.php File
I'm lost and I don't understand what it is bad or not..
If you want more information, said it and I will try my best for realized that
Thanks.

How to rewrite custom module url including id with any custom url in Magento

I have installed storelocator in Magento that is free extension.But if it is clicked on View Details button, it is opened with storelocator/index/view/id/1/. For SEO purpose, it is needed to open with hearing-aid-center-in-new-york or hearing-aid-center-in-Chicago. here it will be common with hearing-aid-center-in- and new-york, chikago will be coming dynamic. So, for this requirement, how to rewrite this url. And Where I should write the rewrite the code for the above requirement. I had tried a lot with id 1 as static way by creating observer using Magento event "controller_front_init_routers" by the help of this site https://magento.stackexchange.com/questions/73369/custom-url-for-magento-extension. It is saved in core_url_rewrite and it it shows in "URL Rewrite Management" section and it also redirects, but redirects 404 Not Found page.I can't understand why it redirects 404 Not Found page and how to implement it and how to get id of storelocator in overserver dynamic way.
Besides it, I also need to override the customer account url and sales order history url.
I don't know how to rewrite the customer account url with /my-account/order-history instead of /sales/order/history/ and also with /my-account/dashboard instead of /customer/account/.I can't understand how to implement these requirement for friendly SEO purpose. Any help, Thanks.
You need to extend Store locator with a new module that depends on it. There are loads of posts out there on how to do this. This module will need to add an additional attribute for URL Key e.g. new-york. It would also need a new Controller that extends Storelocator_IndexController. This controller will handle all requests for Stores. It will lookup for the Store by URL Key instead of ID. Once it has this it will continue using the inherit code as before. Of course you will have to override and update all Urls to use the Key.
Lastly you would need a rewrite rule in the config.xml like:
<config>
<global>
<rewrite>
<mymodule_storelocatoroverides_view>
<from><![CDATA[#^\/hearing\-aid\-center\-in\-([a-z\-]+)[\/]?$#]]></from>
<to><![CDATA[mymodule/storelocatoroverides/view/id/1/]]></to>
<complete>1</complete>
</mymodule_storelocatoroverides_view>
</rewrite>
</global>
</config>
Alternatively
As a quick solution but by no means the best solution:
If you have a finite number of stores, then you can simple add rewrite rules in a module config.xml (create a simple plugin) E.g for New York (assuming its ID 1):
<config>
<global>
<rewrite>
<mymodule_storelocatoroverides_newyork>
<from><![CDATA[#^\/hearing\-aid\-center\-in\-new\-york[\/]?$#]]></from>
<to><![CDATA[storelocator/index/view/id/1/]]></to>
<complete>1</complete>
</mymodule_storelocatoroverides_newyork>
</rewrite>
</global>
</config>
Again you would have to override the Url outputs.
As for the Customer Url's you can use the same approach again. So add Rewrites to the config.xml and replace Url links to use your new rewrites. Again you may have to override Core code to change the redirects.
Hope that helps!

How can I make duplicates of authorize.net module in magento?

I've been stuck on this for a couple of days now. For some reason, duplicating the Magento Authorize.net (Mage_Paygate) module just doesn't work on the front-end. I've managed to duplicate the module and have it show up with its own configuration options in the back-end, but I can't get the payment method to show up on front-end during check out.
So far I've tried copying the module and changing every single instance of the model names to avoid conflict, I've done this multiple times from scratch so I'm confident that it's not an issue of an invalid class call...
There's some kind of magic button that I'm missing. I'm really hoping to hear from someone who's successfully duplicated a core payment module, specifically Authorize.net
I need to create duplicates for accounting purposes. There is no other solution than to duplicate the authorize.net modules.
Thanks
EDIT: Some insight into why I need multiple copies of the authorize.net module: My company processes high volume sales, more than most merchant services will allow. The idea is that we will have several authorize.net modules pre-loaded with multiple authorize.net API credentials that each correspond to a different merchant, so that when one of the merchants caps out we can disable that payment method and enable the next one.
The reason I can't just switch the information in a single authorize.net module is that we need the funds for each credit card to be deposited into separate bank accounts - ECC, the software we use to transfer data from magento to quickbooks, accepts the payment method based on the Title field. In order to differentiate which credit card processor we are transferring funds for, we append a (ACRONYM) to the title field. When this title field changes, ECC poops itself because it suddenly doesn't know where the funds came from for the transactions it's downloading.
We've tried various things and feel that making copies of an authorize.net module would be the best solution. Thanks.
I managed to figure it out, here's what I did in case anyone else runs into this, it's very handy to be able to duplicate payment and shipping modules that follow different rules without having to rely on store view changes:
Create new module Namespace_Modulename , I used Mage_Paygate1 until my concept was proven.
Copy the etc folder from the module you'd like to duplicate
Copy the main model file, in my case Mage/Paygate/Model/Authorizenet.php
Edit the system.xml:
change group name so that a new block of configuration options will appear in the admin panel config, leave the module="" alone. We're going to refer to the original module files rather than copy them. I used authorizenet1.
Change the label node if you'd like, for clarity. In my case I named my copies Authorize.net (processor name)
Edit config.xml:
Module name, change to Namespace_Modulename, I used Mage_Paygate1.
Make sure there is a blocks declaration if the original config.xml didn't have one. We want to refer to the blocks folder in the original module, ie:
<blocks>
<directpost><class>Mage_Paygate_Block</class></directpost>
</blocks>
change the node in the bottom under
<default><section> (<default><payment>) to the group name used in system.xml
(authorizenet1)
Make sure the model node is correctly pointing to the new module, not the old. (paygate1/authorizenet)
Now edit the new model, (Authorizenet.php):
rename the Class to match the new Namespace_Modulename (Mage_Paygate1)
Make sure the $_code equals your group name from the config.xml file (authorize1)
That's what worked for me. The idea is to only copy the pieces that change the module's configuration and just recycle all of the original code.
There's probably an even cleaner way to do this but I'm no magento master by any means, so I very much welcome any advice on how to future-proof this sort of hack.
Along with below code we need to change the model also, in config.xml. if you not make below change new payment method will not displayed in checkout
<models>
<authorizenet1>
<class>Mage_Paygate1_Model</class>
</authorizenet1>
</models>

Magento new user registration - inject own code

I'm trying to make a loose connection between a Magento installation and another application. In particular, there are only two things I care about at the moment - customers and their purchases. What I edited thus far is the file app/code/core/Mage/Customer/controllers/AccountController.php - function createPostAction() is augmented to send the customer's info to the other application. Another modification was made in success.phtml file to send out the details of the order once it's placed.
The problem I'm having is that if a user just places items in a cart (as a guest) and then registers as part of the checkout process - the customer's record in the second application is never created. It only works if the user first explicitly registers, and then checks out separately.
I suppose AccountController.php may be the wrong file to modify to achieve my desired result, which file should I use instead?
Edit: I am not including the code samples, as it's largely irrelevant - the problem is not with the code, but with the fact that it's apparently in the wrong place. Where would be a good place to add custom code which should run when a new customer is registered?
This is a bit tricky to do cleanly in Magento but possible to achieve. I've had the same task a while ago and solved using observers
First you need to create an observer which listens controller_action_postdispatch event:
<events>
<controller_action_postdispatch>
<observers>
<yourmodule_anything>
<type>singleton</type>
<class>yourmodule/observer</class>
<method>someMethod</method>
</yourmodule_anything>
</observers>
</controller_action_postdispatch>
</events>
Then in your observer method you can check action names as follows
<?php
$action = $observer->getEvent()->getControllerAction();
if ($action->getFullActionName() == 'customer_account_createpost') {
if (Mage::getSingleton('customer/session')->isLoggedIn()) { // this check allows to determine if customer session was created which means successfull registration
}
}
Something similar can be done for checkout registration. In general observers are great (and proper) way to extend Magento functionality. Read more about them here
Anton S is right - you should check if Magento is firing an event when this occurs. If so, your code should respond to that event.
If that is not the case, or you need custom logic to run before the account is created, please consider overriding/overloading the controller using a custom module: http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/how_to_overload_a_controller
This keeps the original core code intact. It essentially tells Magento to use your custom controller instead. If the method it needs to execute is not found in your controller, it will use the code from the core instead.

Custom Magento Order Field and the Magento Database

Alright, so I've successfully added a custom order field to my page layout in magento. I require a "dealer" from which the user has to select a "dealer state" and then I use ajaxt to pull in all the dealers from that specific state into another field called "dealerid". The value of this field is in fact the dealer id in my database. My question is kind of a full one.
First, I need to add a field to the "order table" that saves the dealer id for the specific order and secondly I need to actually have magento take this field from the form and insert it. How do I go about doing these things? Mainly, what table would magento store this dealerid and what files do I modify to actually have magento access and insert this field value?
Note: I am using the one page checkout method only.
First of all, familiarize yourself with magento SQL update mechanism (its diffrent when your dealing with EAV - like customer tables - and regular tables - like orders). Create a module which will have a sql-update script in /YourNamespace/YourModule/sql/yourmodule_setup/mysql4-install-1.0.0.php for instance. You should use something like:
$installer = $this;
$installer->getConnection()->addColumn($installer->getTable('sales/order'), 'dealerid', 'decimal(12,4) NULL');
and in your config.xml:
<global>
(...)
<models>
(...)
<yourmodule>
<class>Mage_Sales_Model</class>
<resourceModel>yourmodule_mysql4</resourceModel>
</yourmodule>
<yourmodule_mysql4>
<class>YourNamespace_Yourmodule_Model_Mysql4</class>
<entities>
<order><table>sales_order</table></order>
</entities>
(...)
</yourmodule_mysql4>
(...)
</models>
<resources>
<yourmodule_setup>
<setup>
<module>YourNamespace_YourModule</module>
<class>YourNamespace_YourModule_Model_Mysql4_Setup</class>
</setup>
</yourmodule_setup>
</resources>
(...)
</global>
Remember to create your own classes: YourNamespace_YourModule_Model_Mysql4_Setup, YourNamespace_Yourmodule_Model_Mysql4. Check in app/code/core/Mage/Sales/ how they should look like (nothing special needed there, they just must inherit from proper core classes). Also check out the app/code/core/Mage/Sales/sql and app/code/core/Mage/Sales/etc/config.xml.
If you want to get a field from the onepage checkout, you must do a couple of things... I don't know what you really need from the user (and in what part of checkout its going to be taken). Adding an input is done by the templates (edit proper files in app/design/frontend/yourskin/default/template/checkout).
To capture the right data you must rewrite the checkout controller whit your own controller action (look at the app/code/core/Mage/Checkout/Controllers/OnepageController.php) to get the data from the form and store it in the checkout session. After that you must hook to the checkout_type_onepage_save_order_after event (its in the app/code/core/Mage/Checkou/Model/Type/Onepage.php), take the data from your session and put it in the Order model (and save it of course). Its kind of a standard procedure, so you shouldn't have a problem.
Its possible that you will also have add a column to the sales_flat_order (the structure of order tables changed in 1.4.x.x, so I'm not sure of it - you will have to do some experimenting).
If you want to see it in the adminhtml order view, you will have to override some Core/Adminhtml/Blocks/Sales/Order (and templates probably).
Its kind of a complex procedure, so you'll have to figure out some things by yourself. This post is kind of a general idea how it should be done.

Categories