Adding new category attribute in Magento using MySQL

Written by Bogdan on Nov. 12, 2013, 10:30 a.m.Comments

There are two ways for adding a new attribute for categories in Magento:

  1. The easy way using a MySQL tool like phpMyAdmin;
  2. The Magento way (using a custom extension).

I usually use the easy way because most of the times I need to be productive. I'm not going to discuss about Magento way because there are a lot of topics over the internet.

So, for the easy way you have to follow these steps.

1. In the table eav_attribute add the following:

INSERT INTO `eav_attribute` ('entity_type_id', 'attribute_code', 'attribute_model`, 'backend_model', 'backend_type', 'backend_table', 'frontend_model', 'frontend_input', 'frontend_label', 'frontend_class', 'source_model', 'is_required', 'is_user_defined', 'default_value', 'is_unique', 'note') VALUES (3, 'replace_with_your_attribute_code', NULL, '', 'text', '', '', 'textarea', 'replace_with_your_attribute_label', NULL, '', 0, 0, 1, '', 0, '');

For entity_type_id the value is usually 3 (for category) but first check table eav_entity_type to make sure.

2. Note down the attribute_id value from last insert and find table eav_entity_attribute.

INSERT INTO `eav_entity_attribute` ('entity_type_id', 'attribute_set_id', 'attribute_group_id', 'attribute_id', 'sort_order') VALUES (3, 12, 7, 'replace_with_attribute_id', 8);

As in previous query, replace entity_type_id with your value from table entity_type_id - same value used in eav_attribute for entity_type_id field.

3. In last step you need to find table catalog_eav_attribute and insert the following values:

INSERT INTO `catalog_eav_attribute` ('attribute_id', 'frontend_input_renderer', 'is_global', 'is_visible', 'is_searchable', 'is_filtrable', 'is_comparable', 'is_visible_on_front', 'is_html_allowed_on_front', 'is_used_for_price_rules', 'is_filtrable_in_search', 'used_in_product_listing', 'used_for_sort_by', 'is_configurable', 'apply_to', 'is_visible_in_advanced_search', 'position', 'iswysiwyg_enabled') VALUES ('replace_with_attribute_id', NULL, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, '', 0, 1, 0);

If you need to see the fields more clearly:

1. eav_attribute:

fieldvalue
entity_type_id3
attribute_code'replace_with_your_attribute_code'
attribute_modelNULL
backend_model''
backend_type'text'
backend_table''
frontend_model''
frontend_input'textarea'
frontend_label'replace_with_your_attribute_label'
frontend_classNULL
source_model''
is_required0
is_user_defined0
default_value''
is_unique0
note''

2. eav_entity_attribute:

fieldvalue
entity_type_id3
attribute_set_id12
attribute_group_id7
attribute_id'replace_with_attribute_id'
sort_order8

3. catalog_eav_attribute:

fieldvalue
attribute_id'replace_with_attribute_id'
frontend_input_rendererNULL
is_global0
is_visible1
is_searchable0
is_filterable0
is_comparable0
is_visible_on_front0
is_html_allowed_on_front0
is_used_for_price_rules1
is_filterable_in_search0
used_in_product_listing0
used_for_sort_by0
is_configurable1
apply_to''
is_visible_in_advanced_search0
position1
is_wysiwyg_enabled0

Simple way to get file extension in PHP

Written by Bogdan on Nov. 6, 2013, 11:33 a.m.Comments

The most simple way to get a file extension in PHP is using pathinfo function. This function was introduced in PHP 4.0.3 so it should be available on all servers;

Example:

$ext = pathinfo($path, PATHINFO_EXTENSION);

You can read more about this function on PHP: pathinfo - Manual

Generating random passwords in PHP

Written by Bogdan on Oct. 31, 2013, 5:59 p.m.Comments

Generating random passwords in PHP is as simple as:

<?php
$alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$length = 10;
substr(str_shuffle($alphabet), 0, $length);

VivaSMS Invitation Code

Written by Bogdan on Oct. 15, 2013, 10:40 a.m.Comments

For those who want to create a VivaSMS account and don't have a friend who already joined the network, you can click here to create a new account.

Disable Wordpress FTP Mode

Written by Bogdan on Oct. 10, 2013, 8:09 p.m.Comments

Sometimes Wordpress fails to detect your server configuration and forces FTP mode.
In order to force Wordpress use direct mode, add the following line at the bottom of your wp-config.php:

define('FS_METHOD', 'direct');

Started a new SEO strategy

Written by Bogdan on Jan. 20, 2013, 1:18 a.m.Comments

Today I started a new SEO strategy for a client of mine. She needs to be on the first page in google results for the following keywords: "traduceri autorizate" and "traduceri pitesti".

First I did some On-page optimizations: meta tags, description, relevant page title. Then I added some internal links containing those keywords pointing to relevant pages and after that I thought that two links on my personal website won't do any harm.

I hope this SEO strategy works and my blog won't have to suffer because of it.

Django + Haystack + Solr: No results

Written by Bogdan on Jan. 5, 2013, 1:04 a.m.Comments

I had to implement a custom search for this blog using Django + Haystack + Solr.
Everything went well until I tested the search page. Every query that I made returned this message: "No results found.".
After searching the internet about 2 hours for a solution, I thought why not restart the Django app?

After I restarted the Django app, the search results started to show.

BBC: New Year's Lights Show 2013

Written by Bogdan on Jan. 3, 2013, 10:54 p.m.Comments

This is the best fireworks / lights show I have ever seen. I hope next year I'll be there to see it live.

Isn't this the best fireworks / lights show you have ever seen?

GAPI: Failed to request account data

Written by Bogdan on Jan. 3, 2013, 5:41 p.m.Comments

To fix this problem, in gapi.class.php change:

const account_data_url = 'https://www.google.com/analytics/feeds/accounts/default';
const report_data_url = 'https://www.google.com/analytics/feeds/data';

to:

const account_data_url = 'https://www.googleapis.com/analytics/v2.4/management/accounts/~all/webproperties/~all/profiles';
const report_data_url  = 'https://www.googleapis.com/analytics/v2.4/data';

Google made some changes and GAPI library was not updated.

Also, to fix some php notices, on line 277 you should find:

$account_root_parameters['startDate'] = strval($google_results->startDate);
$account_root_parameters['endDate'] = strval($google_results->endDate);

replace that with:

if(isset($google_results))
{
$account_root_parameters['startDate'] = strval($google_results->startDate);
$account_root_parameters['endDate'] = strval($google_results->endDate);
}

CodeIgniter: Disallowed Key Characters

Written by Bogdan on Jan. 3, 2013, 2:26 p.m.Comments

Codeigniter was giving me an error: 'Disallowed Key Characters.'.
The problem was that I had an openx zone that was loading some javascript from linkedin and that javascript set some cookies with invalid (in codeigniters view) characters in key name. Anyway, if you have this problem, check global variables like: $_POST, $_GET, $_SERVER and $_COOKIE. You'll find out that at least one key contains disallowed characters.

My instinct was to remove that adzone, but what happens if some other adzone sets invalid cookies? The users will not be able to view the site until that cookie is deleted.

So I came up with this little modification to the CI_Input class:

Save the code above in a file named MY_Input.php in the /application/core folder. The only modification is on line #7 where instead of exit I did a str_replace to remove invalid characters.