I've got a project where I need to automate the process of creating entries. I'm doing so using the ExpressionEngine Channel Entries API but as part of that I need to set a price for the entry in the Store field.
As it stands when a Store field is present in the channel I get PHP errors when submitting data, it seems that Store is being called somewhere along the line by the Channel Entries API and then doesn't have the data it's expecting so I get:
Invalid argument supplied for foreach()
Filename: Service/ProductsService.php
Line Number: 137
The code I'm using to inject new entries or update existing entries is as follows:
ee()->load->library('api');
ee()->api->instantiate('channel_entries');
ee()->api->instantiate('channel_fields');
$channel_entry_data = array(
'title' => (string)$data['metadata']->title,
'status' => 'open',
'field_id_188' => (string)$data['metadata']->description,
'field_ft_188' => 'none',
'field_id_206' => strtotime($data['metadata']->date),
'field_ft_206' => 'none',
'field_id_189' => (string)$data['metadata']->type,
'field_ft_189' => 'none',
'field_id_190' => (string)$data['metadata']->format[0],
'field_ft_190' => 'none',
'field_id_208' => (string)$data['metadata']->format[1],
'field_ft_208' => 'none',
'field_id_191' => (string)$data['metadata']->identifier[0], // required
'field_ft_191' => 'none',
'field_id_192' => (string)$data['metadata']->identifier[1], // required
'field_ft_192' => 'none',
'field_id_193' => (string)$data['metadata']->source,
'field_ft_193' => 'none',
'field_id_194' => (string)$data['metadata']->rights,
'field_ft_194' => 'none',
'field_id_209' => $store_product_data,
'field_ft_209' => 'none'
);
ee()->api_channel_fields->setup_entry_settings($channel_id, $channel_entry_data, FALSE);
if (ee()->api_channel_entries->save_entry($channel_entry_data, $channel_id, $existing_entry_id, NULL)) {
return TRUE;
} else {
return FALSE;
}
store_product_data is an array as follows:
$store_product_data = array(
'price'=>1,
'length'=>NULL,
'width'=>NULL,
'height'=>NULL,
'weight'=>NULL,
'handling'=>NULL,
'free_shipping'=>1,
'stock'=>array(
array(
'id'=>NULL,
'sku'=>NULL,
'track_stock'=>NULL,
'stock_level'=>NULL,
'min_order_qty'=>NULL
)
)
);
But this doesn't work, I get the PHP error for every entry I insert and a bunch of empty rows for each entry in the exp_store_products table, but none of the actual product data (price and free shipping toggle).
Any thoughts/suggestions greatly appreciated. I want to use the channel entries API if I can but maybe I just need to resort to a quick and dirty DB insert.