Access Magento admin session from frontend

Next time I want to show something in the Magento frontend just for admin users, Alan Storm’s module Magento_CrossAreaSessions will come in handy! This is a topic that caused me headache in the past. Read more at Magento Quickies or get the module from GitHub: Magento_CrossAreaSessions

Background: Magento separates adminhtml and frontend sessions strictly, so it is not a trivial task to access the backend session on a frontend page.

The module allows reading of raw session data and processing of ACL rules, which is good enough for most cases.

Free Icons and Online Icon Generator Tools

Need some icons for your (web) app but have nobody with Mad Photoshop Skills™ at hand? These three sites will help you:

Iconfinder.net

My first icon resource on the web is www.iconfinder.net. It is a very large database of icons and icon sets in various formats and sizes. They are licensed differently, so not all sets are free but even for free commercial use there are plenty. And you can filter by license.

Iconfinder.com Icon Download Database

ICONSDB.com Icon Customizer

Icons DB has thousands of completely free, monochromatic customizable icons. If you need some quick simple flat icons you probably find them here. The clou: you can chose to download all of them in any color and any size as ICO, PNG, GIF, JPG or ICNS

Iconsdb.com Online Icon Generator

Iconizer.net Icon Generator

Iconizer is a big icon database very similar to iconfinder but adds icon customization on top. So even complex multicolor icons can be changed almost arbitrarily in color and size.

Iconizer.net Icon Generator

Another tip:

Choose your icons carefully to be easily recognized by most if not all of your target audience. Even widespread icons like chains for “link” only seem to be natural for geeks like us but are not for your average Joe.

I recommend this article as food for thought: http://stiern.com/articles/usability/usability-in-icons/

The Magento buyRequest Object – A Reference

Whoever looked at the Magento source code or database in the context of orders, probably encountered the parameter $buyRequest in many methods or the option info_buyRequest in quote items and order items. Its purpose is not immediately obvious since it contains seemingly redundant information. Also it does not have its own model class, it’s just a Varien_Object and a serialized array when stored to the database.

Example:

mysql> select code,value from sales_flat_quote_item_option where option_id=2359;
+-----------------+------------------------------------------------------------------------------------------------------------+
| code            | value                                                                                                      |
+-----------------+------------------------------------------------------------------------------------------------------------+
| info_buyRequest | a:4:{s:4:"uenc";s:140:"[...],,";s:7:"product";s:4:"5000";s:15:"related_product";s:0:"";s:3:"qty";s:1:"1";} |
+-----------------+------------------------------------------------------------------------------------------------------------+

In short: The $buyRequest object represents the customer request coming from a click on “Add to cart”, all related data can be derived from this object. It’s basically a generator for quote items. The minimal necessary data is thus the product id (product) and quantity (qty).

Where is the $buyRequest object needed?

For example it is created when you add a product to the wishlist, so that it can easily be transfered to the cart with the same configuration. When reconfiguring a product, i.e. editing it from the cart, the buyRequest is passed to the product view to preselect all options.

Also for recurring profiles and reordering the buyRequest objects get “reused” to generate a new order.

Use Cases

Some examples, when it is useful to deal with the buyRequest:

Continue reading “The Magento buyRequest Object – A Reference”

Magento Layout: getChildHtml() with Empty Result

The problem: Mysteriously, getChildHtml() yields an empty result in the template, although the child blocks were created.

The solution: Blocks in the Magento layout should always have a name. Without name-attribute they are shown, but child elements cannot be assigned and stay “orphaned”.

The responsible method is Mage_Core_Model_Layout::_generateBlock(). As you can see, the parent block only gets assigned if it has a name:

            $parentName = $parent->getBlockName();
            if (!empty($parentName)) {
                $parentBlock = $this->getBlock($parentName);
            }

Note that $parent is an XML node here, not a block object. So it does not help that blocs without name automatically get a name like ANONYMOUS_1. The assignment happens via  name-attribute of the node, so that it works for <reference> as well as for <block> parents.

Color Palette Tools (not only) for Casual Designers

