【问题标题】:Magento - How to create attribute programmatically that can be used in 'Sort by'Magento - 如何以编程方式创建可用于“排序依据”的属性
【发布时间】:2015-07-16 13:44:15
【问题描述】:

我需要以编程方式创建几个属性。

这是我的升级脚本的一部分:

$dataOrder = array(
    'attribute_set'           => 'Main',
    'group'                   => 'Datawarehouse',
    'type'                    => 'int',
    'input'                   => 'text',
    'label'                   => 'Total order quantity',
    'global'                  => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'required'                => 0,
    'comparable'              => 0,
    'searchable'              => 0,
    'unique'                  => 0,
    'user_defined'            => 1,
    'visible_on_front'        => 1,
    'visible'                 => 1,
    'is_filterable'           => 1,
    'used_for_sort_by'        => 1,
    'used_in_product_listing' => 1,
);

我的脚本工作正常,我的所有属性都已创建,但我在Sort by 下拉列表(前端)中看不到它们。在后台我可以看到我的属性,我可以分配一个值,一切都很好。

但在Catalog -> Manage Attributes -> Properties -> Frontend properties 下:用于产品列表中的排序设置为“否”。

我认为used_for_sort_byused_in_product_listing 就足够了,但看起来不是。

如何将其设置为是,而无需在后台进行更改?通过在我的升级脚本中添加一些行或在其他地方添加一些代码。

编辑我刚刚意识到它不仅用于产品列表中的排序,而且更新方式不正确。 required 下面的所有内容都没有按照应有的方式更新,所有内容都设置为“否”。

【问题讨论】:

  • 我还是有同样的问题。
  • 在创建这个新属性后重新索引属性是否也有同样的问题?
  • 是的,我愿意。我可以在表 catalog_eav_attribute 中看到 used_for_sort_by 仍然设置为 0 而不是 1。我可以通过查询将其设置为 1,但我认为这不是一个好的解决方案。

标签: magento sorting attributes product


【解决方案1】:

您很可能在已弃用的类上调用 addAttribute() 方法。试试:

$installer = Mage::getResourceModel('catalog/setup', 'catalog_setup');
$installer->addAttribute(Mage_Catalog_Model_Product::ENTITY, 'attribute_id', $dataOrder);

【讨论】:

    【解决方案2】:

    我认为这会很好。 'used_for_sort_by' => true 应该可以工作。

    $eavSetup->addAttribute(
                \Magento\Catalog\Model\Product::ENTITY,
                'sell_counts',
                [
                    'type' => 'text',
                    'backend' => '',
                    'frontend' => '',
                    'label' => 'Sell Count',
                    'input' => 'text',
                    'class' => '',
                    'source' => '',
                    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                    'visible' => true,
                    'required' => false,
                    'user_defined' => false,
                    'default' => '',
                    'searchable' => true,
                    'filterable' => true,
                    'comparable' => true,
                    'visible_on_front' => true,
                    'used_in_product_listing' => true,
                    'used_for_sort_by' => true,
                    'unique' => false,
                    'apply_to' => ''
                ]
            );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-02
      相关资源
      最近更新 更多