I am importing contacts from a CSV by reading it with PHP and creating an array of data, then using API v3 to create the Contact.
I have been able to create the Contact, add emails, add website, add phone numbers... Now I am trying to assign a Membership.
From the docs, I believe best practice is the Order API. I am chaining together the
api.Order.create and the
api.Payment.create and this works as expected, until I pass in my membership with the following code:
'params' => [ 'contact_id' => "$value.id", // This should be an ID like 215, but is empty. 'membership_type_id' => 4, 'skipStatusCal' => 1, 'status_id' => "Pending" ],
If I set
contact_id to be the next ID that will be generated by CiviCRM, it works as expected but I don't always know what the next ID in Civi will be.
Based on chaining, I would expect to pass in
$value.id which would be the ID but it is empty.
To test this, I have tried chaining
api.Note.create and passing it
$value.id where the correct ID is output.
Here is a sample of my code:
// Contact data is already set before this point. $user_data['api.Order.create'] = [ 'financial_type_id' => 2, 'total_amount' => 0, 'contribution_status_id' => "Pending", 'line_items' => [ [ 'params' => [ 'contact_id' => "$value.id", // This should be an ID like 215, but is empty. 'membership_type_id' => 4, 'skipStatusCal' => 1, 'status_id' => "Pending" ], 'line_item' => [ [ 'entity_table' => "civicrm_membership", 'qty' => 1, 'unit_price' => 0, 'line_total' => 0, 'price_field_id' => 1 ] ] ] ], // Activate Membership. 'api.Payment.create' => [ 'total_amount' => 0, ], ]; $contact = civicrm_api3( 'Contact', 'create', $user_data );
How can I pass the contact_id to the params in the line_items?