Archive for the ‘Magento Development’ Category

Add Magento Admin Account Using MySQL Script

Thursday, January 21st, 2010

Note: this script works on Magento CE 1.3.2.4 and 1.4.0.* but does not work in the Magento Enterprise Edition

As a Magento development company, we have a number of designers, developers, and project managers that need admin access to the sites that we build. As opposed to using one global admin account for our entire team, we believe it to be a much better practice to assign individual admin accounts to each team member who is going to be working on a project.

One of our developers, Matt Johnson, has developed a script to allow us to easily add an admin user to a Magento installation. Each member on our team that needs Magento admin access has a sql file that adds their admin account to Magento. When we start a new project, we run the sql script for the members of our team that are going to need access to the server. I hope you’ll find this snippet useful.

/* This is an example SQL script.
You should replace all the UPPERCASED variables.
The <USERNAME> variable can only be alphanumeric characters, and probably underscores (haven't tested)
 
You can either generate a password hash using the PHP code below,
or you can grab a password hash from the admin_user table from an
existing Magento install for which you know the admin password.
*/
 
/* Use the following PHP script to generate a salted password hash. You can also use a straight md5 hash, but it's much more easily brute-forced
<?php $password = 'PASSWORD'; $salt = 'GF'; echo $hash = md5($salt.$password).':'.$salt; ?>
*/
 
insert into admin_user
select
(select max(user_id) + 1 from admin_user) user_id,
'FIRST NAME' first_name,
'LAST NAME' last_name,
'EMAIL' email,
'USERNAME' username,
'HASH EXAMPLE: 178f29c8e4c6c801db90cd171e3b2b53:in' password, /* You can replace this value with an md5 hash */
now() created,
NULL modified,
NULL logdate,
0 lognum,
0 reload_acl_flag,
1 is_active,
(select max(extra) from admin_user where extra is not null) extra;
 
insert into admin_role
select
(select max(role_id) + 1 from admin_role) role_id,
(select role_id from admin_role where role_name = 'Administrators') parent_id,
2 tree_level,
0 sort_order,
'U' role_type,
(select user_id from admin_user where username = 'USERNAME') user_id,
'USERNAME' role_name

Posted in Magento Development | 12 Comments »

Enable/disable template/block Hints Using MySQL

Friday, January 8th, 2010

When doing frontend development with Magento, there are many times when I want to turn on template/block hints for just one page load. I don’t like having to wait on the Magento admin to load up the System > Configuration > Developer page, switch to Website view, and then turn on template/path hints. I have written a couple of MySQL snippets that allow me to enable/disable hints with one query.

I wrote the scripts so that for clean Magento installs, they will insert the necessary records into the core_config_data table. For Magento installs that have had the hints enabled/disabled, the record will already be present in the config table, so the script will just update that record.

Enable template/block hints for the first* website

-- Enable template hints
SET @template_hints = 1;
INSERT INTO `core_config_data` ( `scope`, `scope_id`, `path`, `value`) VALUES ('websites','1','dev/debug/template_hints', @template_hints) ON DUPLICATE KEY UPDATE `value`=@template_hints;
-- Enable block hints
SET @template_hints_blocks = 1;
INSERT INTO `core_config_data` ( `scope`, `scope_id`, `path`, `value`) VALUES ('websites','1','dev/debug/template_hints_blocks', @template_hints_blocks) ON DUPLICATE KEY UPDATE `value`=@template_hints_blocks;

Disable template/block hints for the first* website

-- Enable template hints
SET @template_hints = 0;
INSERT INTO `core_config_data` ( `scope`, `scope_id`, `path`, `value`) VALUES ('websites','1','dev/debug/template_hints', @template_hints) ON DUPLICATE KEY UPDATE `value`=@template_hints;
-- Enable block hints
SET @template_hints_blocks = 0;
INSERT INTO `core_config_data` ( `scope`, `scope_id`, `path`, `value`) VALUES ('websites','1','dev/debug/template_hints_blocks', @template_hints_blocks) ON DUPLICATE KEY UPDATE `value`=@template_hints_blocks;

As I covered in this post, it is possible to turn on hints at a global level, which activates hints for all website and for the admin panel. This is useful if you’re making customizations to the admin panel, or if you have multiple websites for which you’re wanting to activate/deactivate hints at the same time. The following two snippets modify hints at a global level:

Enable template/block hints for the admin panel and ALL websites

-- Enable template hints
SET @template_hints = 1;
INSERT INTO `core_config_data` ( `scope`, `scope_id`, `path`, `value`) VALUES ('default','0','dev/debug/template_hints', @template_hints) ON DUPLICATE KEY UPDATE `value`=@template_hints;
-- Enable block hints
SET @template_hints_blocks = 1;
INSERT INTO `core_config_data` ( `scope`, `scope_id`, `path`, `value`) VALUES ('default','0','dev/debug/template_hints_blocks', @template_hints_blocks) ON DUPLICATE KEY UPDATE `value`=@template_hints_blocks;

Disable template/block hints for the admin panel and ALL websites

