Hi All,
Arrays seem to be my downfall so hope someone can help!
I have the following:
$pa_no_of_doors_value = array_shift( wc_get_product_terms( 1008, 'pa_no-of-doors', array( 'fields' => 'names' ) ) );
echo '<br/>No Of Doors: '.$pa_no_of_doors_value.'<br/>';
$pa_no_of_doors['type'] = array(
'name' => htmlspecialchars(stripslashes('No Of Doors')),
'value' => $pa_no_of_doors_value,
'position' => 1,
'is_visible' => 1,
'is_variation' => 0,
'is_taxonomy' => 0
);
print_r($pa_no_of_doors);
echo '<p> </p>';
$pa_car_make_value = array_shift( wc_get_product_terms( $post_id, 'pa_car-make', array( 'fields' => 'names' ) ) );
echo '<br/>Car Make: '.$pa_car_make_value.'<br/>';
$pa_car_make['type'] = array(
'name' => htmlspecialchars(stripslashes('Car Make')),
'value' => $pa_car_make_value,
'position' => 1,
'is_visible' => 1,
'is_variation' => 0,
'is_taxonomy' => 0
);
print_r($pa_car_make);
I then use:
update_post_meta($post_id, '_product_attributes', $pa_car_make);
The above then updates the DB (based on Wordpress).
As you can see, this is only updating with one of the options. What I'm trying to do is a foreach loop (as I've got many more add, then pass all of them into the _product_attributes field
Having added them via Wordpress and looked at the DB, I can see how it needs to then appear:
a:2:{
s:8:"car-make";a:6:{s:4:"name";s:8:"Car Make";s:5:"value";s:12:"Aston Martin";s:8:"position";s:1:"0";s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:0;}
s:14:"pa_no-of-doors";a:6:{s:4:"name";s:14:"pa_no-of-doors";s:5:"value";s:0:"";s:8:"position";s:1:"1";s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:1;}
}
So thinking something like:
update_post_meta($post_id, '_product_attributes', serialize( $attributes ));
But my brain just can't work out how to do a foreach of the various types! Sorry for asking a probably simple question!!