Magento-如何创建“十进制"属性类型 [英] Magento - How to create "decimal" attribute type

查看:61
本文介绍了Magento-如何创建“十进制"属性类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了一些在线搜索,但尚未找到该问题的任何答案.我遇到的情况是,我需要一个十进制值的乘积属性,它必须既支持负数也支持正数,并且还必须是可排序的.由于某些原因,Magento没有十进制"属性类型.使用十进制值的唯一类型是价格,但不支持负数.如果我使用文本"作为类型,它将支持我想要的任何内容,但是由于它会将值视为字符串而不是浮点数,因此无法正确排序.我可以通过手动编辑eav_attribute表并将'frontend_input'从'price'更改为'text',但将'backend_type'保留为'decimal',来解决此问题,就像其他人在我发现的帖子中所说的那样.效果很好...直到有人在管理面板中编辑属性.保存属性后,Magento注意到frontend_input是'text'并将'backend_type'更改为'varchar'.我能想到的唯一解决方法是创建自定义属性类型,但是我不确定从哪里开始,也无法在线找到任何详细信息.

I've done a bit of searching online but I have not found any answers to this question yet. I have a situation where I need a product attribute that is a decimal value and it must support negative numbers as well as positive and must also be sortable. For some reason, Magento does not have a "decimal" attribute type. The only type that uses decimal values is Price, but that doesn't support negative numbers. If I use "text" as the type, it supports whatever I want, but it doesn't sort properly because it sees the values as strings rather than floating point numbers. I have been able to work around this issue, as others have in posts I've found, by manually editing the eav_attribute table and changing 'frontend_input' from 'price' to 'text', but leaving the 'backend_type' as 'decimal'. This works great...until someone edits the attribute in the admin panel. Once you save the attribute, Magento notices that the frontend_input is 'text' and changes the 'backend_type' to 'varchar'. The only way around this that I can think of is by creating a custom attribute type, but I'm not sure where to start and I can't find any details online for this.

其他人遇到过这个问题吗?如果是这样,您做了什么纠正?如果我需要创建自定义属性类型,您有什么建议吗?或者您可以指向我那里的任何教程中进行此操作吗?

Has anyone else experienced this problem? If so, what have you done to correct it? If I need to create a custom attribute type, do you have any tips or can you point me at any tutorials out there for doing this?

谢谢!

推荐答案

您要创建的是自定义属性类型.

What you want to do is create a custom attribute type.

这可以通过首先创建安装程序脚本来完成(这将更新数据库).

This can be done by first creating a installer script (this updates the database).

startSetup();

$installer->addAttribute('catalog_product', 'product_type', array(
    'group'             => 'Product Options',
    'label'             => 'Product Type',
    'note'              => '',
    'type'              => 'dec',    //backend_type
    'input'             => 'select', //frontend_input
    'frontend_class'    => '',
    'source'            => 'sourcetype/attribute_source_type',
    'backend'           => '',
    'frontend'          => '',
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE,
    'required'          => true,
    'visible_on_front'  => false,
    'apply_to'          => 'simple',
    'is_configurable'   => false,
    'used_in_product_listing'   => false,
    'sort_order'        => 5,
));

$installer->endSetup();

之后,您需要创建一个名为的自定义php类:

After that you need to create a custom php class named:

任何源类型_模型_属性_源_类型

Whatever_Sourcetype_Model_Attribute_Source_Type

然后在其中粘贴:

class Whatever_Sourcetype_Model_Attribute_Source_Type extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
    const MAIN = 1;
    const OTHER = 2;

public function getAllOptions()
{
    if (is_null($this->_options)) {
        $this->_options = array(
            array(
                'label' => Mage::helper('sourcetype')->__('Main Product'),
                'value' =>  self::MAIN
            ),
            array(
                'label' => Mage::helper('sourcetype')->__('Other Product'),
                'value' =>  self::OTHER
            ),
        );
    }
    return $this->_options;
}

public function toOptionArray()
{
    return $this->getAllOptions();
}

public function addValueSortToCollection($collection, $dir = 'asc')
{
    $adminStore  = Mage_Core_Model_App::ADMIN_STORE_ID;
    $valueTable1 = $this->getAttribute()->getAttributeCode() . '_t1';
    $valueTable2 = $this->getAttribute()->getAttributeCode() . '_t2';

    $collection->getSelect()->joinLeft(
        array($valueTable1 => $this->getAttribute()->getBackend()->getTable()),
        "`e`.`entity_id`=`{$valueTable1}`.`entity_id`"
        . " AND `{$valueTable1}`.`attribute_id`='{$this->getAttribute()->getId()}'"
        . " AND `{$valueTable1}`.`store_id`='{$adminStore}'",
        array()
    );

    if ($collection->getStoreId() != $adminStore) {
        $collection->getSelect()->joinLeft(
            array($valueTable2 => $this->getAttribute()->getBackend()->getTable()),
            "`e`.`entity_id`=`{$valueTable2}`.`entity_id`"
            . " AND `{$valueTable2}`.`attribute_id`='{$this->getAttribute()->getId()}'"
            . " AND `{$valueTable2}`.`store_id`='{$collection->getStoreId()}'",
            array()
        );
        $valueExpr = new Zend_Db_Expr("IF(`{$valueTable2}`.`value_id`>0, `{$valueTable2}`.`value`, `{$valueTable1}`.`value`)");

    } else {
        $valueExpr = new Zend_Db_Expr("`{$valueTable1}`.`value`");
    }



    $collection->getSelect()
        ->order($valueExpr, $dir);

    return $this;
}

public function getFlatColums()
{
    $columns = array(
        $this->getAttribute()->getAttributeCode() => array(
            'type'      => 'int',
            'unsigned'  => false,
            'is_null'   => true,
            'default'   => null,
            'extra'     => null
        )
    );
    return $columns;
}


public function getFlatUpdateSelect($store)
{
    return Mage::getResourceModel('eav/entity_attribute')
        ->getFlatUpdateSelect($this->getAttribute(), $store);
}
}

希望这会有所帮助.

有关更多信息,请参见此处.

For further info see here.

这篇关于Magento-如何创建“十进制"属性类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