【问题标题】:add Magento product attributes添加 Magento 产品属性
【发布时间】:2011-11-11 10:25:16
【问题描述】:

我使用 Magento 1.5.1.0。 我想通过 PHP 脚本添加产品。 我有一个自定义属性集,包含 8 个自定义属性,如何通过 php 为自定义属性添加值?

【问题讨论】:

    标签: php magento


    【解决方案1】:
        $host = "127.0.0.1/magento/index.php"; //our online shop url
         $client = new SoapClient('http://'.$host.'/api/soap/?wsdl'); //soap handle
         $apiuser= "user"; //webservice user login
         $apikey = "pw"; //webservice user pass
    
         $sess_id= $client->login($apiuser, $apikey); //we do login
         $attributeSets = $client->call($sess_id, 'product_attribute_set.list');
         $set = current($attributeSets);
    
        $newProductData = array(
                          'name'              => 'name'
                       // websites - Array of website ids to which you want to assign a new product
                        , 'websites'          => array(1) // array(1,2,3,...)
                        , 'short_description' => 'short'
                        , 'description'       => 'description'
                        , 'status'            => 'status'
                        , 'your_attributes'   => $value
                        , 'your_attributes2'   => $value
                        , 'your_attributes3'   => $value
                          and so on 
                      );
    
      try {  
      $client->call($sess_id, 'product.create', array('simple', $set['set_id'], 'sku_of_product', $newProductData));
      }
      catch (Exception $e) { //while an error has occured
      echo "==> Error: ".$e->getMessage(); //we print this 
      }
    

    Hf&GL :D

    问候博蒂

    【讨论】:

    • 谢谢。唯一的问题是我的服务器上没有激活 Soap。还有其他方法吗?
    【解决方案2】:

    通过带有 product.create 或 product.update 的 SOAP(如果它已经存在)

    $newProductData = array('name' => 'name',
                            'your_attribute' => $value
                            ,'your_attribute2' => $value  
                             );
    
     $proxy->call($sessionid, 'product.create', array('simple', $set['set_id'], sku, $newProductData));
    

    然后将使用您的自定义属性创建产品。

    问候博蒂

    【讨论】:

    • 感谢您的快速回复,您能告诉我所有的 php 文件应该是什么样子吗?我在 magento 示例中看到 $proxy = new SoapClient('magentohost/api/soap/?wsdl'); $sessionId = $proxy->login('apiUser', 'apiKey'); $attributeSets = $proxy->call($sessionId, 'product_attribute_set.list'); $set = current($attributeSets);最好的问候
    • 是的,这对我来说很好用。它仅适用于 V1 肥皂 api
    【解决方案3】:

    因为我在搜索使用更高版本的 SOAP API V2 做同样的事情时发现了这个响应,所以我添加了我最终想出的解决方案。

    对于 V2 SOAP API,我们似乎需要将 additional_attributes 嵌套在 multi_data 或 single_data 层中?

    查看app/code/core/Mage/Catalog/Model/Product/Api/V2.php #256 我认为我们需要使用

    $manufacturer = new stdClass();
    $manufacturer->key = "manufacturer";
    $manufacturer->value = "20";
    $additionalAttrs['single_data'][] = $manufacturer;
    

    $manufacturer = new stdClass();
    $manufacturer->key = "manufacturer";
    $manufacturer->value = "20";
    $additionalAttrs['multi_data'][] = $manufacturer;
    

    使用如下:

        $productData = new stdClass();
        $additionalAttrs = array();
    
                // manufacturer from one of the two above ^
    
        $productData->name                   = $data['name']; 
        $productData->description            = $data['description'];
        $productData->short_description      = $data['short_description'];
        $productData->weight                 = 0;
        $productData->status                 = 2; // 1 = active
        $productData->visibility             = 4; //visible in search/catalog
        $productData->category_ids           = $data['categories']; 
        $productData->price                  = $data['price'];
        $productData->tax_class_id           = 2; // 2=standard
        $productData->additional_attributes  = $additionalAttrs;
    
        // Create new product
        try {
            $proxy->catalogProductCreate($sessionId, 'virtual', 9, $sku, $productData); // 9 is courses
        } catch (SoapFault $e) {
            print $e->getMessage();  //Internal Error. Please see log for details.
            exit();
        }
    

    【讨论】:

      猜你喜欢
      • 2016-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多