0

I want to develop a module in which i want to call my api from every page of magento website How can i achieve that ?

I have tried with observer but unable to do that can any one give a solution for printing simple message on every page to website(including admin) at module level.

  • 2
    you have to be more specific than that. Give a scenario you are trying to achieve. – Marius Jun 22 '16 at 8:23
  • @Marius Just i want that print a simple message on each and every page because i developed a module that is not directly integrated to my whole website like i have achieved in drupal – Arpit Jun 22 '16 at 8:33
  • where do you want that message displayed? Above the content, above the whole page? somewhere specific? – Marius Jun 22 '16 at 8:46
  • @Marius above the whole page – Arpit Jun 22 '16 at 8:54
2

Magento already displays some messages above the whole page like when javascript is not enabled or when the store is in "demo" mode.

You can rewrite the block that displays those messages and do your own work in there.

Something like this.
Create a new module. Let's call it StackExchange_Notice.
You will need the following files.

app/etc/modules/StackExchange_Notice.xml - the declaration file

<?xml version="1.0"?>

<config>
    <modules>
        <StackExchange_Notice>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Page />
             </depends>
        </StackExchange_Notice>
    </modules>
</config>

app/code/local/StackExchange/Notice/etc/config.xml - the configuration file

<?xml version="1.0"?>
<config>
    <modules>
        <StackExchange_Notice>
            <version>1.0.0</version>
        </StackExchange_Notice>
    </modules>
    <global>
        <blocks>
            <page>
                <rewrite>
                     <html_notices>StackExchange_Notice_Block_Html_Notices</html_notices>
                </rewrite>
            </page>
            <adminhtml>
                <rewrite>
                    <page_notices>StackExchange_Notice_Block_Adminhtml_Notices</page_notices>
                </rewrite>
            </adminhtml>
        </blocks>
        <models>
            <stackexchange_notice>
                <class>StackExchange_Notice_Model</class>
            </stackexchange_notice>
        </models>
    </global>
    <adminhtml>
        <layout>
            <updates>
                <stackexchange_notice>
                    <file>stackexchange_notice.xml</file>
                </stackexchange_notice>
            </updates>
        </layout>
    </adminhtml>     
    <frontend>
        <layout>
            <updates>
                <stackexchange_notice>
                    <file>stackexchange_notice.xml</file>
                </stackexchange_notice>
            </updates>
        </layout>
    </frontend>       
</config>

app/code/local/StackExchange/Notice/Block/Html/Notices.php - the rewrite of the frontend notice block.

<?php 
class StackExchange_Notice_Block_Html_Notices extends Mage_Page_Block_Html_Notices
{
    public function getMessageToDisplay()
    {
        $messageModel = Mage::getModel('stackexchange_notice/message');
        return $messageModel->getMessage();
    }
}

app/code/local/StackExchange/Notice/Block/Adminhtml/Notices.php - the rewrite of the adminhtml notice block.

<?php 
class StackExchange_Notice_Block_Adminhtml_Notices extends Mage_Page_Block_Html_Notices
{
    public function getMessageToDisplay()
    {
        $messageModel = Mage::getModel('stackexchange_notice/message');
        return $messageModel->getMessage();
    }
}

app/code/local/StackExchange/Notice/Model/Message.php - the model that does the api calls

<?php 
class StackExchange_Notice_Model_Message 
{
    public function getMessage()
    {
        //make api call here and return the message
    }
}

app/design/frontend/base/default/layout/stackexchange_notice.xml - the frontend layout file - replaces the frontend template file for the notices.

<?xml version="1.0"?>
<layout>
    <default>
        <reference name="global_notices">
            <action method="setTemplate">
                 <template>stackexchange_notice/notices.phtml</template>
            </action>
        </reference>
    </default>
</layout>

app/design/frontend/base/default/template/stackexchange_notice/notice.phtml - the new template for notices.

<?php
/**
 * @var $this StackExchange_Notice_Block_Html_Notices
 */
?>
<?php if ($this->displayNoscriptNotice()): ?>
    <noscript>
        <div class="global-site-notice noscript">
            <div class="notice-inner">
                <p>
                    <strong><?php echo $this->__('JavaScript seems to be disabled in your browser.'); ?></strong><br />
                    <?php echo $this->__('You must have JavaScript enabled in your browser to utilize the functionality of this website.'); ?>
                </p>
            </div>
        </div>
    </noscript>
<?php endif; ?>
<?php if ($this->displayDemoNotice()): ?>
    <div class="global-site-notice demo-notice">
        <div class="notice-inner"><p><?php echo $this->__('This is a demo store. Any orders placed through this store will not be honored or fulfilled.') ?></p></div>
    </div>
<?php endif; ?>
<!-- above is copied from original file -->
<!-- below is new -->
<?php if ($message = $this->getMessageToDisplay()) : ?>
    <div class="global-site-notice demo-notice">
        <div class="notice-inner"><p><?php echo $message?></p></div>
<?php endif;

app/design/adminhtml/default/default/layout/stackexchange_notice.xml - the admin layout file - replaces the frontend template file for the notices.

<?xml version="1.0"?>
<layout>
    <default>
        <reference name="global_notices">
            <action method="setTemplate">
                 <template>stackexchange_notice/notices.phtml</template>
            </action>
        </reference>
    </default>
</layout>

app/design/adminhtml/default/default/template/stackexchange_notice/notice.phtml - the new admin template for notices.

<?php
/**
 * @see StackExchange_Notice_Block_Adminhtml_Notices
 */
?>
<?php if ($this->displayNoscriptNotice()): ?>
    <noscript>
        <div class="noscript">
            <div class="noscript-inner">
                <p><strong><?php echo $this->__('JavaScript seems to be disabled in your browser.'); ?></strong></p>
                <p><?php echo $this->__('You must have JavaScript enabled in your browser to utilize the functionality of this website.'); ?></p>
            </div>
        </div>
    </noscript>
<?php endif; ?>
<?php if ($this->displayDemoNotice()): ?>
    <p class="demo-notice"><?php echo $this->__('This is a demo store. Any orders placed through this store will not be honored or fulfilled.') ?></p>
<?php endif; ?>
<!-- above is copied from the original template -->
<!-- below is extension specific -->
<?php if ($message = $this->getMessageToDisplay()) : ?>
    <p class="demo-notice"><?php echo $message?></p>
<?php endif;

Clear the cache and it should work.
I haven't tested the code so watch out for syntax errors.

|improve this answer|||||
  • Thank you :) really great working !! but i was doing with observer can we achieve with that also ? – Arpit Jun 22 '16 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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