Magento add order history

There is several ways to add order history, with less or more data

Quick way :

 $message = 'Comment to add in order history';
 $order->addStatusHistoryComment($message);
 $order->save();

With all informations :

$history = Mage::getModel('sales/order_status_history')
  ->setStatus($order->getStatus())
  ->setComment('Comment to add in order history')
  ->setEntityName(Mage_Sales_Model_Order::HISTORY_ENTITY_NAME)
  ->setIsCustomerNotified(false)
  ->setCreatedAt(date('Y-m-d H:i:s', time()));

  $order->addStatusHistory($history);
  $order->save();

Magento – Emulate store

It’s that simple :

$appEmulation = Mage::getSingleton('core/app_emulation');
 
//Start environment emulation of the specified store
$initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId);

 
//Stop environment emulation and restore original store
$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);

Magento – edit breadcrumb from block

To edit a breadcrumb from the block, you can simply do that :

class Adin_Sales_Block_Invoice_View extends Mage_Core_Block_Template
{

    private $_mainOrder;

    public function _prepareLayout()
    {
        if ($breadcrumbs = $this->getLayout()->getBlock('breadcrumbs')) {
            $breadcrumbs->addCrumb('All orders', array('label'=>$this->__('All my invoices'), 'title'=>$this->__('All my invoices'), 'link'=>$this->getUrl('sales/invoice/history')));
            $breadcrumbs->addCrumb('My order', array('label'=>$this->__('My invoices : order %s', $this->_mainOrder->getIncrementId()), 'title'=>$this->__('My invoices : order %s',$this->_mainOrder->getIncrementId()), ));
        }
        return parent::_prepareLayout();
    }

$_mainOrder is defined in the __contruct() function

Magento – empty $_FILES while uploading files in adminhtml

You made a form in adminhtml with upload file but in the controller the $_FILES var is hopelessly empty ?
You probably forgot to add “‘enctype’ => ‘multipart/form-data’,” on your form declaration

example :
file : /app/code/local/Adin/Shipping/Block/Adminhtml/Delivery/Cost/Import/Edit/Form.php

protected function _prepareForm()
{
$form = new Varien_Data_Form(array(
            'id'        => 'edit_form',
            'action'    => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
            'method'    => 'post',
            'enctype'   => 'multipart/form-data',
        ));

 $fieldset->addField('fichier', 'file', array(
            'label'     => $helper->__('Data file'),
            'value'	=> 'upload',
            'name'  => 'fichier',
            'after_element_html'     => $helper->__('<small>File from carrier</small>'),
            'required'  => true,
        ));

Magento – The Order State “complete” must not be set manually.

If you want to update order status & state programmaticaly, you try this

$order->setStatus(Mage_Sales_Model_Order::STATE_COMPLETE);
$order->setState(Mage_Sales_Model_Order::STATE_COMPLETE);

And you get this error :

The Order state 'complete' must not be set manually.

To fix it, use this :

$order->addStatusToHistory(Mage_Sales_Model_Order::STATE_COMPLETE, "Comments");
$order->setData('state', Mage_Sales_Model_Order::STATE_COMPLETE);

Magento add block into cms page and add permissions

If you want to add a block into cms page, you just have to add this into its content

{{block type="adin_page/page_html_combos" name="combos" template="page/html/combos.phtml"}} 

Of course, don’t forget to create the block class app/code/local/Adin/Page/Block/Page/Html/Combos.php

class Adin_Page_Block_Page_Html_Combos extends Mage_Core_Block_Template
{
//...
}

and the .phtml file
app/design/frontend/adin/default/template/page/html/combos.phtml

But when you want to render the page, nothing.
On the logs (system.log), you have : Security problem: adin_page/page_html_combos has not been whitelisted.
It’s a new feature since securoty Patch SUPEE-7405 and Magento CE 1.9.2.2, you have to add your block into whitelist. You can do it manually, in backoffice, System > Permissions > Blocks or do it programmatically :

$blockNames = array(
    'adin_page/page_html_combos'
);
foreach ($blockNames as $blockName) {
    $whitelistBlock = Mage::getModel('admin/block')->load($blockName, 'block_name');
    $whitelistBlock->setData('block_name', $blockName);
    $whitelistBlock->setData('is_allowed', 1);
    $whitelistBlock->save();
}

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