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);
$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(),

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

$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.


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',
$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





or via OptionInstance

$optionInstance = $product->getOptionInstance();

But it doesn’t work, I finally use this


To delete product options, there is several way too

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

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

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.

Magento create observers on custom entities

If you need to create an observer on your custom entities, just follow these few steps:

In this case, we will create an observer on the before save event of the tarif entity.

On your module config.xml file, declare your observer, this will bind your hook event to your class/method


tarif_save_before means your observer will trigger on the before save event of the tarif entity.
To find out which entity to use, you can add (temporary) a mage::log() on this class : app/code/core/Mage/Core/Model/Abstract.php

 protected function _beforeSave()
        if (!$this->getId()) {
        Mage::dispatchEvent('model_save_before', array('object'=>$this));
        Mage::log('entity to use: '.$this->_eventPrefix.'_save_before');
        Mage::dispatchEvent($this->_eventPrefix.'_save_before', $this->_getEventData());
        return $this;

adin_marketplace is my module.
adin_marketplace/observer class is the class file to use (app/code/local/Adin/Marketplace/Model/Observer.php )
adinMarketplaceTarifSaveBefore is the method inside my class.

Now, create your class and your method : app/code/local/Geophyle/Marketplace/Model/Observer.php

class Adin_Marketplace_Model_Observer {
public function adinMarketplaceTarifSaveBefore($observer)
    $event = $observer->getEvent();
    $tarif = $event->getTarif();
    //do some stuff...

And that’s all.
You change an .xml file, so don’t forget to clear your cache.