-- Enable template hints
SET @template_hints = 0;
INSERT INTO `core_config_data` ( `scope`, `scope_id`, `path`, `value`) VALUES ('default','0','dev/debug/template_hints', @template_hints) ON DUPLICATE KEY UPDATE `value`=@template_hints;
-- Enable block hints
SET @template_hints_blocks = 0;
INSERT INTO `core_config_data` ( `scope`, `scope_id`, `path`, `value`) VALUES ('default','0','dev/debug/template_hints_blocks', @template_hints_blocks) ON DUPLICATE KEY UPDATE `value`=@template_hints_blocks;

* If you have multiple websites, you’ll need to update the ‘1′ value set for the website field to reflect the ID of your website. If you have multiple websites, you can just turn on hints globally using the last two SQL snippets.

Posted in Magento Development | No Comments »

Initial Magento Setup Development Tips

Wednesday, December 30th, 2009

Over the past year and a half of developing Magento sites, our development team has created a standard set of modifications/tweaks that we make to every Magento installation.

1. Automatically turn on developer mode based on domain.

We have a stage site setup for all of our projects at stage..com. We also develop projects locally at either .dev or .local (based on developer preference). This snippet of code will only enable error reporting and Magento’s developer mode on stage or local domains.

Insert the following code after “require_once $mageFilename;” in index.php

# If the domain is ***.dev, ***.local or stage.**** then run site in developer mode and turn on error reporting
if(in_array(substr($_SERVER['HTTP_HOST'], -4), array('.dev', 'ocal') )
  || strstr($_SERVER['HTTP_HOST'], 'stage.')
){
	Mage::setIsDeveloperMode(true);
	ini_set('display_errors', 1);
}

2. Enable logging

Magento has a built-in logging method that allows you to log any variable type to a log file. This is very helpful when building Magento modules, as you can easily inspect data structures, without having to open a debugging session to inspect the variables in local scope. By default, logging is turned off in Magento. To enable logging, go to the “Developer” tab on the “System > Configuration” page. Change the “Enabled” select list under the “Log Settings” section to “Yes” and then save the page.

You can log variables to the Magento log using the following code: Mage::log($variable); By default, logs are stored in var/log/system.log

Here are some example usages of Mage::log()

# Log data from a Model
$model = Mage::getModel('catalog/product')->load(1);
Mage::log($model->getData());
 
# Log data from a Collection
$collection = Mage::getResourceModel('catalog/product_collection')->addAttributeToSelect('*')->load();
Mage::log($collection->getItems());
 
# Log data from array
$array = array('bar' => 'foo', 1, 2,);
Mage::log($array);

If you’re developing on a Mac, I’d recommend opening the system.log file with the Console app. If you’re on a *nix based machine, you “tail” the latest contents of the log file using the following bash command:
tail -f

/var/system.log


3. Enhance error backtraces

All of our developers use xdebug as a part of their Apache configuration. Xdebug has two main benefits: (1) It allows us to use PDT to debug PHP applications. (2) It overrides the default PHP error messages with detailed, fully customizable error backtraces. You can see an example backtrace below:

Xdebug Error Backtrace

This is what that error would look like without xdebug:

Generic Magento Error

Magento has built-in error and exception handling. Since errors are being handled by Magento, we have to modify a core file to make Magento let PHP/xdebug handle the displaying of the messages.

In a future blog post, will cover how to make Magento let xdebug handle exceptions

In app/code/core/Mage/Core/Model/App.php, replace the setErrorHandler() method with the setErrorHandler() method below:

    public function setErrorHandler($handler)
    {
        // HACK: Magento should only handle errors if developer mode is off
	if(!Mage::getIsDeveloperMode())
        	set_error_handler($handler);
	// END HACK
        return $this;
    }

Modifying core Magento files is never recommended, but if Magento gets upgraded and this change gets overridden, there won’t be an issue, since it’s not critical for the site to function.

4. Enhance exception backtraces

In this Enabling Xdebug’s Exception Handler in Magento blog post you can read about how to modify Magento to let Xdebug handle exceptions.

Summary

Hopefully these few tips will help you in your Magento development.  If you have any general tips for Magento development, I’d love to hear about them.

Posted in Magento Development | 2 Comments »

Shortening Increment ID length for Orders, Invoices, and Shipments

Saturday, October 3rd, 2009

We recently had a client who needed to shorten the default Magento Increment ID length from the default 9 characters (eg 100000000) to 6 characters.  This can be accomplished by modifying the “increment_pad_length” in the “eav_entity_type” for whatever entity you’re wanting to modify.  Here is a screenshot of the resulting table:

magento_shorten_increment_id


If you’re wanting to change the number from which Magento starts counting orders/invoices/shipments, or wanting to change the prefix for those entities, check out this great post by Timothy at Elias: http://eliasinteractive.com/blog/magento-ecommerce-how-to-reset-all-test-order-information-and-set-unique-prefix-for-orders-invoices-shipments-and-credit-memos/

Posted in Magento Development | 2 Comments »

Custom Admin Theme

Saturday, July 11th, 2009

Our Objective

