2011-12-19 126 views
0

這裏的類似問題並沒有幫助我解決問題,所以我只能自己問。Magento使用自創屬性和選項創建新產品

我有一個PHP文件,它創建一個屬性(下拉)和一個PHP文件,爲它創建選項。

現在我想創造出產生我要與一個或多個屬性Magento的一個簡單的產品(這是之前創建)

這裏是我的代碼生成數據爲它的PHP文件:

$code = "\$newProductData = array( 
      " . ((!empty($name)) ? "'name' => '$name', " : "") . " 
      " . ((!empty($websites)) ? "'websites' => '$websites[0]', " : "") . " 
      " . ((!empty($description)) ? "'description' => '$description', " : "") . " 
      " . ((!empty($description_short)) ? "'description_short' => '$description_short', " : "") . " 
      " . ((!empty($price)) ? "'price' => '$price', " : "") . " 
      " . ((!empty($type)) ? "'type' => '$type', " : "") . " 
      " . ((!empty($status)) ? "'status' => '$status', " : "") . " 
      " . ((!empty($weight)) ? "'weight' => '$weight', " : "") . " 
      " . ((!empty($tax_class_id)) ? "'tax_class_id' => '$tax_class_id', " : "") . " 
      " . ((!empty($categories)) ? "'categories' => '$categories', " : "") . " 
      " . ((!empty($manufacturer)) ? "'manufacturer' => '$manufacturer', " : "") . " 
      " . ((!empty($color)) ? "'color' => '$color', " : "") . " 
      " . ((!empty($url_key)) ? "'url_key' => '$url_key', " : "") . " 
      " . ((!empty($url_path)) ? "'url_path' => '$url_path', " : "") . " 
      " . ((!empty($visibility)) ? "'visibility' => '$visibility', " : "") . " 
      " . ((!empty($news_from_date)) ? "'news_from_date' => '$news_from_date', " : "") . " 
      " . ((!empty($news_to_date)) ? "'news_to_date' => '$news_to_date', " : "") . " 
      " . ((!empty($special_price)) ? "'special_price' => '$special_price', " : "") . " 
      " . ((!empty($special_from_date)) ? "'special_from_date' => '$special_from_date', " : "") . " 
      " . ((!empty($special_to_date)) ? "'special_to_date' => '$special_to_date', " : "") . " 
      " . ((!empty($meta_title)) ? "'meta_title' => '$meta_title', " : "") . " 
      " . ((!empty($meta_keyword)) ? "'meta_keyword' => '$meta_keyword', " : "") . " 
      " . ((!empty($meta_description)) ? "'meta_description' => '$meta_description', " : "") . " 
      " . ((!empty($enable_googlecheckout)) ? "'enable_googlecheckout' => '$enable_googlecheckout', " : "") . " 
      " . ((!empty($custom_design)) ? "'custom_design' => '$custom_design', " : "") . " 
      " . ((!empty($custom_design_from)) ? "'custom_design_from' => '$custom_design_from', " : "") . " 
      " . ((!empty($custom_design_to)) ? "'custom_design_to' => '$custom_design_to', " : "") . " 
      " . ((!empty($custom_layout_update)) ? "'custom_layout_update' => '$custom_layout_update', " : "") . " 
      " . ((!empty($page_layout)) ? "'page_layout' => '$page_layout', " : "") . " 
      " . ((!empty($old_id)) ? "'old_id' => '$old_id', " : "") . " 
      " . ((!empty($required_options)) ? "'required_options' => '$required_options', " : "") . " 
      " . ((!empty($has_options)) ? "'has_options' => '$has_options', " : "") . " 
      " . ((!empty($image_label)) ? "'image_label' => '$image_label', " : "") . " 
      " . ((!empty($small_image_label)) ? "'small_image_label' => '$small_image_label', " : "") . " 
      " . ((!empty($thumbnail_label)) ? "'thumbnail_label' => '$thumbnail_label', " : "") . " 
      " . ((!empty($gift_message_available)) ? "'gift_message_available' => '$gift_message_available', " : "") . " 
      " . ((!empty($cost)) ? "'cost' => '$cost', " : "") . " 
      " . ((!empty($is_in_stock)) ? "'is_in_stock' => '$is_in_stock', " : "") . " 
      " . ((!empty($qty)) ? "'qty' => '$qty', " : "") . " 
      " . ((!empty($minimal_price)) ? "'minimal_price' => '$minimal_price', " : "") . " 
      " . ((!empty($tier_price)) ? "'tier_price' => '$tier_price', " : "") . " 
      " . ((!empty($options_container)) ? "'options_container' => '$options_container', " : "") . " 
     );"; 

     /* evaluate code */ 
     eval($code); 

     /* cause i dunno if i could simple add the attributes under the evaluated code, i'm merging each attribute to main array with product data like productdata = (array)productdata + (array)attributeX */ 
    /* $attributes[$iteration] = "myattributename like custommanufacturer" */ 
    /* $attributeoptions[$iteration] = "myoptionname like customname" */ 
     while($iteration != $attr_count -1) 
     { 
     $iteration++; 
     $attributeData = array($attributes[$iteration] => $attributeoptions[$iteration]); 
     $newProductData = array_merge((array)$newProductData,(array)$attributeData); 
     } 
    if ($proxy->create($type, $set['set_id'], $sku, $newProductData)) 
     { 
      $hp_bereich .= "\nsuccess=yes"; 
      $hp_bereich .= "\nwarning="; 
      $hp_bereich .= "\nerrorcode=0"; 
      $hp_bereich .= "\nSKU=" .$sku; 
     } 
     else 
     { 
      $hp_bereich .= "\nsuccess=no"; 
      $hp_bereich .= "\nwarning=Error creating product see scriptblock"; 
      $hp_bereich .= "\nerrorcode=-2001"; 
      $hp_bereich .= "\nSKU=" .$sku; 
     } 

