Skip to content

Magento – only show categories containing products in main navigation

I was speaking to a client in Bristol who needed a freelance Magento developer to make some changes to their site. Top of the list was to modify the main navigation so that a category would only show if it contained active products, or sub categories with active products.

To cut a very long story short, if you override or subclass Mage_Catalog_Block_Navigation you can do this fairly quickly. Add the method _hasProducts as shown below, then you need to call this inside the renderCategoriesMenuHtml() and _renderCategoryMenuItemHtml() methods…

/**
* Check if a product has an active products
* @param int $category_id
* @return boolean
*/
private function _hasProducts( $category_id )
{
$products = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection()
->addAttributeToSelect('entity_id')
->addAttributeToFilter('status', 1)
->addAttributeToFilter('visibility', 4);
/* @var $products Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection */
return ( $products->count() > 0 )  ? true : false;
}

Then modify the 2 methods below…

protected function _renderCategoryMenuItemHtml()
{
//snip a whole load of code until you reach the first foreach loop
foreach ($children as $child) {
if ( $child->getIsActive() ) {
if( $child->hasChildren() ) {
$activeChildren[] = $child;
}else{
if( $this->_hasProducts( $child->entity_id ) ) {
$activeChildren[] = $child;
}
}
}
//don't change the rest of the method...
}

public function renderCategoriesMenuHtml()
{
//snip a whole load of code until you reach the first foreach loop
foreach ($this->getStoreCategories() as $child) {
/* @var $child Varien_Data_Tree_Node */
if ( $child->getIsActive() ) {
if( $child->hasChildren() ) {
$activeCategories[] = $child;
}else{
if( $this->_hasProducts( $child->entity_id ) ) {
$activeCategories[] = $child;
}
}
}
}
//don't change the rest of the method...

}

Looking for a Magento developer to help you get your ecommerce website moving? Sweet-Apple is based near Bath and builds and supports cost effective Magento ecommerce websites. Give us a call on 01380 830224 – we’d love to help.

Posted in Magento | 2 Comments

Wait before upgrading to Lion OS X 10.7!

I’ve just been speaking to a client in Marshfield who’s just bought a new iMac from the AppleStore in Bath. The sales person was enthusiastically telling them how they could get their free upgrade to Lion when it comes out in July. I’m here to tell you that any responsible person providing Mac support should be telling you quite clearly DO NOT upgrade to Lion for at least 3 months after July 2011, possibly more. Why?

Lion will have compatibility bugs…

Every time Apple release a major OS update, inevitably some programs will have problems working with it. If you use nothing but Apple provided software, you might find that you have no issues at all – but for the majority of that people that just isn’t true. They’ve got old versions of Photoshop, Illustrator, MYOB, Appleworks, Word, etc. that they still use for their day to day work and rely on.

Lion drops support for old PPC software

10.7 Lion is a massive change. It finally ditches all support for software written to run on the old PowerPC G4 and G5 processor families that you may remember from days of yore (well ok, 5 years ago). Apple created a technology called Rosetta that enabled new Intel powered Macs to run software that was written to run only on PPC machines. Since OS X 10.4, Rosetta has done a stirling job, letting your shiny new Intel Mac run your old software. Sure, they didn’t run as lightning quick as software optimised for Intel chips, but it got the job done. But no longer…

Before you even dream of upgrading to Lion, make sure you check each and every program that you use on a day to day basis is “Intel Native”. The easiest way to do this is to open System Profiler and click into the Software->Applications section. Wait for a few moments whilst the list gets populated. Then click on the “Kind” column. All the programs that are “PowerPC” will NOT work in Lion and you’ll need to find replacements or get upgrades.

Even if you find that there’s nothing that business critical, I still wouldn’t upgrade to Lion for at least 3 months, if not 6. Let the “early adopters” gnash their teeth with frustration – despite what the computer industry would like you to believe, it’s much more expensive and painful on the “bleeding edge” of technology.

Looking for unbiased, down to earth and sensible Mac support? Give Sweet-Apple a call on 01380 830224. We’re here to save your hair, sanity and wallet from making expensive Mac mistakes…

Posted in Troubleshooting | Tagged , , , | 1 Comment

Ultra cheap refurbished Sandybridge MacBook Pro i7 2011

UPDATE: If you look on Amazon for Macbook Pros you can often find brand new ones for prices very close to what Apple are selling Refurbs at…

Apple have just started to sell “Sandy Bridge” Refurbished MacBook Pros on the Refurb Store. I’ve been waiting for this ever since the new 2011 MacBook Pros were announced, as they offer a massive speed boost compared to my current Core2MacBook Pro, so I’ve bitten the bullet and bought one. You can currently save around 15% off the standard retail price of a new 2011 MacBook Pro, plus still have the full Apple warranty.

  1. Refurbished MacBook Pro 2.3GHz dual-core Intel i5 – £849 inc VAT and Delivery
  2. Refurbished MacBook Pro 2.7GHz dual-core Intel i7 – £1099 inc VAT and Delivery
  3. Refurbished MacBook Pro 2.0GHz quad-core Intel i7 – £1319 inc VAT and Delivery
  4. Refurbished MacBook Pro 2.2GHz quad-core Intel i7 – £1569 inc VAT and Delivery
Posted in Refurbished | Tagged , , | Comments Off on Ultra cheap refurbished Sandybridge MacBook Pro i7 2011

Detecting screen orientation changes in iPhones and iPads

I’ve been working on a freelance web development job for a customer in Bath, and one of the requirements was for the site to be specifically tested on iOS devices such as iPhones and iPads. The site used a lot of JavaScript effects, absolutely positioned elements and jQuery plugins, and during testing we noticed that when changing the screen orientation on an iPhone, some glitches were appearing. So what to do?

Fortunately Apple have included a number of iOS specific events that web developers can tap into. The onorientationchange event is thrown when the iPhone is rotated from portrait to landscape, so you can listen for this event and add code to fix the problem…

window.onorientationchange = function(){
   // Do clever stuff to fix problems caused by rotating 
};
Posted in Web Design | Tagged , , | Comments Off on Detecting screen orientation changes in iPhones and iPads

Overloading functions in WordPress child themes

I was doing a quick freelance WordPress development job for a publishing company based in Cheltenham today, having to modify an existing theme to better fit their design goals. Because the theme was quite complex and included a large amount of custom functionality, I didn’t want to start hacking the core theme files. So I need to create a sub-theme.

Creating sub-themes is a trivial task. The problem comes when you need to override some of the functions used in the parent theme. The difficulty is that you cannot redeclare a function in PHP without it throwing a fatal error. Plus you can’t overload functions or methods by creating a new method signature, as you could in C#. So how do you solve the problem?

If the theme is exceptionally well designed, all of the parent theme function will be wrapped in a function_exists() call. Due to the order that WordPress loads theme files, it will first load the child theme function, then attempt to load the parent theme function. If a parent theme function is declared as shown below, you can happily redeclare the function in your child theme safe in the knowledge you won’t get fatal errors.

if ( ! function_exists( 'theme_function' ) ) {
    function theme_function() {
   // Do clever stuff here
    }

If they haven’t wrapped their functions, you need to hope that the function you wish to override has been added to one of WordPress many hooks, or that the theme has it’s own method of locating and loading functions in the child theme ( as Thematic does). If not, you’re going to have to do some ugly hacks, or risk your changes getting over-written when the client updates the theme.

Looking for a web developer to turn your web designs into reality? Sweet-Apple develops websites for small businesses, design agencies and freelance designers, quickly, efficiently and cost-effectively. Why not give us a call on 01380 830224 and find out how we can help…

Posted in Web Development, Wordpress | Tagged , , , | Comments Off on Overloading functions in WordPress child themes