If you need to make adjustments to the adminhtml theme files (template, layout, or skin), you have two options for accomplishing this.

Here’s an example of what we would want to accomplish:
Files in
/app/design/adminhtml/default/custom_admin_theme/
need to override files in:
/app/design/adminhtml/default/default/

Likewise, files in
/skin/adminhtml/default/custom_admin_theme/
need to override files in:
/skin/adminhtml/default/default/

The following two options accomplish this functionality:

Option 1 – “Admin Theme” module

Ivan Weiler from Inchoo has created a module that allows you to specify your own custom theme using the same config page you use to specify custom frontend themes. You can download this module here (note: this module is currently in Alpha, so you’ll need to modify the setting of your downloader before being able to download it). Here is a screenshot of this module in action:

"Admin Theme" Screenshot

Option 2 – Add XML to your module’s config file

If you don’t want to install a module just to enable this functionality, you can add the following XML code inside the tag inside the config.xml file of any of your active modules. Alternatively, you can add this XML to your /app/etc/local.xml file. I prefer this option, due to its simplicity and the fact that the store administrator will have no need to update the adminhtml theme from the admin panel.

<config>
...
    <stores>
        <admin>
            <!-- custom admin theme -->
            <design>
                <theme>
                    <default>custom_admin_theme</default>
                </theme>
            </design>
        </admin>
    </stores>	
...
</config>

The code above overrides the XML from the /app/code/core/Mage/Adminhtml/etc/config.xml lines 410-422

410
411
412
413
414
415
416
417
418
419
420
421
422
    <stores>
        <admin>
            <!-- default admin design package and theme -->
            <design>
                <package>
                    <name>default</name>
                </package>
                <theme>
                    <default>default</default>
                </theme>
            </design>
        </admin>
    </stores>

Posted in Magento Development | No Comments »

Enable Template/Block Hints in Admin Panel

Friday, July 10th, 2009

Anyone that has developed a theme in Magento knows how helpful template/block hints are. They help quickly identify which files are being loaded for a specific page.

Magento’s admin panel uses the exact same design pattern as the frontend (layouts + blocks + templates). If you’ve ever done any modifications to the Magento admin panel, you’ve probably tried to turn on template/block hints for the admin panel. The only problem is, Magento doesn’t have built-in support for this. I did some digging around and found out how to enable this feature in the admin panel.

Step 1 – Connect to database

Using your favorite database administration tool, connect to your Magento database. These are tools I’ve used and recommend: Navicat ($129), MySQL Query Browser (Free), Sequel Pro (Mac Only – Free), or phpMyAdmin (free).

Step 2 – Enter values into ‘core_config_data’ table

Run the following query on the Magento database:

INSERT INTO core_config_data (scope, scope_id, path, value)
VALUES ('default', 0, 'dev/debug/template_hints', 1),
('default', 0, 'dev/debug/template_hints_blocks', 1);

Step 3 – Test in admin panel

Once you make this addition to the core_config_data database, template/block hints should show up in the admin panel. Here is a screenshot of the CMS Page with hints turned on:

Magento Admin - Hints Turned On

Magento Admin - Hints Turned On

Disabling Hints

When you’ve finished development want to turn off template hints in the admin panel, open the core_config_data table and change the ‘value’ column of the two row you inserted to “0″.

Why does this work?

For those of you who are like me and want to know the “why” as well as the “how”, I’m going to go over why this works. Here is the Magento code that checks to see if template/blocks hints are enabled:

File: /app/code/core/Mage/Core/Block/Template.php (Magento 1.3.2)
109
110
111
112
113
114
115
116
117
118
public function getShowTemplateHints()
{
    if (is_null(self::$_showTemplateHints)) {
        self::$_showTemplateHints = Mage::getStoreConfig('dev/debug/template_hints')
            && Mage::helper('core')->isDevAllowed();
        self::$_showTemplateHintsBlocks = Mage::getStoreConfig('dev/debug/template_hints_blocks')
            && Mage::helper('core')->isDevAllowed();
    }
    return self::$_showTemplateHints;
}

The “Mage::getStoreConfig” method checks for config values that are set in the current scope (ie, default, website, store, store view). In the admin panel, only values set in the “Default Scope” are loaded.

Magento only allows you to turn on hints when you’re in the configuration scope of a Website or Store View. This means that when the code above tries to load the configuration, it returns “null” because that config value isn’t set in the “Default Config” scope. Running the MySQL query above adds the hint config values to the “Default Config” scope. Here is a screenshot showing the settings for turning on the hints – notice that the “Main Website” configuration scope is selected.

Magento Config Scope Selection

Magento Config Scope Selection

Conclusion

I hope this helps you in your development of Magento modules that integrate with the admin panel. I considering writing a module to enable the hints to be turned on in the “Default Scope”, but decided against it, due to the simplicity of the method outlined in this post.

Posted in Magento Development | 7 Comments »

Call us at 417-597-3397, email us at sales@classyllama.com, or use this form to contact us:

  1. (required)
  2. (required)
 

cforms contact form by delicious:days

Full builds (design and development) start at $70k