I usually search on my own but I'm having trouble finding documentation on this subject so I'm a bit lost Here is my problem, I need to send the name, price, description in xml in a product, but the price is empty, I wonder if I have the right method to do this?
$file = file_get_contents('php://input');
$xml = simplexml_load_file($file);
$dom = new DOMDocument;
$product = new Product($xml->id);
$product->name = $xml->name;
$product->price = $xml->price;
$product->description = $xml->description;
$product->save();
$id = $dom->createElement('id', "$xml->id");
$name = $dom->createElement('name', "$xml->name");
$price = $dom->createElement('price', "$xml->price");
$description = $dom->createElement('description', "$xml->description");
$dom->appendChild($id);
$dom->appendChild($name);
$dom->appendChild($price);
$dom->appendChild($description);
$dom->formatOutput = true;
$dom->save($file);
$this->output .= $product->id;
My XML :
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<Product>
<name>Test Post</name>
<price>23.900000</price>
<description>test descriptin pour envoie</description>
</Product>
</prestashop>
Thanks !
CodePudding user response:
The error returned is that the object model complains the product price is missing. And this is caused by not properly addressing the corresponding XML element hierarchy.
This is wrong, because it addresses name
, price
and description
directly inside the root element of your XML:
$product->name = $xml->name;
$product->price = $xml->price;
$product->description = $xml->description;
All these are inside the Product
element; so you need to address those accordingly:
/** @var SimpleXMLElement|false The Product element inside the XML */
$xmlProduct = $xml->Product;
// Product element must exist and price is mandatory for a Product object
if ( false !== $xmlProduct && false !== $xmlProduct->price) {
$product->name = $xmlProduct->name ? $xmlProduct->name : 'Unnamed Product';
$product->price = $xmlProduct->price;
$product->description = $xmlProduct->description? $xmlProduct->description : null;
$product->save();
}
Similar issue when you populate your DOM, you are not properly creating the elements hierarchy. It needs look like:
$dom = new DOMDocument;
// Create a new Product element for the new DOMDocument
$productElement = $dom->createElement('Product');
// Assuming the source Product elements and its price are correctly defined
$idElement = $dom->createElement('id', "$xml->Product->id");
$productElement->appendChild($idElement);
$nameElement = $dom->createElement('name', "$xml->Product->name");
$productElement->appendChild($nameElement);
$priceElement = $dom->createElement('price', "$xml->Product->price");
$productElement->appendChild($priceElement);
$descriptionElement = $dom->createElement('description', "$xml->Product->description");
$productElement->appendChild($descriptionElement);
// Create the main $prestashopElement
$prestashopElement = $dom->createElement('prestashop');
// Then attach the $productElement itself to the main $prestashopElement
$prestashopElement->appendChild($productElement);
// Then finally attach the main $prestaShop element to the $dom document
$dom->appendChild($prestashopElement);
It is unclear why you load your document using simplexml, but then switch to the more low level API from DOMDocument to create a new DOMDocument which you later use to overwrite your $file
.
All of this is totally inappropriate if you just expect to update the existing xml document. But those are too many problems and questions for a single post here.
So if you need further help, create a post for each specific question.
Also please, next time don't post a screenshot of the errors, but the errors themselves.