2014-11-04 2 views
2

스크립트를 통해 Magento 특성 옵션을 만들고 있지만 새 ID를 가져 와서 동일한 스크립트에서 바로 사용할 수 있어야합니다.프로그래밍 방식으로 특성 옵션을 추가하는 것은 즉시 사용할 수 없습니다.

현재 ID를 가져 오지 않았습니다. 스크립트를 죽이고 다시 시작하면 생성 된 옵션이 선택되어 ID가 반환되지만 동일한 스크립트의 일부는 아닙니다. (필요한 Mage::app() 등이 포함), 옵션을 생성, 당신은 마 젠토 백엔드에서 볼 수

$attr = Mage::getModel('catalog/product')->getResource()->getAttribute($key); 
    if ($attr->usesSource()) { 
      $vattr_id = $attr->getSource()->getOptionId($value); 
    }else{ 
      echo "No Source"; 
      $vattr_id = false; 
    } 


if($vattr_id){ 
     return $vattr_id; 
}else{ 

     $attr_model = Mage::getModel('catalog/resource_eav_attribute'); 
     $attr = $attr_model->loadByCode('catalog_product', $key); 
     $attr_id = $attr->getAttributeId(); 

     $option['attribute_id'] = $attr_id; 
     $option['value'][$value][0] = $value; 
     $option['value'][$value][1] = $value; 

     $setup = new Mage_Eav_Model_Entity_Setup('core_setup'); 
     $setup->addAttributeOption($option); 
     $attr = Mage::getModel('catalog/product')->getResource()->getAttribute($key); 
     if ($attr->usesSource()) { 
       $vattr_id = $attr->getSource()->getOptionId($value); 
       echo "AttrID: $vattr_id"; 
     } 

} 

이 실행하지만, $vattr_idNULL입니다 : 여기

내가 사용하고있는 코드입니다. 스크립트를 다시로드하면 먼저 첫 번째 블록에서 속성 옵션을 찾습니다.

Magento가 모델을 캐싱하는 방법과 관련이 있습니다 만,이 부분을 지우려면 어디를 봐야 할 지 모르겠습니다.

답변

3
function getAttributeOptionId($attributeName, $attributeValue) { 

    /* @var $attribute Mage_Eav_Model_Entity_Attribute */ 
    $attribute = Mage::getModel("eav/entity_attribute")->loadByCode("catalog_product", $attributeName); 

    // checking attribute code 
    if ($attribute->getId()) { 
     $source = $attribute->getSource(); 
     $options = $source->getAllOptions(); 

     // looking for existing id 
     foreach ($options as $optionValue) { 
      if ($attributeValue == $optionValue["label"]) { 
       return $optionValue["value"]; 
      } 
     } 

     // making new option 
     $addOptionData = array(
      "attribute_id" => $attribute->getId(), 
      "value" => array(array($attributeValue)) 
     ); 

     $setup = new Mage_Eav_Model_Entity_Setup('core_setup'); 
     $setup->addAttributeOption($addOptionData); 

     // getting new id 
     $attribute = Mage::getModel("eav/entity_attribute")->loadByCode("catalog_product", $attributeName); 
     $source = $attribute->getSource(); 
     $options = $source->getAllOptions(); 

     foreach ($options as $optionValue) { 
      if ($attributeValue == $optionValue["label"]) { 
       return $optionValue["value"]; 
      } 
     } 
    } 

    return null; 
} 

echo getAttributeOptionId("brand", "Intel"); 
+0

나는이 답변에 투표하는 사람을 찾고 있습니다. – Butterfly

관련 문제