Programming

Magento Hello world module (extension)

Magento Hello world module (extension)

Previously I already wrote how to build custom module (extension) in Magento. Here is a new, refreshed article on that subject.

We will start by creating a simple “Hello world” module. However, you will soon see that simple takes a new meaning with Magento. Creating a bare bone module requires at least two files in Magento. For your module to work you need /app/etc/modules/MyCompany_MyModule.xml, app/code/local/MyCompany/MyModule/etc/config.xml. But, barebone module will not give you a “Hello developer” πŸ™‚ -So, we need to add few more files to the game.

Read more

Programatically create Magento blocks and inject them into layout

Programatically create Magento blocks and inject them into layout

Imagine a scenario where you wish to simply create a view file like custom-note.phtml and show this view file on some new url inside the Magento store. One way to do this is to create a CMS page and call that block from within CMS page. But what if you wish to create and append this block to other areas of Magento layout that are inaccessible from CMS page interface form admin? What if I want to add new div element under the breadcrumbs and append new block under it?

Read more

Magento module development, building interface with config file and reusing config values

Magento module development, building interface with config file and reusing config values

Huh, this is a long title. To bad I could not fit extra more into it :). Extending admin interface in Magento can be a tedious task. Unlike WordPress where you have a thing or two to setup then the rest of it comes to good old HTML forms and stuff, Magento requires completely different approach. Magento uses xml configuration files to build parts of admin interface needed for your custom module.

In this article I will show you how to “understand and read” existing config.xml files in order to build interface and at the end, how to read the values stored in admin backend. After all, that’s the point of admin interface, you setup some stuff, write some values, then you use those values across your site where certain functionality is needed.

Read more

Magento, price helper code snippet

Magento, price helper code snippet

Here is a little code snippet as requested from one of my readers :). Hope you find it useful.

<?php
$_coreHelper = $this->helper(’core’);
$_taxHelper = $this->helper(’tax’);
$_finalPriceInclTax = $_taxHelper->getPrice($_product, $_product->getFinalPrice(), true);
$_finalPriceInclTaxNicelyFormated = $_coreHelper->currency($_finalPriceInclTax,true,false);
 
echo $_finalPriceInclTaxNicelyFormated;
?>
Read more

Access denied – Magento module error

Access denied – Magento module error

I believe that most of you building custom Magento module came across this error “Access denied” when trying to view their module output in Magento administration. I just lost around an hour trying to find my way around this one. Note, this is far from my first Magento module, however it still got me thinking and sweeting. So I thought I’ll share the pain πŸ™‚

Main issue (or super cool feature) with modules is that most of the config.xml errors die peacefully, meaning you see no meaningful error description on page. You either see stuff like “Access denied” or you see blank page or you see god know what.

Read more

Add breadcrumbs in Magento to pages that miss them

Add breadcrumbs in Magento to pages that miss them

Here is one solution on how to add breadcrumbs to pages in Magento that by default do not have breadcrumbs. Recently we had a client that requested a basic breadcrumb to be shown on pages that Magento does not serve breadcrumbs.

<?php 
/**
 *
 * CUSTOM BREADCRUMBS
 *
 * Adds url breadcrumbs for pages that do not have breadcrumbs by default
 *
 */
 
?>
<?php if(is_null($crumbs)): ?>
<?php 
 
/**
 * NOTE
 * On some servers use ->getServer('PATH_INFO')
 * and on some ->getServer('ORIG_PATH_INFO')
 */
 
$urlRequest = Mage::app()->getFrontController()->getRequest();
$urlPart = $urlRequest->getServer('ORIG_PATH_INFO');
 
if(is_null($urlPart))
{
    $urlPart = $urlRequest->getServer('PATH_INFO');
}
 
 
$urlPart = substr($urlPart, 1 );
$currentUrl = $this->getUrl($urlPart);
 
//$controllerName = Mage::app()->getFrontController()->getRequest()->getControllerName();
//$controllerName = ucfirst($controllerName);
 
$controllerName = str_replace("/", " ", $urlPart);
$controllerName = str_replace("_", " ", $controllerName);
$controllerName = str_replace("-", " ", $controllerName);
$controllerName = ucfirst($controllerName);
 
?>
<span class="breadcrumbs">
<strong class="float"><?php echo $this->__("You're currently on: ") ?></strong>
<ul class="breadcrumbs">
<li class="home">
        <a title="<?php echo $this->__('Go to Home Page') ?>" href="<?php echo $this->getUrl() ?>"><?php echo $this->__('Home') ?></a>
    </li>
    <li> / </li>
        <li class="<?php echo strtolower($controllerName) ?>">
        <strong><?php echo $this->__($controllerName) ?></strong>
    </li>
</ul>
</span>
<?php endif; /* END OF CUSTOM BREADCRUMBS */ ?>
Read more

WordPress and Magento integration – One way to go

WordPress and Magento integration – One way to go

Lot of us in Magento related development have an issue of connecting their Magento with other CMS solutions out there. Lot of our clients like and want WordPress alongside Magento. WordPress is a great platform when it comes to publishing and managing articles. Not to mention the SEO stuff. One of the biggest issue with integrating two systems in PHP is the lack of the namespaces. This leads to function name redeclaration issues and so on. Integrating something into something is such a loosely term, depending on level of integrations… If you ask me πŸ™‚

Read more

Show Magento products by month and year

Show Magento products by month and year

For the last two days, I have been working on a “gallery like” approach to Magento. One of stores we are developing requires it. Below is a code that lists moths, from January to December, showing number of products for each month. You can reuse function to list products for any given month. Just extract the code from within the function and place it inside some foreach loop and deal with product object. This way you can have one block listing months with appropriately generated links and clicking that link leads you to another page that loads products for given month and year.

Read more

Making use of Magento getSingleton method

Making use of Magento getSingleton method

In one of my previous articles I showed you how to use getModel and getData methods in Magento. Although we should not put those to methods at the same level, since I’d say the getModel is a bit more higher. Higher in sense, you first use the geModel to create the instance of object then you use getData to retrieve the data from that instance. I have said it before, and I’m saying it again; Magento is a great peace of full OOP power of PHP. It’s architecture is something not yet widely seen in CMS solutions.

One of the architectural goodies of Magento is it’s Singleton design pattern. In short, Singleton design pattern ensures a class has only one instance. Therefore one should provide a global point of access to that single instance of a class.

Read more

Retrieving product information in Magento

Shop Supplies Product Info

If you are developing Magento template sooner or later you will need to obtain some product information and rearrange it to fit your template. One of the most useful function while working with Magento is Mage::getModel($modelClass).

Here is how you would use it to retrieve Magento product information Mage::getModel(‘catalog/product’);.

This ‘catalog/product’ relates to app/code/core/Mage/Catalog/Model/Product.php file. If you open that file you will see it’s actually a class named Mage_Catalog_Model_Product. That same class is extending the Mage_Catalog_Model_Abstract class meaning it inherits all of it’s methods.

Read more

Figuring out Magento object context

Figuring out Magento object context

One of the problems working under the hood of the Magento CMS is determining the context of $this. If you are about to do any advanced stuff with your template, besides layout changes, you need to get familiar with Magento’s objects (classes).

Let’s have a look at the /app/design/frontend/default/default/template/catalog/product/view.phtml file. If you open this file and execute var_dump($this) your browser will return empty page after a short period of delay. By page I mean on the product view page; the one you see when you click on Magetno product. Experienced users will open PHP error log and notice the error message caused by var_dump().

Read more