使用REST API将产品分配到Magento中的特定类别

时间:2014-07-03 15:50:05

标签: php magento

我使用下面的代码将产品发布到Magento,但是我需要知道如何将产品分配到特定类别。有人知道解决方案吗?

提前致谢。

$oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);
$resourceUrl = "$apiUrl/products";

$productData = json_encode(array(
            'type_id'           => 'simple',
            'attribute_set_id'  => 4,
            'sku'               => 'simple' . uniqid(),
            'weight'            => 1,
            'status'            => 1,
            'visibility'        => 4,
            'name'              => 'Name of the product',
            'description'       => 'Description',
            'short_description' => 'Short Description',
            'price'             => 6.99,
            'tax_class_id'      => 0
        ));
        $headers = array('Content-Type' => 'application/json');
        $oauthClient->fetch($resourceUrl, $productData, OAUTH_HTTP_METHOD_POST, $headers);

1 个答案:

答案 0 :(得分:0)

解决。这是在将产品添加到magento过程之后。

$productId = 100; //Extracted from the previous response
$categoryId = 4;
$resourceUrl = "$apiUrl/products/$productId/categories";
$productData = json_encode(array('category_id' => $categoryId));
$headers = array('Content-Type' => 'application/json');
$oauthClient->fetch($resourceUrl, $productData, OAUTH_HTTP_METHOD_POST, $headers);
$response = $oauthClient->getLastResponseInfo();