Your account is limited to posting only in the Pre-Sales Questions forum.
Support forum access is limited to members with a purchase history.

If you are working on behalf of another member, please contact us with the name of the license holder and we will upgrade your account.
   
 
Create a package with PHP
Posted: 31 July 2012 01:30 AM   [ Ignore ]
Wallflower
Rank
Total Posts:  11
Joined  2012-07-26

Hello,

I’m trying to create a little time saving device for my end-users that will create a package for them using php. I have started to write the script that will do this, but I’m having trouble constructing the $data array for the Cartthrob Package field. Can you help?

This script should create a new package entry with package field containing products with IDs 4, 5, & 6. The custom field ID of my package field is 33. The channel ID is 9.

My script is below, and I’ve marked the place where I’m stuck.

Any pointers greatly appreciated!!


Mike

<?php 
$channel_id 
9;
$this->EE->load->library('api');
$this->EE->api->instantiate('channel_entries');
$this->EE->api->instantiate('channel_fields');

$data = array(
        
'title'         => 'Package Title',
        
'entry_date'    => '1256953732',
        
'field_id_33'    => ''// I need help here...
        
'field_ft_33'    => '' // and here.
);

$this->EE->api_channel_fields->setup_entry_settings($channel_id$data);

if (
$this->EE->api_channel_entries->submit_new_entry($channel_id$data) === FALSE)
{
        show_error
('An Error Occurred Creating the Entry');
}
?> 
Profile
 
 
Posted: 31 July 2012 02:01 AM   [ Ignore ]   [ # 1 ]
Administrator
Avatar
RankRankRankRankRank
Total Posts:  10218
Joined  2008-09-29

I’m assuming field_id_33 is your package field?

and the field_ft_33 is probably it’s format field… which can probably be left blank, or set to NULL. Note sure.

Basically, if that’s your package field, you need to find the data from an existing package, and look at it’s structure. The data’s stored serialized, so to get at it, you need to do the following:

you'll need something like this function

function _unserialize($data, $base64_decode = FALSE)
                {
                    if (is_array($data))
                    {
                        return $data;
                    }

                    if ($base64_decode)
                    {
                        $data = base64_decode($data);
                    }

                    if (FALSE === ($data = @unserialize($data)))
                    {
                        return array();
                    }

                    return $data;
                } 

 

and you’ll need to run something like this to see its structure:

var_export(_unserialize($your_package_field_data$decode TRUE)); 

 

Basically you need to organize your package like that structure, and then shove it into that field_id_33 like this:

'field_id_33' => base64_encode(serialize($your_array)), 

 

It may sound like a bit much, but it’s actually pretty easy once you see it in action once. It’s all about serializing and unserializing data. The channel_entries api might do that for you, but I don’t know for sure. I don’t use it much (we have our own scripts around here for that kind of thing). In any case, it’s either going to be a serialized array, or a straight array.

 Signature 

We’re moving away from the forums, though not entirely and not immediately. Lack of support lately is coincidental to that. Unfortunately we’ve had a bad month with it, but we do not forsee that we will have any continued problems with support through our portal going forward, or on forums while we’re still transitioning. We will send out a newsletter and make additional notifications through twitter and other outlets over the coming weeks.

Moving forward; things returning to normal.

Profile
 
 
Posted: 31 July 2012 02:31 AM   [ Ignore ]   [ # 2 ]
Wallflower
Rank
Total Posts:  11
Joined  2012-07-26

Wow, thanks for getting back so quickly.

Here’s the format of the array:

$package_products = array ( => array ( 'entry_id' => '4''description' => '', ), => array ( 'entry_id' => '5''description' => '', ), ); 

So we can just stuff it is using:

'field_id_33' => base64_encode(serialize($package_products)),
'field_ft_33' => 'none' 

Thanks a million for your help!

Profile
 
 
Posted: 31 July 2012 02:35 AM   [ Ignore ]   [ # 3 ]
Administrator
Avatar
RankRankRankRankRank
Total Posts:  10218
Joined  2008-09-29

Let me know if that doesn’t work for you.

 Signature 

We’re moving away from the forums, though not entirely and not immediately. Lack of support lately is coincidental to that. Unfortunately we’ve had a bad month with it, but we do not forsee that we will have any continued problems with support through our portal going forward, or on forums while we’re still transitioning. We will send out a newsletter and make additional notifications through twitter and other outlets over the coming weeks.

Moving forward; things returning to normal.

Profile
 
 
Posted: 31 July 2012 02:56 AM   [ Ignore ]   [ # 4 ]
Wallflower
Rank
Total Posts:  11
Joined  2012-07-26

Ah, it doesn’t work!

It’ll create the entry but the package products haven’t been added.

Here is the code (with real IDs in this time):

$channel_id 14;
$package_products = array ( => array ( 'entry_id' => '36''description' => '', ), => array ( 'entry_id' => '37''description' => '', ), );

$this->EE->load->library('api');
$this->EE->api->instantiate('channel_entries');
$this->EE->api->instantiate('channel_fields');

$data = array(
        
'title'         => 'PHP Added a Beginners Course!',
        
'entry_date'    => '1256953732',
        
'field_id_96' => base64_encode(serialize($package_products)),
        
'field_ft_96'    => 'none'
);

$this->EE->api_channel_fields->setup_entry_settings($channel_id$data);

if (
$this->EE->api_channel_entries->submit_new_entry($channel_id$data) === FALSE)
{
        show_error
('An Error Occurred Creating the Entry');

Interestingly, I tried putting a previously encoded and know correct string directly in place of the “base64_encode…” bit and it didn’t write anything to the DB then either.

 

Profile
 
 
Posted: 03 August 2012 02:06 AM   [ Ignore ]   [ # 5 ]
Wallflower
Rank
Total Posts:  11
Joined  2012-07-26

Anybody got any ideas for this?

Profile
 
 
Posted: 03 August 2012 03:59 AM   [ Ignore ]   [ # 6 ]
Wallflower
Rank
Total Posts:  11
Joined  2012-07-26

SOLVED: You don’t need to encode or serialise the array - Cartthrob does that for you:

'field_id_96' => $package_products
Profile
 
 
Posted: 03 August 2012 04:44 AM   [ Ignore ]   [ # 7 ]
Administrator
Avatar
RankRankRankRankRank
Total Posts:  10218
Joined  2008-09-29

Glad you got this sorted. Sorry I missed the last response.

 Signature 

We’re moving away from the forums, though not entirely and not immediately. Lack of support lately is coincidental to that. Unfortunately we’ve had a bad month with it, but we do not forsee that we will have any continued problems with support through our portal going forward, or on forums while we’re still transitioning. We will send out a newsletter and make additional notifications through twitter and other outlets over the coming weeks.

Moving forward; things returning to normal.

Profile