現在問題: 產品創建:完成 屬性將顯示在產品內部的管理面板(下拉選擇選項):nope。

任何人都可以幫助我解決這個問題嗎?

我必須從頭開始創建一個產品(包含很少的信息),包括將由用戶控制/填充信息的php文件創建的屬性。

+0

以及我剛剛注意到我的自定義生成的屬性沒有添加到默認attributeset(必須編輯我的PHP)。現在它將顯示自定義屬性的下拉菜單,但不會顯示通過生成產品設置的選項。 – 2011-12-19 14:01:02

回答

2

首先,最重要的是,不要使用eval。這太冒險了,可能允許代碼注入,如果只有其中一個變量沒有被正確驗證,它可能包含攻擊代碼。許多網絡主機完全禁用它是有原因的。在這種情況下,eval沒有任何好處,它實現了任何你不能做的事。


確實做出簡單的產品非常簡單。

Mage::getModel('catalog/product') 
    ->setTypeId('simple') 
    ->setWebsiteIds($websites) 
    ->setName($name) 
    ->setDescription($description) 
    ->setDescriptionShort($description_short) 
    // etc... 
    ->save(); 

該setters是神奇的方法,所以你可以使用任何你喜歡的屬性名稱。如果傳遞的值爲null,則屬性未設置,因此無條件地調用所有這些設置器是安全的。如果您正在構建從一個數組的變量 - 可能使用extract() - 那麼它使用更快捷,直接的數組:如果你想了解它是如何在現有產品的商店的屬性,使用類似

Mage::getModel('catalog/product') 
    ->setTypeId('simple') 
    ->addData($array) 
    ->save(); 

如下:

$product = Mage::getModel('catalog/product')->load($productId); 
print_r($product->debug()); 

這是一個很好的調試技術,並揭示了很多。這樣做可以查找下拉屬性的存儲方式。

+0

首先,感謝「eval」 - 信息,所以這個「魔術」 - 方法會像這樣:setMyspecialattribute如果它的名字爲「myspecialattribute」?下劃線如「my_special_attribute」怎麼樣?我是否必須爲它或ID設置選項的名稱?我將檢查調試功能以獲取更多信息。謝謝 – 2011-12-19 15:12:02

+0

關於下劃線的好處;他們變成了駱駝案件。 'my_special_attribute'成爲'setMySpecialAttribute()'和'getMySpecialAttribute()'。 – clockworkgeek 2011-12-19 15:19:45

+0

當一個屬性是一個下拉列表,然後你設置並獲得它的選項ID。最後,要獲得該值,必須使用略微笨拙的'product-> getAttributeText('my_special_attribute')'。這一次它不是一種神奇的方法,沒有「設置」等價物。 – clockworkgeek 2011-12-19 15:23:53