Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
convert CT1 price modifiers to CT2
<?php
$fieldtypes = array(
'ct_price_mod' => 'cartthrob_price_modifiers',
'ct_coupon' => 'cartthrob_discount',
'ct_items' => 'cartthrob_order_items',
'ct_matrix_pq' => 'cartthrob_price_quantity_thresholds',
);
foreach ($fieldtypes as $field_type => $new_field_type)
{
//get fields that match this old field type
$fields = get_instance()->db->select('field_id')
->where('field_type', $field_type)
->get('channel_fields')
->result();
foreach ($fields as $field)
{
$field_data = NULL;
//get the entries with field data for this field id
$entries = get_instance()->db->select('entry_id, field_id_'.$field->field_id)
->where('field_id_'.$field->field_id.' !=', '')
->get('channel_data')
->result();
foreach ($entries as $entry)
{
if ($field_type === 'ct_items')
{
$data = @unserialize($entry->{'field_id_'.$field->field_id});
if (is_array($data))
{
foreach ($data as $row_id => $row)
{
$insert = array(
'order_id' => $entry->entry_id,
'row_order' => $row_id,
);
foreach (array('entry_id', 'title', 'quantity', 'price') as $key)
{
$insert[$key] = (isset($row[$key])) ? $row[$key] : '';
unset($row[$key]);
}
$insert['extra'] = (count($row) > 0) ? base64_encode(serialize($row)) : '';
get_instance()->db->insert('cartthrob_order_items', $insert);
}
$field_data = 1;
}
}
else if ($field_type === 'ct_price_mod')
{
$data = @unserialize($entry->{'field_id_'.$field->field_id});
if (is_array($data))
{
foreach ($data as &$row)
{
if (isset($row['option']))
{
$row = array_merge(array('option_value' => $row['option']), $row);
unset($row['option']);
}
}
$field_data = base64_encode(serialize($data));
}
}
else
{
$field_data = base64_encode($entry->{'field_id_'.$field->field_id});
}
if ( ! is_null($field_data))
{
get_instance()->db->update(
'channel_data',
array('field_id_'.$field->field_id => $field_data),
array('entry_id' => $entry->entry_id)
);
}
}
}
get_instance()->db->update('channel_fields', array('field_type' => $new_field_type), array('field_type' => $field_type));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.