如何获取 Magento 中属性选项的位置?

发布于 2024-12-11 17:09:16 字数 645 浏览 0 评论 0原文

我有以下问题。在一个额外的模块中,我想根据属性的位置对属性的选项进行排序。当我尝试获取属性的选项时,我可以获得 Id 和标签,但该对象中没有更多内容。

例如,我可以这样做:

$attribute = $_product->getResource()->getAttribute($code)->getOptionsText($value):

或者只是 getOptionId($value),但是没有任何东西可以获取可在后端编辑的位置。那么,如何获得这个呢?尚未在网上找到任何(有用的)内容。

(同样类似的问题 magento 按位置排序属性选项集合? 不提供任何帮助)

编辑: 我设法做的是直接执行 SQL 语句,如下所示:

SELECT sort_order FROM mag_eav_attribute_option WHERE option_id = 114 AND attribute_id = 533;

但我认为,有更好的选择来获取该值。

I've got the following problem. In an extra module, I want to sort the options of an attribute, based on their position. When I try to get the Option of an Attribute, I can get the Id and the Label, but there is nothing mor ein that object.

I can do, for example, this:

$attribute = $_product->getResource()->getAttribute($code)->getOptionsText($value):

Or just getOptionId($value), but there is nothing to get the Position, which is editable in the backend. So, how to get this? Havn't found anything (useful) on the net yet.

(Also the similar question magento sort attribute option collection by position? doesnt give any help)

EDIT:
What I managed to do, is doing a direct SQL statement, like this:

SELECT sort_order FROM mag_eav_attribute_option WHERE option_id = 114 AND attribute_id = 533;

But I think, there is a better option to get that value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

提赋 2024-12-18 17:09:17

我自己找到了答案并想与大家分享。

$attribute = Mage::getModel('eav/entity_attribute')->load( $code, 'attribute_code');
$option_col = Mage::getResourceModel( 'eav/entity_attribute_option_collection')
 ->setAttributeFilter( $attribute->getId() )
 ->setStoreFilter()
 ->setPositionOrder( 'ASC' );
$option_col->getSelect()->order('main_table.sort_order '.$orderby);

我希望它能帮助某人。

I found the answer myself and want to share it with you.

$attribute = Mage::getModel('eav/entity_attribute')->load( $code, 'attribute_code');
$option_col = Mage::getResourceModel( 'eav/entity_attribute_option_collection')
 ->setAttributeFilter( $attribute->getId() )
 ->setStoreFilter()
 ->setPositionOrder( 'ASC' );
$option_col->getSelect()->order('main_table.sort_order '.$orderby);

I hope it helps someone.

三人与歌 2024-12-18 17:09:17

这实际上很简单,如果你看看@Mage_Eav_Block_Adminhtml_Attribute_Edit_Options_Abstract

$attributeId = 230;
$options = Mage::getResourceModel('eav/entity_attribute_option_collection')
            ->setAttributeFilter($attributeId)
            ->setPositionOrder('desc', true)
            ->load();

foreach($options as $opt){
    Mage::log($opt->getSortOrder());
}

我发现你已经想出了类似的东西,但我想我会发布这个,因为它可能对其他人有帮助。

This is actually quite simple if you take a look @ Mage_Eav_Block_Adminhtml_Attribute_Edit_Options_Abstract

$attributeId = 230;
$options = Mage::getResourceModel('eav/entity_attribute_option_collection')
            ->setAttributeFilter($attributeId)
            ->setPositionOrder('desc', true)
            ->load();

foreach($options as $opt){
    Mage::log($opt->getSortOrder());
}

I see you came up with something similar already but I thought I would post this as it may be helpful to others.

三月梨花 2024-12-18 17:09:17

然后开始调试:

print_r($_product->getResource()->getAttribute($code));
print_r($_product->getResource()->getAttribute($code)->getData());
print_r(get_class_methods($_product->getResource()->getAttribute($code)));

Start to debug then:

print_r($_product->getResource()->getAttribute($code));
print_r($_product->getResource()->getAttribute($code)->getData());
print_r(get_class_methods($_product->getResource()->getAttribute($code)));
高冷爸爸 2024-12-18 17:09:17

好的,现在您已经有了属性代码 $code 和属性选项值 $value。然后就可以得到相应的排序顺序,如下所示:

$source = $product->getResource()->getAttribute($code)->getSource();
$optionId = $source->getOptionId($product->getData($code));
$optionSortOrder = Mage::getResourceModel('eav/entity_attribute_option_collection')
                    ->setIdFilter($_optionId)
                    ->getFirstItem()
                    ->getSortOrder();

Okay, so you have your attribute code $code and your attribute option value $value. Then you can get the corresponding sort order like that:

$source = $product->getResource()->getAttribute($code)->getSource();
$optionId = $source->getOptionId($product->getData($code));
$optionSortOrder = Mage::getResourceModel('eav/entity_attribute_option_collection')
                    ->setIdFilter($_optionId)
                    ->getFirstItem()
                    ->getSortOrder();
东京女 2024-12-18 17:09:17

来自: http://www.phptechi.com/getting-product -attributes-values-and-labels.html

如何获取属性值排序顺序?

这是使用 SQL 获取属性值排序定位的快速简便方法。
更改以下代码中变量的值:

$CurtAtr 是当前属性,如颜色、大小等
$attrVal 是属性值,例如大小有“小、中、大等”

$resource = Mage::getSingleton('core/resource');
$read = $resource->getConnection('catalog_read');  
$read->fetchAll("SELECT ao.sort_order FROM eav_attribute_option_value as aov, eav_attribute_option as ao where value='$attrVal' and aov.option_id = ao.option_id and attribute_id=$CurtAtr limit 1");

From: http://www.phptechi.com/getting-product-attributes-values-and-labels.html

How to fetch attribute value sort order?

Here is the Quick and Easy way using SQL to get attribute value sort positing.
Change value for variables in following code:

$CurtAtr is current attribute like color, size, etc
$attrVal is attribute value e.g. size has "small, medium, Large, etc"

$resource = Mage::getSingleton('core/resource');
$read = $resource->getConnection('catalog_read');  
$read->fetchAll("SELECT ao.sort_order FROM eav_attribute_option_value as aov, eav_attribute_option as ao where value='$attrVal' and aov.option_id = ao.option_id and attribute_id=$CurtAtr limit 1");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文