Who needs to create a simple design from time to time without having too much knowledge about things like color theory, does not need to put his foot into his mouth with the colors. Here are three ressources for harmonic color palettes:

1. Paletton

Simply click and drag the points in the color wheel with your mouse and get a matching palette in real time, including a preview how the colors look like on a website. Presets for lightness and saturation like “Pastel”, “Shiny” oder “Dark” are useful as starting points.

Link: http://paletton.com

Paletton.com Screenshot

2. Colour Lovers

A different approach: Here designers provide color palettes as well as patterns and single colors. You can search palettes by theme, rate and comment them. Chooese from millions of hand crafted palettes!

Link: http://www.colourlovers.com/

COLOURlovers Screenshot

3. Hex Color Tool

For monochromatic palettes the hex color tool suffices. Start with one color, choose the step size to lighten/darken, done. You can experiment with the inputs without losing previous results.

Link: http://hexcolortool.com/

Hex Color Tool Screenshot

$color = rand(0x000000,0xffffff); is out! Even the simplest website should not lack a harmonic color scheme. Continue reading “Color Palette Tools (not only) for Casual Designers”

PHP: Mock header() to Unit Test Controllers

In 2011 I suggested a technique to mock functions in PHP Unit Tests that takes advantage of the name resolution rules of PHP namespaces. You can read it here:

It makes me proud that the great Matthew Weier O’Phinney 1 now describes the same technique to test code that emits output, especially code that sends headers with the built-in header() function. Read more in his post here:

In my opinion this is a great example of how useful this method is. “Headers already sent” errors in your unit tests can drive you crazy. Unfortunately, there are still many applications that do not use namespaces (*cough* Magento *cough*) where it does not work.

Notes:

  1. for those who don’t know him: He’s the Zend Framework Project Lead and you should follow his blog at http://mwop.net/blog.html!

Magento Testing: Fill Forms with one click

Who doesn’t know this situation: When testing features like the guest checkout manually, you have to fill all form fields tedously again and again. With Chrome auto complete or “test”, Ctrl + C, Ctrl + V it’s halfway fast but still a bit annoying. And what if the test data is supposed to make sense and should not be the same every time?

Inspired by this article on css-tricks.com I developed a little Magento module, that enables filling Magento forms with dummy data with one mouse click. Currently it is implemented for billing address and shipping address.

Github-Repository: SSE_FormFiller

And this is how it looks:

Screenshot: Forms in Checkout

Configuration

Of course you want to see this button only on your development system, that’s why there are two ways to hide it: Either disable the module completely per configuration or show the button only in developer mode:

SSE_FormFiller Configuration

The nice thing about the second variation is that the JavaScript still gets loaded, so that you can substitute the button with a bookmarklet. Here the snippets:

Billing address form

JavaScript:

formFiller.fill(billingForm.form)

Bookmarklet (right click, add as bookmark!)

Shipping address form

JavaScript:

formFiller.fill(shippingForm.form)

Bookmarklet (right click, add as bookmark!)

Technology

A bit of information on the implementation:

  • The dummy data comes from Faker.js.
  • The button gets added to choosen blocks with an observer for core_block_abstract_to_html_after
  • In the JavaScript the form ID determines which fields should be filled.

The module is designed to be extended for other forms as well. Suggestions and of course pull requests on Github are most welcome! Continue reading “Magento Testing: Fill Forms with one click”

Form Problems after Magento Update to CE 1.8.1 or EE 1.13.1 (Login, Cart)

If the login forms in the frontend do not work anymore after an update to Magento CE 1.8.1 or EE 1.13.1, the used theme has to be updated. These forms now need a form key, if it is missing, validation in the controller fails. The same applies for the “Update Cart” button in cart.phtml. How to fix it, see this link:


http://blueclawecommerce.co.uk/blog/fix-customer-cannot-login-to-magento-1-8-1/

Of course it would be optimal, if the them overrides as little core templates as possible and does its changes with layout updates in [theme]/local.xml and CSS wherever possible.