开发者

Magento Create Product Attribute That Is An Integer

So apparently the support you get from Varien when you purchase t开发者_运维知识库he Enterprise Edition is next to worthless so I am hoping that someone here can maybe lead me in the right direction.

In Magento I am would like to create a product attribute that is the type int. When I create the attribute from the admin panel it creates it as a varchar.

What I am ultimately trying to do is create a total sold attribute so that I can use it for sorting by most sold products in the category display. The problem with it being varchar is that when you sort by this, it sorts it as a string and not a number.

Varien told me that in order to get this to be an integer I would have to do an update to the database. It looks like to me that I would have to change the backend_type column in the eav_attribute table to int and that should hopefully move the values to catalog_product_entity_int table instead of it going to catalog_product_entity_varchar table. Does this make sense? Has anyone else done this before?

Now that I have that question out there, is this what I should expect from the support from Varien? So far their support seem pretty useless. It seems that asking how to do something this simple would not be a big deal (especially since I asked them how to do it, and not to do it for me)


I've never tried to change the type of an existing attribute before, and I try to stay out of fiddling directly with the mysql database structure. It's sort of like directly changing the contents of a memory location in a running desktop application; you can do it, but unless you know exactly what you're doing chances are you'll leave something in an unsupported state. Also, I can't prove this, but I'm pretty sure the specifics of the EAV implementation has chanced over the lifetime of the product, which means different versions would have different answers.

If you can start over from scratch, I'd add an attribute to the model programmatically (around the "Final Array of Key Value Pairs that Define the Attribute" header). Even if you can't start over from scratch, create an integer attribute programmatically and diff your database before and after to see what needs to happen on the backend for an attribute to be created.


You're on the right track with changing the backend_type in eav_attribute, however that won't automatically migrate the attribute values to catalog_product_entity_int, you'll need to run a INSERT INTO SELECT query to take your values from catalog_product_entity_varchar across.

If you create your own module (which you will need to do to calculate the total sales per product anyway), you can insert a new int type attribute via the mysql-install file per this blog post . Also, the module creator extension will give you the module framework to get started.

The other option is to leave the attribute as a varchar and alter the category sort so that it evals the attribute as an int. But I think changing the type is safer and more efficient.

Hope this helps, JD


With regard to your second question about expectations from support, it depends on the terms of the agreement as to whether it would just cover bug fixes, server and application configuration etc. I think it would be unusual for a support agreement to write new code for your custom requirements, but perhaps I'm missing the point.

Their webpage explicitly says that Not included = Code Development, Development Support, Custom Extensions, etc...

Cheers, Jonathan


I had exactly the same problem. Additional problem was that i didn't want to loose attribute values.

First of all you need to export products with attribute by magento dataflow system (System/ImportExport/Profiles). You need to create profile to export and mark in Export: 'only mapped fields'. You can use sku and attribute which you want to change for integer in database.

Later you should create sql install file in any module to change database:

mysql4-install-0.1.0.php

$installer->startSetup();
$installer->updateAttribute('catalog_product', '<<your_attribute_code>>', array(
'backend_type'    => 'int',
));
$this->endSetup();
?>

Later it is very important to reindex product_attribute and product_flat_data.

Next step is to mark all product which use this attribute and using action "update attributes" you should set value by 100 for example.

Now reindex again !!!

and import file by another profile in dataflow.

Good luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