Can Magento send email to admin, when user place order ? - php

Can Magento send email to admin, when user place order ?
It is necessary to send information about placed order to admin’s email
admin notification should have another template

Yes it can you can set all orders to be bcc -d from
system > configuration > sales > sales emails

I hacked core code to do this on my Magento install. The 1st level of properly editing core files is to override them in app/code/local somewhere...
Make your admin_order_notify_email template, save it, and note its ID. Mine was 8. Oh, and to get access to the customer's email address, use this code in the template: {{var order.getCustomerEmail()}}. This vexed me for months. :P My next trick will be to barcode the order number in the admin order notification email.
Now, open the file app/code/core/Mage/Sales/Model/Order.php
<?
$mailTemplate = Mage::getModel('core/email_template');
/* #var $mailTemplate Mage_Core_Model_Email_Template */
//chris near line 854: $copyTo = $this->_getEmails(self::XML_PATH_EMAIL_COPY_TO);
$copyMethod = Mage::getStoreConfig(self::XML_PATH_EMAIL_COPY_METHOD, $this->getStoreId());
if ($copyTo && $copyMethod == 'bcc') {
foreach ($copyTo as $email) {
//chris $mailTemplate->addBcc($email);
}
}
//chris near line 900: added this to use admin email template for new orders. Note it is hard coded to template 8, which I added
$mailTemplate->setDesignConfig(array('area'=>'frontend', 'store'=>$this->getStoreId()))
->sendTransactional(
8,
Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $this->getStoreId()),
$this->_getEmails(self::XML_PATH_EMAIL_COPY_TO),
"MyBusinessName Orders",
array(
'order' => $this,
'billing' => $this->getBillingAddress(),
'payment_html' => $paymentBlock->toHtml(),
)
);
?>

The crm4ecommerce extension is encrypted, and cannot be audited for security.
Another free option is Inchoo Admin Order Notifier.
"Magento extension that enables email notifications towards various emails when customer places the order. Useful when you want your personell notified that some customer just placed the order. Supports transactional email."
From: https://github.com/ajzele/Inchoo_AdminOrderNotifier

Related

add recipients to "Customer invoice / Order details" in Woocommerce emails

In Woocommerce, there is already a built-in way to add recipients to the "New Order" , "Failed", and "Cancelled" emails, but for some reason, the "Customer invoice / Order details" don't allow any recipient other than the customer.
There is a simple plugin that allows for that, but it is very limited in features.
Any guidance for how to add recipients to this email?
I plan to use a Code Snippet to call the hook/filter/action and then tell it explicitly which email addresses to use. I intend to write the script so that whatever user initiates the action, will be CC'ed on that email. Presumably the same function that can do the first part of my question can help me to check current user and will grab their email address.
Any help is appreciated
I don't know what buil-in functionality are you talking about. But you can use the below filter to add recipients to the invoice email. You don't need any plugin, just use this below code snippet in your active theme/child theme functions.php
add_filter("woocommerce_email_recipient_customer_invoice", "add_recipient_to_email", 10, 2);
function add_recipient_to_email( $recipients, $object ){
$new_email = "newemail#domain.com"; //New email Id
$recipients = $recipients.','.$new_email;
return $recipients;
}

WooCommerce - How can I stop the New Order email being sent to admin recipient if a certain shipping method is used?

I have added a custom shipping option which works great, however duplicate emails are sent - my custom shipping new order email and the standard admin new order email.
In WooCommerce > Settings > Email I have added a new option for my custom shipping and added recipients and so on. What I think I need to do now is put a condition in to the Admin New Order section, something along the lines of..
if(shipping method == 'Custom Shipping')
return; // this can just bail out as a custom shipping email is being sent
I think this will stop the standard new order email being sent if the order uses Custom Shipping.
Can anyone give me guidance on how I can implement this? Do I put code in to the admin-new-order.php file with this if statement? Thank you
You could remove the standard mail actions: https://docs.woocommerce.com/document/unhookremove-woocommerce-emails/
And then intercept the order_status_changed hook to trigger your mail depending on conditions (like the shipping method): https://docs.woocommerce.com/wc-apidocs/class-WC_Order.html
function your_function($id)
{
$order = wc_get_order($id);
if (!empty($order) && $order->get_shipping_method() === 'Custom Shipping') {
do_action('woocommerce_before_resend_order_emails', $order, 'email_name');
// prepare your email
do_action('woocommerce_after_resend_order_email', $order, 'email_name');
}
}
add_action('woocommerce_order_status_changed', 'your_function');

