How to redirect simple products to grouped product in Magento

For a quick and dirty way of doing this, you can modify the viewAction() on the product controller tested in 1.7,1.8,.19

Make a copy of

app/code/core/Mage/Catalog/controllers/ProductController.php

and place it in

app/code/local/Mage/Catalog/controllers/ProductController.php

Modify the viewAction() after this section:

// Prepare helper and params
 $viewHelper = Mage::helper('catalog/product_view');

$params = new Varien_Object();
 $params->setCategoryId($categoryId);
 $params->setSpecifyOptions($specifyOptions);

Add the following:

// Does the product have a parent product?
 $p = Mage::getModel('catalog/product_type_grouped')->getParentIdsByChild($productId);

if(!empty($p)) {

// redirect to parent

$prod = Mage::helper('catalog/product')->getProduct($p[0], null, null);
 header('HTTP/1.1 301 Moved Permanently');
 header('Location: '.$prod->getData('url_path'));
 exit;

 }

This will give you a 301 redirect to the parent.

Character encoding issues utf8 & MySQL – How To

Any developer working with php and MySQL will have come across this issue whilst developing.
And often the problem doesn’t rear it’s ugly head until after you’ve captured a lot of data.

If your getting strange characters in your database like question marks and , then follow this simple guide to check each part is set to UTF8.

UT8 is best practice for handling all types of characters and that’s the charset we will be using.

If your just trying to retrieve the unreadable data then see a solution at  the bottom of this post.

MySQL DB

First let’s check the MySQL server, open up your favourite GUI like MySQL Tools or via command line and run:

SHOW SESSION VARIABLES LIKE 'character_set%';

charset

The important parts to notice here

  • character_set_database
  • character_set_results
  • character_set_connection.

These should all be utf8 , if they aren’t then you need configure your database to use utf8.

You can reverse engineer your table using

SHOW CREATE TABLE table_name;

At the very end you will see “DEFAULT CHARSET=charset” change this to utf8.

As further final checks run

show variables like "collation_database";

This should show ‘utf8_general_ci’

And you can check each column individually like so.

SHOW FULL COLUMNS FROM table_name;

PHP Connection

All connections to MySQL need to use utf8, you can do this by using SET NAMES.

For PDO connections you can either include this in the SQL statement before “SET NAMES UTF8;” or you can include it in the connection e.g

$pdo = new PDO('mysql:host=myhost.com;dbname=db_name', 'db_user', 'db_password',array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8");

For standard mysqli connections use:

$mysqli->set_charset("utf8");

It’s important you place that straight after the connection before any queries are run.

What to do if you haven’t set this up from the start?

Thankfully there is a way to retrieve data that might otherwise look garbled in php you can use:


//ISO code for most browsers default encoding

mb_convert_encoding($text, "ISO-8859-1")

Note that you must connect to the DB the way you where doing before you fixed any issues. If you try and retrieve data using SET NAME utf8 then the above will not work as your requesting the data in a different format.

ISO-8859-1 is the character encoding used for most browsers, however there are others so this may not work in every case.

High traffic sites using Magento

We recently had a client debate on what is the best ecommerce solution I am a supporter of Magento the beast it may be, but it is robust. To back up my opinion I did some research although not conclusive.

Magento itself boasts some big clients using the Enterprise edition http://www.magentocommerce.com/product/enterprise-whos-using-magento

I analysed a few of these sites with leading analysis tool compete which estimates the traffic for a given url , the most high traffic site using Magento is Harbor Freight  with some 4 Million uniques at points over the last 2 years, and over 52 Million pageviews (per day) which isn’t to be sniffed at.

stat1

 

Other sites I checked out are:

(figures quoted per day)
Eastwood.com – 240K uniques, 2M page views
Toms.com – 1M uniques, 5M page view

Secondly with the help of BuiltWith there’s some interesting charts on Magento use:

stat2

 

stat3

stat4

 

This shows firstly that the top 1M sites Magento tops the usage worldwide, drilling down in to the most popular 10,000 Magento still tops the opensource solutions with the only other platform osCommerce coming close. Finally in the migration Magento has the highest migration to the platform from other platforms.

In all it’s good evidence to support using Magento now and for high traffic websites. What this does not show is the set up and configuration that may be needed to tweak Magento to run on high traffic sites.

Magento fix for Unable to reindex Product Flat Data – Stuck on Processing. ‘SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row

If you’ve moved across sites from a development to live site or your encountering this error after importing/exporting then here’s the solution that works every time.

1. Search for “catalog_product_flat” . Under my installation its called catalog_product_flat_1

2. Then in mysql run:

SET FOREIGN_KEY_CHECKS=0;

DROP TABLE catalog_product_flat_1

3. Last step open up SSH and run (or do this from the Magento admin)

php path/to/shell/indexer.php --reindex catalog_product_flat or navigate to the /shell directory and run php indexer.php --reindex catalog_product_flat

You should get “Product Flat Data index was rebuilt successfully”

Javascript function to return device type

Here’s a simple JS function to return the type of device based on screen size and some other helpful variables for the main manufacturers.

// mobile check
function checkForMobile() {

	var x = screen.width;
	var y = screen.height;
	var agent = navigator.userAgent.toLowerCase();
	var mobileOS = typeof orientation != 'undefined' ? true : false;
	var touchOS = ('ontouchstart' in document.documentElement) ? true : false;
	var otherBrowser = (agent.indexOf("series60") != -1) || (agent.indexOf("symbian") != -1) || (agent.indexOf("windows ce") != -1) || (agent.indexOf("blackberry") != -1);
	var iOS = (navigator.platform.indexOf("iPhone") != -1) || (navigator.platform.indexOf("iPad") != -1) ? true : false;
	var android = (agent.indexOf("android") != -1) || (!iOS && !otherBrowser && touchOS && mobileOS) ? true : false;
	var istablet = (/ipad|android|android 3.0|xoom|sch-i800|playbook|tablet|kindle/i.test(navigator.userAgent.toLowerCase()));
	var tablet = (istablet==true && x >= 768) ? true : false;

	// is it mobile

	if(x >= 320) {
		whichDevice = "Mobile";
	}

	if(tablet==true) {

		whichDevice = "Tablet";

	} 	

	if(x >= 800 && tablet==false) {

		whichDevice = "Desktop";

	} 

	return whichDevice;

}
       //sets the device type
	checkForMobile();

Found something better? , please let me know.