I have an XML with the following structure
<product>
<sku>
<![CDATA[ 11008269 ]]>
</sku>
<name>
<![CDATA[MONTANA 165]]>
</name>
<price_ind>366.00</price_ind>
<Filters>
<filter>
<group id="6">Material</group>
<value id="2">Wood</value>
</filter>
<filter>
<group id="7">Color</group>
<value id="10">White</value>
</filter>
<filter>
<group id="7">Color</group>
<value id="11">Black</value>
</filter>
<filter>
<group id="8">Usage</group>
<value id="49">Outdoors</value>
</filter>
</Filters>
</product>
The filter nodes hold some product attributes. I am trying to parse this XML and create the attributes in PHP. but it is failing. The attributes are added to the product without its corresponding values. The value is empty.
The attribute terms are getting added in the attribute though.
This is how I am trying to do it
function create_attributes( $id, $xml) {
//create an array of the xml data
$record = json_decode( json_encode( (array) $xml ), true );
$count = count($product_attributes);
$i = 0;
if ( $record['Filters']['filter'] != null )
{
//store existing product attributes in order not to get overwritten
$product_attributes = get_post_meta($id, '_product_attributes',true);
//iterate through the XML
while( array_key_exists( $i, $record['Filters']['filter'] ) ) {
if ( $record['Filters']['filter'][ $i ]['group'] == "Material" )
{
$attribute = "pa_material";
wp_set_object_terms($id, $record['Filters']['filter'][ $i ]['value'], 'pa_material' , true);
}
else
if ( $record['Filters']['filter'][ $i ]['group'] == "Color" )
{
$attribute = "pa_color";
wp_set_object_terms($id, $record['Filters']['filter'][ $i ]['value'], 'pa_color' , true);
}
if ( $record['Filters']['filter'][ $i ]['group'] == "Usage" )
{
$attribute = "pa_usage";
wp_set_object_terms($id, $record['Filters']['filter'][ $i ]['value'], 'pa_usage' , true);
}
//build the attributes array
$product_attributes[$attribute] = array(
'name' => $attribute,
'value' => $record['Filters']['filter'][ $i ]['value'],
'position' => $count,
'is_visible' => '1',
'is_variation' => '0',
'is_taxonomy' => '1');
$i++;
$count++;
}
update_post_meta( $id, '_product_attributes', $product_attributes ); }