Wordpress: Output custom user_meta data in registration notification

In WordPress, I have custom registration site for new users. Upon registration, there is an optional checkbox to subscribe to our newsletter. As far as I understand it, it adds the value of the checkbox to the user_meta table (the whole thing has been coded by a company in India, which I woould very much prefer to not involve again, since they delayed their work time and time again and didn't do good work after all).
The corresponding code snippet in my child theme's functions.php looks like this:
<?php echo '<div class="form-row form-row-wide">
<input type="checkbox" name="inter_offers" value="yes"> '; _e('I would like to subscribe to the Kurth Electronic newsletter.','kurth-child'); echo '<br></div>';
return $fields; ?>
add_action('woocommerce_created_customer','adding_extra_reg_fields');
function adding_extra_reg_fields($user_id) {
extract($_POST);
update_user_meta($user_id, 'billing_inter_offers',$inter_offers);
} ?>
(I have left out lines irrelevant to this issue.)
Now, this value is saved internally, but not displayed to me. I would like to show the value in an E-Mail or notification generated by WordPress when the user completes registration, so that we can manually add them to our newsletter list whenever someone chooses to subscribe to the newsletter. The problem is that I only have a limited knowledge of PHP and I don't know where to start.
I should also note that this is not done via the standard WorPress registration form, but by a WooCommerce registration form (I have disabled the standard WordPress registration for security reasons).
I tried using the "Better Notifications" plugin (https://wordpress.org/plugins/bnfw/) for custom notifications whenever a new user completes their registration, but it ignores any php code that I add to the custom notifications body to display the user_meta data.
Any help would be appreciated.
Because the registration is done via woocomerce you may have to look for a notification PlugIN that works with woocomerce, the one you have may just work properly with wordpress core version!
You also could generate a mail via php in the function, so that you get a message with the user mail adress, but i think without php knowledge it is not that easy to use the built in php mailer... (You may need an api there!)
But wouldn't it be better to automatically sign them into your newsletter software? For example for Mailchimp or other systems like that there are quite good wordpress plugins!
You may also be able to include the forms of these PlugIns in your registration form, but without a closer look at this woocomerce registration i can't tell for sure!
I think this will do the trick, it will notify you every time a new user is created and also tell you if they subscribed or not.
function new_customer_registered_send_email_admin($user_login, $user_email) {
ob_start();
do_action('woocommerce_email_header', 'New customer registered');
$email_header = ob_get_clean();
ob_start();
do_action('woocommerce_email_footer');
$email_footer = ob_get_clean();
$user = get_user_by( 'email', $user_email );
$subscribed = get_user_meta( $user->ID, 'billing_inter_offers', true );
woocommerce_mail(
get_bloginfo('admin_email'),
get_bloginfo('name').' - New customer registered',
$email_header.'<p>The user '.esc_html( $user_login ).' created an account ' . ( $subscribed ? ' and subscribed to the newsletter.' : '.' ) . '<br>Email:'.esc_html( $user_email ).'</p>'.$email_footer
);
}
add_action('new_customer_registered', 'new_customer_registered_send_email_admin', 10, 2);
I ended up using the plugin amr users to generate a userlist of all users who had a certain metadata tag set to a certain value (in my case, if they want to recieve a newsletter - the previous developers never bothered to make the data actually readable without extra effort). It is a little clunky to use and not what I originally intended, but it got the job done.

Magento only sends order confirmation emails when manually prompted to

A Magento website I'm maintaining stopped sending order confirmations and/or invoices all of a sudden.
Nothing has been changed in the email settings and they're all complying with common Magento guidelines. I've tried sending a test email through the following php script to make sure it's not caused by any server settings:
<?php
$to = "me#domain.com";
$subject = "Test email";
$message = "This is a test email.";
$from = "testing#anothertest.test";
$headers = "From:" . $from;
if (mail($to, $subject, $message, $headers)) {
echo("Your message has been sent successfully");
} else {
echo("Sorry, your message could not be sent");
}
?>
The test message got delivered flawlessly.
I installed the SMTP Pro Email plugin to see if changing settings there would manage a result; nothing. The plugin offers the ability to send a test email, which again gets delivered without flaw.
Even stranger, clicking the 'Send Email' button in the Magento backend for an order does deliver the email. Signing up for a newsletter does result in a subscription confirmation request email. Clearly, the website can send mails, it just somehow doesn't get triggered to do so for order order confirmations and invoices.
And yes, both
System > Configuration > Sales > Sales Emails > Order > Enabled
and
System > Configuration > Sales > Sales Emails > Invoice > Enabled
are set to Yes and include a BCC address.
Similarly,
System > Configuration > Advanced > System > Mail Sending Settings > Disable Email Communications
is set to No.
I'm running Magento CE 1.6.2 on this site, so it's not the common cron job problem version 1.9 seems to cause for a lot of folk.
I'm racking my brain here, does anybody have a clue?
Still haven't found what causes the problem, but added some code to succes.phtml to force the mail to be sent upon loading that page:
<?php
$order = new Mage_Sales_Model_Order();
$incrementId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order->loadByIncrementId($incrementId);
try
{
$order->sendNewOrderEmail();
} catch (Exception $ex) { }
?>
It's not the most elegant solution, but at least the customers get their order confirmation through this workaround.
I am using version 1.8.1. I have been fighting with this same problem for 2 weeks now. The simple solution came by me "enabling" every option in System > Configuration > Advanced. All the new order confirmations get sent out immediately now to the person placing the order.
Background: After installing and configuring the new Magento Cart, I noticed that emails were only going through if I manually sent them through the admin screens. This told me that the server SMTP settings were correct, but I was wondering why a person placing an order would not receive the confirmation email...ever. After much searching and reading I thought that it had to do with one of the other functions of the Magento cart. So I went into the system confuration under the advanced tab (System > Configuration > Advanced) and found that I had disabled certain things like Sendtofrined, Newsletter, etc. Not wanting to waste time at the moment, I simply went through the entire list and enabled everything. Now I have the coupon function, add to wish list function and newsletter stuff that I do not want to see in the cart, but the emails are going through immediately and reliably. I will now attempt to find which function is related to the order confirmation email by process of elimination. But this did the trick for me and I hope it helps someone else out there, without having to add code to the php files.

Set subscriber status in Magento programmatically

I am trying to write a module that syncs my newsletter subscribers in Magento with a external database. I need to be able to update the subscription status in Magento programmatically but I am having diffuculty getting the "setStatus" method in Magento to work. It does not throw any errors but the code does not seem to have any effect. Below is the code where I call the method:
$collection = Mage::getResourceModel('newsletter/subscriber_collection')->showStoreInfo()->showCustomerInfo();
foreach ($collection as $cust) {
$cust->setStatus(1);
}
In theory, this should set the status of all of my subscribers to "subscribed". I could optionally change the argument sent to "setStatus" to any of the below ints for a different status.
1: Subscribed
2: Status Not Active
3: Unsubscribed
How to best change the subscriber status or get this code working?
Here an import script:
<?php
require_once("./app/Mage.php");
Mage::app();
$subscribers = array('email1#server1.com', 'email2#server2.com');
foreach ($subscribers as $email) {
# create new subscriber without send an confirmation email
Mage::getModel('newsletter/subscriber')->setImportMode(true)->subscribe($email);
# get just generated subscriber
$subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
# change status to "subscribed" and save
$subscriber->setStatus(Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED);
$subscriber->save();
}
?>
It seems that newsletter subscribers are also stored elsewhere. What you are setting is just a check in the customer base for some other use.
You need to do the following for each customer as well.
Mage::getModel('newsletter/subscriber')->subscribe($email);
See this link for a complete reference.
Thanks to the link #Ozair shared I was able to figure out what I needed to do.
I was successfully setting the status of the subscriber in the Magento subscriber object but I was not saving the object. I needed to call Magento's save method so it would call the ORM and write it to the database. All I need to do was add
$cust->save();
in the for loop. Below is the whole code snippet.
$collection = Mage::getResourceModel('newsletter/subscriber_collection')->showStoreInfo()->showCustomerInfo();
foreach ($collection as $cust) {
$cust->setStatus(1);
$cust->save();
}
I Hope this helps someone in the future. I needed it for a Constant Contact - Magento Synchronization extension I was making: http://www.freelunchlabs.com/store/constant-contact-and-magento-sync.html

Categories