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?

up vote 3 down vote accepted

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

Magento will add these automagically when you define them in your modules system.xml file.

<some_setting>
    <label>Some Setting Label</label>
    <frontend_type>text</frontend_type>
    <sort_order>1</sort_order>
    <show_in_default>1</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>1</show_in_store>
</some_setting>

You then retrieve the config value for the store in question via...

Mage::getStoreConfig( '/path/to/setting', $store_id );

app/Mage.php

public static function getStoreConfig($path, $store = null)
{
    return self::app()->getStore($store)->getConfig($path);
}

Which, in turn, calls...

app/code/core/Mage/Core/Model/Store.php

public function getConfig($path)
{
    if (isset($this->_configCache[$path])) {
        return $this->_configCache[$path];
    }

    $config = Mage::getConfig();

    $fullPath = 'stores/' . $this->getCode() . '/' . $path;
    $data = $config->getNode($fullPath);
    if (!$data && !Mage::isInstalled()) {
        $data = $config->getNode('default/' . $path);
    }
    if (!$data) {
        return null;
    }
    return $this->_processConfigValue($fullPath, $path, $data);
}

Notice how it's determining the path to the config value based upon the store code here...

$fullPath = 'stores/' . $this->getCode() . '/' . $path;

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.