【问题标题】:magento added product programmatically dosent contain the right valuesmagento 以编程方式添加产品不包含正确的值
【发布时间】:2014-08-11 10:09:25
【问题描述】:

这是我以编程方式添加的属性的代码:

<?php                                                                                                                                                

/** @var $installer Welance_DBSetup_Model_Resource_Setup */                                                                                          
$installer = $this;                                                                                                                                  
$installer->startSetup();                                                                                                                            

//NJ add product attributes                                                                                                                          
$attributeId = $installer->getAttributeId('catalog_product','otb_verpackungseinheit');                                                               
if($attributeId){                                                                                                                                    
    $installer->removeAttribute('catalog_product',$attributeId);                                                                                     
}                                                                                                                                                    

$installer->addAttribute('catalog_product', 'otb_verpackungseinheit', array(                                                                         




        'is_global'                     => '1',                                                                                                      
        'frontend_input'                => 'text',                                                                                                   
        'label'                         => 'Verpackungseinheit',                                                                                     
        'is_visible_on_front'           => '1',                                                                                                      
        'used_in_product_listing'       => 'true',                                                                                                   
        'default_value_text'            => '',                                                                                                       
        'default_value_yesno'           => '0',                                                                                                      
        'default_value_date'            => '',                                                                                                       
        'default_value_textarea'        => '',                                                                                                       
        'is_unique'                     => '0',                                                                                                      
        'is_required'                   => '0',                                                                                                      
        'frontend_class'                => '',                                                                                                       
        'is_searchable'                 => '1',                                                                                                      
        'is_visible_in_advanced_search' => '1',                                                                                                      
        'is_comparable'                 => '1',                                                                                                      
        'is_html_allowed_on_front'      => '1',                                                                                                      
        'is_used_for_promo_rules'       => '0',                                                                                                      
        'used_for_sort_by'              => '0',                                                                                                      
        'is_configurable'               => '0',                                                                                                      
        'is_filterable'                 => '0',                                                                                                      
        'is_filterable_in_search'       => '0',                                                                                                      
        'backend_type'                  => 'varchar',                                                                                                
        'default_value'                 => '',                                                                                                       

    )                                                                                                                                                
);                                                                                                                                                   
$installer->updateAttribute('catalog_product', 'otb_verpackungseinheit', 'apply_to', 'configurable,simple,grouped,bundle,virtual,downloadable');     


$installer->endSetup();                                                                                                                              

属性出现,这些值设置正确:

'is_global'                     => '1',
'frontend_input'                => 'text',
'label'                 => 'Verpackungseinheit',    

但是当我在后端查找属性时,这些值仍然为 0:

'is_visible_on_front'           => '1',
'is_searchable'                 => '1',
'is_visible_in_advanced_search' => '1',
'is_comparable'                 => '1',
'is_html_allowed_on_front'      => '1',

有人有想法吗?

【问题讨论】:

    标签: php magento attributes


    【解决方案1】:

    您的目录值似乎被忽略了(catalog_eav_attribute 表中的值)。

    我最好的猜测是您的安装程序类 Welance_DBSetup_Model_Resource_Setup 是从 Mage_Eav_Model_Entity_Setup 扩展而来的,而不是从 Mage_Catalog_Model_Resource_Setup 扩展而来的。

    【讨论】:

      【解决方案2】:

      我有一些以编程方式创建属性的代码。 有功能createAttribute()。您必须通过提供参数来调用它。希望对你有帮助

      function createAttribute($code, $label, $attribute_type, $product_type, $attribute_set_name, $group_name )
      {
          $_attribute_data = array(
              'attribute_code' => $code,
              'is_global' => '1',
              'frontend_input' => $attribute_type, //'boolean',
              'default_value_text' => '',
              'default_value_yesno' => '0',
              'default_value_date' => '',
              'default_value_textarea' => '',
              'is_unique' => '0',
              'is_required' => '0',
              'apply_to' => array($product_type), //array('grouped')
              'is_configurable' => '1',
              'is_searchable' => '1',
              'is_visible_in_advanced_search' => '1',
              'is_comparable' => '1',
              'is_used_for_price_rules' => '0',
              'is_wysiwyg_enabled' => '0',
              'is_html_allowed_on_front' => '1',
              'is_visible_on_front' => '1',
              'used_in_product_listing' => '0',
              'used_for_sort_by' => '0',
              'frontend_label' => array($label)
          );
      
          $model = Mage::getModel('catalog/resource_eav_attribute');
      
          if (!isset($_attribute_data['is_configurable'])) {
              $_attribute_data['is_configurable'] = 0;
          }
          if (!isset($_attribute_data['is_filterable'])) {
              $_attribute_data['is_filterable'] = 0;
          }
          if (!isset($_attribute_data['is_filterable_in_search'])) {
              $_attribute_data['is_filterable_in_search'] = 0;
          }
      
          if (is_null($model->getIsUserDefined()) || $model->getIsUserDefined() != 0) {
              $_attribute_data['backend_type'] = $model->getBackendTypeByInput($_attribute_data['frontend_input']);
          }
      
          $defaultValueField = $model->getDefaultValueByInput($_attribute_data['frontend_input']);
          if ($defaultValueField) {
            //  $_attribute_data['default_value'] = $this->getRequest()->getParam($defaultValueField);
          }
      
          $model->addData($_attribute_data);
      
          $model->setEntityTypeId(Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId());
          $model->setIsUserDefined(1);
          try {
              $model->save();
              $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
                  //-------------- add attribute to set and group
      
                  $attribute_code = $code;
      
                  $attribute_set_id=$setup->getAttributeSetId('catalog_product', $attribute_set_name);
                  $attribute_group_id=$setup->getAttributeGroupId('catalog_product', $attribute_set_id, $group_name);
                  $attribute_id=$setup->getAttributeId('catalog_product', $attribute_code);
      
                  $setup->addAttributeToSet($entityTypeId='catalog_product',$attribute_set_id, $attribute_group_id, $attribute_id);
      
          } catch (Exception $e) { echo '<p>Sorry, error occured while trying to save the attribute. Error: '.$e->getMessage().'</p>'; }
      }
      

      带参数调用函数-

      $attribute_set_name = "Default";
      $group_name = "General";
      createAttribute('technical_specs', "Technical Specs", "select", "simple", $attribute_set_name, $group_name); // use text instead of select if it is textbox
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-14
        • 1970-01-01
        • 2011-09-23
        • 1970-01-01
        • 2012-12-01
        • 2015-04-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多