Jump to content

PSWebServiceLibrary - Webservice - Adding product simple (without combination) there are stock problem


Faina

Recommended Posts

I use PSWebServiceLibrary to have a "dialog" with Prestashop.
If I add a product with this simple code:
 

xmlResponse = $webService->get(['url' => PS_SHOP_PATH."/api/products?schema=blank"]);
$fieldsXML = $xmlResponse->product[0];
$fieldsXML->state = "1";
$fieldsXML->name = "test";
$fieldsXML->price = "10.00"; 
try {
    $addResponse = $webService->add([ 'resource' => "products", 'postXml' => $xmlResponse->asXML() ]);
    $fieldsXML = $addResponse->product[0];
    echo 'Enjoy-> Your product Id is: '.$fieldsXML->id;
} 
catch (PrestaShopWebserviceException $e) { 
    echo "No good -> ".$e->getMessage(); 
}

I have my new product in catalog->products... but I haven't my new product in catalog->stocks.
When I go to catalog->stocks, my console show an error like this:
TypeError: Cannot read property 'split' of null (stock.bundle.js?1.7.6.3:7)

I have seen that the PS has create in Dbase a row in table `product_attribute` associated at new product (instead of there isn't row in tables like `product_attribute`, `product_attribute_combination`, `product_attribute_image`, ecc)
** If I delete it (by phpMyAdmin), than all work fine and the product will be visible in stocks **

If I create the product by dashboard all work fine and if I check table `product_attribute` there isn't correctly the product

I have searched many solution, like change attributes values in the insert, change them in a second time... nothing.

Link to comment
Share on other sites

I have solved... and I think that is a bug of Prestashop or PSWebServiceLibrary
With my original code I get ALL ATTRIBUTE of the product blank shema 

$xmlResponse = $webService->get(['url' => PS_SHOP_PATH."/api/products?schema=blank"]);
$fieldsXML = $xmlResponse->product[0];

I edit only some attribute 

$fieldsXML->state = "1";
$fieldsXML->name = "test";
$fieldsXML->price = "10.00"; 
$fieldsXML->advanced_stock_management=true;
$fieldsXML->low_stock_alert=false;
$fieldsXML->minimal_quantity = "1"; 
$fieldsXML->available_for_order = true;
$fieldsXML->active = true; 

I send my new xml (attribute edited and other attribute received) to Prestashop.

$webService->add([ 'resource' => "products", 'postXml' => $xmlResponse->asXML() ]);

The problem is in the result that I have from get([...]);
The tag inside $fieldsXML have a format like (for example) <weight/> and not <weight></weight> 
So, when I edit the attribute the format will be "correct" (<state>1</state>) the other remain <attribute/>... this will be a problem when inserting the product because it seems that it's not "completely accepted" by Prestashop.

To solve the problem I have to edit each attribute as empty and then edit that I need.
This is my working code:

$xmlResponse = $webService->get(['url' => PS_SHOP_PATH."/api/products?schema=blank"]);
$fieldsXML = $xmlResponse->product[0];
foreach ($fieldsXML as $nodeKey => $node){
    $fieldsXML->$nodeKey = "";
}
$fieldsXML->state = "1";
$fieldsXML->name = "test";
$fieldsXML->price = "10.00"; 
$fieldsXML->advanced_stock_management=true;
$fieldsXML->low_stock_alert=false;
$fieldsXML->minimal_quantity = "1"; 
$fieldsXML->available_for_order = true;
$fieldsXML->active = true; 
try {
    $addResponse = $webService->add([ 'resource' => "products", 'postXml' => $xmlResponse->asXML() ]);
    $fieldsXML = $addResponse->product[0];
    echo 'Enjoy-> Your product Id is: '.$fieldsXML->id;
} 
catch (PrestaShopWebserviceException $e) { 
    echo "No good -> ".$e->getMessage(); 
}

I hope that I have explained well and that it is helpful for everyone... because there aren't many info on web.

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...