【问题标题】:Magento creating new product with self-created attributes and optionsMagento 创建具有自创属性和选项的新产品
【发布时间】:2011-12-19 13:56:33
【问题描述】:

这里的类似问题对我的问题没有帮助,所以我自己问吧。

我有一个创建属性 (Drop-Down) 的 php 文件和一个为其创建选项的 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;
      }

现在问题来了: 产品创造:完美完成 属性将显示在产品内部的管理面板上(带有选定选项的下拉菜单):不。

谁能帮我解决这个问题?

我必须从头开始创建一个产品(信息很少),包括将由控制/填充 php 文件的用户创建的属性。

【问题讨论】:

  • 好吧,我刚刚注意到我的自定义生成属性没有添加到默认属性集中(必须为此编辑我的 php)。现在它将显示我的自定义属性中的下拉菜单,但不会显示通过生成产品设置的选项。

标签: php api magento attributes product


【解决方案1】:

首先,也是最重要的,不要使用eval。这太冒险了,并且可能允许代码注入,如果其中一个变量没有得到适当的验证,它可能包含攻击代码。许多网络主机完全禁用它是有原因的。在这种情况下,eval 没有任何好处,它没有任何你不能做的事情。


做一个简单的产品确实很简单。

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

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

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

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

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

这是一种很好的调试技术,可以揭示很多东西。执行此操作以了解下拉属性的存储方式。

【讨论】:

  • 首先,感谢“eval”信息,所以这个“魔法”方法会类似于:setMyspecialattribute 如果它的名字是“myspecialattribute”?像“my_special_attribute”这样的下划线呢?我必须为其设置选项的名称还是ID?我将检查调试功能以获取更多信息。谢谢
  • 关于下划线的要点;他们变成了骆驼案。 my_special_attribute 变为 setMySpecialAttribute() 和 getMySpecialAttribute()。
  • 当属性是下拉菜单时,您可以设置并获取它的选项 ID。最后要获得价值,您必须使用略显笨拙的$product->getAttributeText('my_special_attribute')。这次它不是魔术方法,也没有“设置”等价物。
  • 感谢“技术术语”camelcase :D 好吧,我只需要将选项的值(或 id)转移到产品中,以便下拉列表看起来像:DropDownAttribute --> 1. me 2. 你 3. 我们 4. 我们从它那里取回 id / value 我已经有了一个函数。非常感谢,如果我之后有任何问题或者如果它有效,会写信;)
  • 对不起,烦人的问题,但是否有可能(php4 兼容,不知道信息是否重要)这样写:$_product->set .$camelcasedattribute -> $attributoptionid?
猜你喜欢
  • 2014-02-10
  • 2014-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-27
  • 2016-02-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多