2011-09-01 10 views
2

"vendor_id"라는 속성이 있습니다. 제품의 속성으로 "vendor_id"가있는 N 개의 제품이 있습니다. "vendor_id"속성에 대한 옵션은 관리자가 새로운 "공급 업체"엔티티를 추가 할 때 프로그래밍 방식으로 생성됩니다. 코드는 다음과 같습니다.Magento에서 속성의 옵션을 프로그래밍 방식으로 업데이트/편집

public function saveAction() 
    { 
    $data = $this->getRequest()->getPost(); 
    $designerName = $data['title']; 

    $product = Mage::getModel('catalog/product'); 
    $attributes = Mage::getResourceModel('eav/entity_attribute_collection') 
      ->setEntityTypeFilter($product->getResource()->getTypeId()) 
      ->addFieldToFilter('attribute_code', 'vendor_id') 
      ->load(false); 
    $attribute = $attributes->getFirstItem()->setEntity($product->getResource()); 
    $myresults = array ('value'=> array('optionone'=>array($designerName))); 

    $attribute->setData('option',$myresults); 
    $attribute->save(); 

그리고이 코드는 현재 작동합니다. 그러면 "vendor_id"속성에 대한 옵션이 생성되고 사용자가 새 제품을 추가하거나 기존 제품을 편집하면 saveAction에서 만든 "공급 업체"엔티티가 "vendor_id"드롭 다운을 채 웁니다() 메소드를 호출합니다.

관리자가 기존 속성 옵션을 편집하려는 경우 새 옵션을 만들고 싶지 않습니다. 기존 옵션을 편집하고 싶습니다. 이름/라벨을 변경할 때 옵션 ID가 동일하게 유지되는 것이 중요합니다.

나는 우리가 편집하거나 새로운 옵션 만드는 경우 우리가 확인하고 볼 수있는 saveAction()에 있도록 정적 VAR를 설정하는 newAction에 중독했습니다

if (null == MyController::$_editScope) 
    { 
     error_log('Need to update option attribute'); 
     $attribute->addData($myresults); 
    } 

문제는는, addData이다() 메서드는 데이터를 추가하지만 기존의 데이터를 업데이트하지 않습니다. 속성은 다음과 같습니다 배 부모 클래스를 가지고 내가 * 기존하세요 나 편집 할 수 방법 * 또는 업데이트를 위해 그들 모두를 통해 검토 한 결과 http://docs.magentocommerce.com/Mage_Eav/Mage_Eav_Model_Entity_Attribute.html

: 인스턴스입니다

$attribute = $attributes->getFirstItem()->setEntity($product->getResource()); 

옵션 이름 ...

답변

3

이 정보는 유용합니다. See complete details here.

//Get the eav attribute model 
$attr_model = Mage::getModel('catalog/resource_eav_attribute'); 

//Load the particular attribute by id 
//Here 73 is the id of 'manufacturer' attribute 
$attr_model->load(73); 

//Create an array to store the attribute data 
$data = array(); 

//Create options array 
$values = array(
    //15 is the option_id of the option in 'eav_attribute_option_value' table 
    15 => array(
      0 => 'Apple' //0 is current store id, Apple is the new label for the option 
     ), 
    16 => array(
      0 => 'HTC' 
     ), 
    17 => array(
      0 => 'Microsoft' 
     ), 
); 

//Add the option values to the data 
$data['option']['value'] = $values; 

//Add data to our attribute model 
$attr_model->addData($data); 

//Save the updated model 
try { 
    $attr_model->save(); 
    $session = Mage::getSingleton('adminhtml/session'); 
    $session->addSuccess(
     Mage::helper('catalog')->__('The product attribute has been saved.')); 

    /** 
    * Clear translation cache because attribute labels are stored in translation 
    */ 
    Mage::app()->cleanCache(array(Mage_Core_Model_Translate::CACHE_TAG)); 
    $session->setAttributeData(false); 
    return; 
} catch (Exception $e) { 
    $session->addError($e->getMessage()); 
    $session->setAttributeData($data); 
    return; 
} 
+0

특정 매장보기에 라벨을 어떻게 설정합니까? – Felix

+0

첫 번째 링크가 사망하여 [여기] (http://arvind-07.blogspot.com/2012/05/addupdate-attribute-option-values.html)에서 동일한 콘텐츠를 발견했습니다. – Isaias

+0

@Isaias [해당 링크] (http : //www.webspeaks.in/2012/05/addupdate-attribute-option-values.html) 이제 작동합니다. 통고 해줘서 고마워. –

관련 문제