Magento – Custom module with custom database table and per store settings

configurationcontact-usdatabasemodule

I am creating a custom module with a custom database table using the following tutorial.

This module is a custom contact form, and I would like the settings for that custom contact form to be on a per store basis, meaning, I want the send from email address to be different for each store I create.

Do I need to add specific fields to the database for Magento to pick up on this? Or does Magento do this automagically?

Best Answer

You could use the existing behavior of Magento which allows to define and get a contact email address for each store view and depending on the department (sales, customer support, general). You can then use theses specific contact per store view in your custom module.

Here is the configuration backend for email addresses: backend email addresses

And to use this specific email in your custom module, the important part in your case is the $sendervariable. Here is a code sample (check the comment in it, be aware that it is taken from a code of me, so adapt to your needs):

$recipient = $item->getRecipient();
$template = $item->getTemplate();

if (!Mage::getConfig()->getNode(Mage_Core_Model_Email_Template::XML_PATH_TEMPLATE_EMAIL .'/' . $template)){
    Mage::throwException(Mage::helper('customer')->__('Wrong transactional notification email template.'));
}

$customer = Mage::getModel('customer/customer')->load($customer->getId());
$recipient = array('name' => $customer->getName(), 'email' => $customer->getEmail());
    /**
     * $senderType can be support, general, sales, custom1, custom2
     *  You can also replace the array by a string and provide only the string 'general', 'support', etc
     */
    $sender = array(
        'name' => Mage::getStoreConfig('trans_email/ident_' . $senderType . '/name', $storeId),
        'email' => Mage::getStoreConfig('trans_email/ident_' . $senderType . '/email', $storeId)
    );
}

$translate = Mage::getSingleton('core/translate');
$translate->setTranslateInline(false);

/* @var $emailTemplate Mage_Core_Model_Email_Template */
$emailTemplate = Mage::getModel('core/email_template');

if (Mage::app()->getStore()->isAdmin()) Mage::app()->getLocale()->emulate($storeId);

$variables += array(
    'name' => $customer->getName()
);

if (Mage::app()->getStore()->isAdmin()) Mage::app()->getLocale()->revert();

$emailTemplate->setDesignConfig(array('area' => 'frontend', 'store' => $storeId))
    ->sendTransactional($template, // xml path email template
        $sender,
        $recipient['email'],
        $recipient['name'],
        $variables
    );

$translate->setTranslateInline(true);
Related Topic