簡體   English   中英

添加Magento產品屬性

[英]add Magento product attributes

我使用Magento 1.5.1.0。 我想通過PHP腳本添加產品。 我有一個自定義屬性設置8個自定義屬性,我如何通過PHP添加自定義屬性的值?

    $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 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();
    }

通過帶有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));

然后將使用您的自定義屬性創建產品。

關心boti

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM