Magento – Adding additional store addresses

Magento allows you to define 5 email addresses in backoffice, to add some, create (or use an existing) system.xml in a new (or existing) module like this

<config>
    <sections>
        <trans_email>
            <groups>
                <ident_relance translate="label">
                    <label>Relances</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>10</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <fields>
                        <email translate="label">
                            <label>Sender Email</label>
                            <frontend_type>text</frontend_type>
                            <backend_model>adminhtml/system_config_backend_email_address</backend_model>
                            <validate>validate-email</validate>
                            <sort_order>2</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </email>
                        <name translate="label">
                            <label>Sender Name</label>
                            <frontend_type>text</frontend_type>
                            <backend_model>adminhtml/system_config_backend_email_sender</backend_model>
                            <validate>validate-emailSender</validate>
                            <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>
                        </name>
                    </fields>
                </ident_relance>
            </groups>
        </trans_email>
    </sections>
</config>

In this case, this will create 2 entries in the core_config_data table : trans_email/ident_relance/email and trans_email/ident_relance/name.

To retrieve values, use this :

Mage::getStoreConfig('trans_email/ident_relance/email');
Mage::getStoreConfig('trans_email/ident_relance/name');

Magento – add something in the header section

If you need to add something in the header section of all your pages, you can go on your template and add it to the files 1column.phtml, 2columns-left.phtml, 2columns-right.phtml… You can, but you shouldn’t, it’s not a good practice.

Let’s see the content of one of this file

<head>
<?php echo $this->getChildHtml('head') ?>
</head>

You can see the section will be filled with all “head” child block, so let’s create one.

On your layout file (design/frontend/adin/default/layout/adin_page.xml), add the declaration of your new block

<layout version="0.1.0">
    <default>
        <reference name="head" before="-">
            <block type="adin_page/headerstuff" name="headerstuff" as="headerstuff" template="page/html/headerstuff.phtml" />
        </reference>
   </default>
</layout>

Create the block class in app/code/local/Adin/Page/Block/Headerstuff.php

class Adin_Page_Block_Headerstuff extends Mage_Core_Block_Template
{
    public function _construct()
    {
    }
}

And create you template file app/design/frontend/adin/default/template/page/html/headerstuff.phtml

<!-- header my stuff to put in the header -->

Magento – all you need about website / store / view

// Gets the current store's details
$store = Mage::app()->getStore();
 
// Gets the current store's id
$storeId = Mage::app()->getStore()->getStoreId();
 
// Gets the current store's code
$storeCode = Mage::app()->getStore()->getCode();
 
// Gets the current website's id
$websiteId = Mage::app()->getStore()->getWebsiteId();
 
// Gets the current store's group id
$storeGroupId = Mage::app()->getStore()->getGroupId();
 
// Gets the current store's name
$storeName = Mage::app()->getStore()->getName();
 
// Gets the current store's sort order
$storeSortOrder = Mage::app()->getStore()->getSortOrder();
 
// Gets the current store's status
$storeIsActive = Mage::app()->getStore()->getIsActive();
 
// Gets the current store's locale
$storeLocaleCode = Mage::app()->getStore()->getLocaleCode();
 
// Gets the current store's home url
$storeHomeUrl = Mage::app()->getStore()->getHomeUrl();

//load store by code or id
$store = Mage::getModel('core/store')->load($code); //you can use code or id

magento create tier prices programmatically

Here a simple script to create tier prices programmatically

$product = Mage::getModel('catalog/product')->load(904);
$product->setPrice($tarif->getPrix());
    
$tier_price = array();
foreach($tarif->getQtyDetails() as $detail)
{
   $tier_price[] = array(
     'cust_group'    => 32000,
     'website_id'    => 0,
     'all_groups'    => 1,
     'price_qty'     => $detail->getMin(),
     'price'         => $detail->getPrix(),
     'website_price' => $detail->getPrix(),
  );
}
$product->setTierPrice($tier_price);

Magento assign attribute to attribute set

This is how to assign an existing attribute to an existing attribute set


$setup = new Mage_Catalog_Model_Resource_Eav_Mysql4_Setup('core_setup');
$setup->startSetup();

$attribute_set_name = ATTRIBUTE_SET_VISIT;
$attribute_code = $attr;
$group_name = GROUP_ADIN;

$attribute_set_id=$setup->getAttributeSetId('catalog_product', $attribute_set_name);
$attribute_group_id=$setup->getAttributeGroupId('catalog_product', $attribute_set_id, $group_name);
$attribute_id=$setup->getAttributeId('catalog_product', $attribute_code);

$setup->addAttributeToSet($entityTypeId='catalog_product',$attribute_set_id, $attribute_group_id, $attribute_id);

Magento product options create and delete

Here, some of my code to manipulate Magento product options.

Creation

You first need an array with the options you want to save

$values = array();
foreach($options as $option)
{
    $values[] = array(
        'title' => $option['name'],
        'price' => $option['price'],
        'price_type' => 'fixed',
        'sku' => $option['sku'],
        'sort_order' => $x,
        'is_delete' => '0',
        );
    $x++;
}
$newOption = array(
            'title' => "Public",
            'type' => 'drop_down',
            'is_require' => 1,
            'sort_order' => 20,
            'is_delete' => '',
            'previous_type' => '',
            'previous_group'    => '',
            'values' =>$values,
        );

Then add them to your product, I have seen a lot of different way to do it

$product->setCanSaveCustomOptions(true);
$product->getOptionInstance()->addOption($newOption);
//or
$product->getOptionInstance()->setOptions($newOption);

$product->getOptionInstance()->saveOptions();

$product->setHasOptions(1);
$product->setRequiredOptions(1);

$product->save();

or via OptionInstance

$optionInstance = $product->getOptionInstance();
$optionInstance->setOptions($newOption);
$optionInstance->setProduct($product);
$optionInstance->->saveOptions();

But it doesn’t work, I finally use this

$product->setProductOptions(array($newOption));
$product->setCanSaveCustomOptions(true);
$product->save()

To delete product options, there is several way too

$oldOptions = $product->getOptionInstance()->getOptions();
foreach ($oldOptions as $key => $option){
       $oldOptions[$key]['is_delete'] = 1;
}
$product->getOptionInstance()->setOptions($oldOptions);
$product->getOptionInstance()->saveOptions();

//or 
$optionInstance = $product->getOptionInstance()->unsetOptions();

But it doesn’t work too, so I use :

$options = Mage::getModel('catalog/product_option')->getCollection()->addFieldToFilter("product_id", $product->getId());
foreach($options as $option)
{
   $option->delete();
}

Some more informations:
To see options on admin, your product entity (table catalog_product_entity) should have attribute (column) has_options set to 1.
Options are saved in tables : catalog_product_option, catalog_product_option_price, catalog_product_option_title, catalog_product_option_type_price, catalog_product_option_type_title and catalog_product_option_value.