I am using XMLRPC to build an XML structure that passes across product information to a 3rd party system. I need to build an associative array of the product custom options and I don’t know what syntax to use as the value in each case is an object.
I can’t debug and play about with it as I normally would as believe it or not I’ve had to do this on a live site so I’ve been emailing myself the array to make sure it looks alright, then when it does I’ve applied it to the site, XMLRPC throws an error saying it can’t serialize the object I’ve built, then I quickly switch it back out again.
If I hardcode it like this it works fine.
$item_array = array(
"product_id" => new xmlrpcval($item->getSku()),
"name" => new xmlrpcval($item->getName()),
"price" => new xmlrpcval($item->getBaseCalculationPrice(), 'double'),
"vat_inclusive" => new xmlrpcval(0,'int'),
"quantity" => new xmlrpcval($item->getQty(),'int'),
"option_text" => new xmlrpcval(
array(
"option_1" => new xmlrpcval("Colour: Military Black"),
"option_2" => new xmlrpcval("Sizes: L")
),
"struct")
);
It’s the folowing section I need to generate, specifically the array in a foreach loop as I don’t know how many options there will be;
"option_text" => new xmlrpcval(
array(
"option_1" => new xmlrpcval("Colour: Military Black"),
"option_2" => new xmlrpcval("Sizes: L")
),
"struct")
If I do it like below then it comes out fine, but the value is a string rather than an object, which XMLRPC can’t serialize;
$optioncount = 1;
$attList = array();
foreach ( $attributes as $attribute => $value ) {
$attpair = implode(": ", $value);
$attList['option_'. $optioncount] = 'new xmlrpcval("'.$attpair.'")';
$optioncount++;
}
If I var_dump($attList) I get;
array(2) {
["option_1"]=>
string(39) "new xmlrpcval("Colour: Military Black")"
["option_2"]=>
string(25) "new xmlrpcval("Sizes: L")"
}
Any other way seems to turn $attList into a total mess – I know this should be very basic but for the life of my I can’t get this working. Thanks for any pointers.
If I var_dump($attList) when I use new xmlrpcval($attpair); I get;
array(2) {
["option_1"]=>
object(xmlrpcval)#469 (3) {
["me"]=>
array(1) {
["string"]=>
string(22) "Colour: Military Black"
}
["mytype"]=>
int(1)
["_php_class"]=>
NULL
}
["option_2"]=>
object(xmlrpcval)#433 (3) {
["me"]=>
array(1) {
["string"]=>
string(8) "Sizes: L"
}
["mytype"]=>
int(1)
["_php_class"]=>
NULL
}
}
Building your array must look like:
And then: