Magento使用自创属性和选项创建新产品

时间:2011-12-19 13:56:34

标签: php api magento attributes product

这里的类似问题并没有帮助我解决我的问题,所以我自己也会问。

我有一个php文件,它创建一个属性(Drop-Down)和一个为它创建选项的php文件。

现在我想创建一个php文件,它为我生成一个简单的magento产品,其中包含一个或多个属性(之前创建过)

这是我为其生成数据的代码:

$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文件的用户创建的属性信息。

1 个答案:

答案 0 :(得分:2)

首先,最重要的是,不要使用eval。它风险太大,可能允许代码注入,如果这些许多变量中的一个没有经过适当的验证,它可能包含攻击代码。许多Web主机完全禁用它是有原因的。在这种情况下,eval没有任何好处,它实现了你无法做到的一切。


制作一个简单的产品确实很简单。

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

setter是魔术方法,因此您可以使用任何您喜欢的属性名称。如果传递的值为null,则取消设置该属性,以便无条件地调用所有这些setter是安全的。如果要从数组中构建这些变量 - 可能使用extract() - 那么直接使用数组会更快:

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

如果您想了解现有产品如何存储它的属性,请使用以下内容:

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

这是一个很好的调试技术并且揭示了很多。执行此操作以查找如何存储下拉属性。