gpt4 book ai didi

Magento - 如何创建 "decimal"属性类型

转载 作者:行者123 更新时间:2023-12-01 16:09:19 25 4
gpt4 key购买 nike

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

还有其他人遇到过这个问题吗?如果是这样,您采取了什么措施来纠正它?如果我需要创建自定义属性类型,您有任何提示吗?或者您能给我指出执行此操作的任何教程吗?

谢谢!

最佳答案

您想要做的是创建自定义属性类型。

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

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 类:

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);
}
}

希望这有帮助。

有关更多信息,请参阅 here.

关于Magento - 如何创建 "decimal"属性类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12043457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com