Jump to content

How to add a product through the webservice with custom feature values


bruno_pereira

Recommended Posts

  • 1 month later...

Hi mate! Sorry if i am late.. but this is how iam doing.. the only problem is i am not getting new categories in associations working properly...

Good luck!

//DEFINICIÓN DE CONSTANTES Y DATOS DE NUESTRA SHOP
define('DEBUG', false); // or true better for debugin! o.O
define('PS_SHOP_PATH', 'https://yourwebsite');
define('PS_WS_AUTH_KEY', 'yourauthkey');
require_once('../PSWebServiceLibrary.php');

try {

    $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
    $opt = array('resource' => 'products');
    $xml = $webService->get(array('url' => PS_SHOP_PATH . '/api/products?schema=blank'));
    $resource_product = $xml->children()->children();

    unset($resource_product->id);
    unset($resource_product->position_in_category);
    unset($resource_product->manufacturer_name);
    unset($resource_product->id_default_combination);
    unset($resource_product->associations);

    $resource_product->id_shop = 1;
    $resource_product->minimal_quantity = 1;
    $resource_product->available_for_order = 1;
    $resource_product->show_price = 1;
    //$resource_product->quantity = 10;           // la cantidad hay que setearla por medio de un webservice particular
    $resource_product->id_category_default = 2;   // PRODUCTOS COMO CATEGORÍA RAIZ
    $resource_product->price = 12.23;
    $resource_product->active = 1;
    $resource_product->visibility = 'both';
    $resource_product->name->language[0] = "blablabla";
    $resource_product->description->language[0] = "blablabla";
    $resource_product->state = 1;

    // CHAPTER 1 // ADDING NEW PRODUCTS... IN FEW WAYS -> NO SUCCESS!!

    $resource_product->addChild('associations')->addChild('categories')->addChild('category')->addChild('id', 6);
    $resource_product->addChild('associations')->addChild('categories')->addChild('category')->addChild('id', '7');
    $resource_product->addChild('associations')->addChild('categories')->addChild('category')->addChild('id', "8");
    //$resource_product->associations->categories->category->addChild('id', 5);

    $opt = array('resource' => 'products');
    $opt['postXml'] = $xml->asXML();
    $xml = $webService->add($opt);
    $id = $xml->product->id;
    echo "<p>PRODUCTO " . $id . " AÑADIDO</p>";

    //set_product_quantity(100,$id,);

    // CHAPTER 2 // UPDATING PRODUCT -> NO SUCCESS!!

    $new_product_categories = array(29, 30, 31); // List of categories to be linked to product
    $xml = $webService->get(array('resource' => 'products', 'id' => $id));
    $product = $xml->children()->children();

    // Unset fields that may not be updated
    unset($product->manufacturer_name);
    unset($product->quantity);

    // Remove current categories
    unset($product->associations->categories);

    // Create new categories
    $categories = $product->associations->addChild('categories');

    foreach ($new_product_categories as $id_category) {
        $category = $categories->addChild('category');
        $category->addChild('id', $id_category);
    }

    $xml_response = $webService->edit(array('resource' => 'products', 'id' => $id, 'putXml' => $xml->asXML()));

    echo "<p>PRODUCTO " . $xml_response->product->id. "  ACTUALIZADO CON CATEGORIAS</p>";


} catch (PrestaShopWebserviceException $e) {
    // Here we are dealing with errors
    $trace = $e->getTrace();
    if ($trace[0]['args'][0] == 404) echo 'Bad ID';
    else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
    else echo '<b>ERROR:</b> ' . $e->getMessage();
}

 

 

And to add features, you use separate webservice for it .. similar way...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...