3

I am attempting to add a field to the entity commerce_payment_transaction. However, when I check the database after installation which should create the field, there is no field_data_ created, nor is there any field on the entity commerce_payment_transaction. I have successfully used code similar to the following to create field on the Commerce Order. Do I have the bundle wrong for the instance?

Here is my code.

During the install, I create the field and its instance.

function commerce_stripe_connect_c_install_install() {

$field_info = field_info_field('tr_vendor_user_reference');

$field = array(
        'field_name' => 'tr_vendor_user_reference',
        'type' => 'user_reference',
        'cardinality' => 1,
        'entity_types' => array('commerce_payment_transaction'),
        'translatable' => FALSE,
        'settings' => array(
                //must have the referenceable_status defined or the user reference won't work
                'referenceable_status' => array(
                        0 => 0,
                        1 => '1',
                ),
        ),


);

if (empty($field_info)) {
    field_create_field($field);
}

$instance = field_info_instance('commerce_payment_transaction', 'tr_vendor_user_reference', 'payment_method');

//I check if the vendor_user_reference has already been created on the commerce_product
if (empty($instance)) {
    $new_instance = array(
            'field_name' => 'tr_vendor_user_reference',
            'entity_type' => 'commerce_payment_transaction',
            'bundle' => 'payment_method',

            'label' => t('Transaction Vendor User Reference'),
            'required' => TRUE,
            'settings' => array(
                    //This is required so that vendors will be available for programmatically selecting
                    //for a product
                    'referenceable_status' => array(
                            0 => 0,
                            1 => '1',
                    ),

                    'view' => array(
                            'args' => array(),
                            'display_name' => '',
                            'view_name' => '',
                    ),

            ),
            //The widget will not show up because it is hidden later on, but this is needed so that
            //a default value can be chosen
            'widget' => array(
                    'active' => 1,
                    'module' => 'options',
                    'settings' => array(
                            'apply_chosen' => '',
                    ),
                    'type' => 'options_select',
                    'weight' => '40',
            ),


            'display' => array(),
    );
    field_create_instance($new_instance);
}

2 Answers 2

2

The payment transaction entity type isn't field-able (see commerce_payment_entity_info()).

But you can change that with hook_entity_info_alter():

function MYMODULE_entity_info_alter(&$info) {
  $info['commerce_payment_transaction']['fieldable'] = TRUE;
}
2
  • I had thought about that, but I thought that fieldable controlled the UI for fields. I am sure that this was part of the problem, but it isn't the complete problem. I just tried setting the fieldable setting, and it still did not create the field. I thought it was because the field creation happens in the .install, but I separated out the setting of fieldable into another module that is enabled first, and it still did not work.
    – angms.bh
    Commented Jul 7, 2015 at 2:48
  • I figured out my problem. I had misnamed the install function. I am now able to create the fields and instances, but when I try to set the new value, it doesn't work. I am using commerce_payment_transaction_new to create my transaction. I set my new value using $transaction->tr_vendor_user_reference=$current_vendor. When I save it using commerce_payment_transaction_save, the value does not get saved to the database in the field_data_tr_vendor_user_reference table.
    – angms.bh
    Commented Jul 7, 2015 at 22:16
1

There are a few things that you need to do to add a field to a transaction

1) set the commerce_payment_transaction to fieldable as mentioned above by Clive.

2) You will need to separate the the implementation of function MYMODULE_entity_info_alter(&$info) from your field definition if you put your field definition in the .install file. This is because the install function is run before the .module file

3) When defining your field in hook_install, you will need to set the bundle on the instance to the payment method id that you are using. In my code above, I had used 'bundle' => 'payment_method'. It needs to be the payment_method identifier. This is the same value you use when creating a new transaction using commerce_payment_transaction_new

As an aside, in my above example I used a user_reference type. If you are using this on your transaction, when you want to set the value for the transaction, you do the following:

$transaction->tr_vendor_user_reference['und']['0']['uid'] = $your_uid_as_integer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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