1

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.

1

After having a phone call with DevDemon support last November, I resorted to using the CodeIgniter active record to update the entries instead. This is because the Store fieldtype uses Javascript to populate certain data when creating or updating an entry with the Store field type. When you use the channel entries API to create or update entries, it is completely unaware of the Store fieldtype JS. Channel Images is the same way FYI.

| improve this answer | |
0

I don't think the Store field type gets it data from the data array that you pass to the channel entries call - I think it always expects to find the data in the $_POST variable.

Make sure there is something in the field, eg:

$channel_entry_data['field_id_209'] = 'store';

and then create a $_POST variable with your Store data:

$_POST['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
            )
        )
    );
| improve this answer | |
  • Hi Andrew, I gave this a shot adding the exact code above directly before the EE channel entries API call but I still get the same PHP warning and none of the posted data appears to make it through to the row in exp_store_products. :/ – Nathan Pitman Dec 1 '14 at 15:55
  • Think I may have to resort to a manual DB insert with the same data and just force hide the warnings on output. Thanks for taking a look anyway! :) – Nathan Pitman Dec 1 '14 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.